Nice programing

cmd 호출로 폴더의 모든 파일과 폴더를 삭제하는 방법

nicepro 2020. 10. 15. 21:42
반응형

cmd 호출로 폴더의 모든 파일과 폴더를 삭제하는 방법


저는 Windows를 사용합니다.

시스템 호출로 폴더의 모든 파일과 폴더를 삭제하고 싶습니다.

다음과 같이 전화 할 수 있습니다.

>rd /s /q c:\destination
>md c:\destination

더 쉬운 방법을 알고 있습니까?


아니, 모르겠어.

어떤 이유로 (ACL, & c.) 원래 디렉토리를 유지하고 대신 실제로 비우려면 다음을 수행 할 수 있습니다.

del /q destination\*
for /d %x in (destination\*) do @rd /s /q "%x"

이것은 먼저 디렉토리에서 모든 파일을 제거한 다음 중첩 된 모든 디렉토리를 반복적으로 제거하지만 전체적으로 최상위 디렉토리를 그대로 유지합니다 (내용 제외).

배치 파일 %내에서 for루프 내에서 두 배가 필요합니다 .

del /q destination\*
for /d %%x in (destination\*) do @rd /s /q "%%x"

del c:\destination\*.* /s /q나를 위해 일했습니다. 나는 그것이 당신에게도 효과가 있기를 바랍니다.


가장 쉬운 방법은 다음과 같습니다.

rmdir /s /q "C:\FolderToNotToDelete\"

경로의 마지막 "\"는 중요한 부분입니다.


예! Powershell 사용 :

powershell -Command "Remove-Item 'c:\destination\*' -Recurse -Force"

하위 폴더 이름에 공백이 포함 된 경우 이스케이프 된 따옴표로 묶어야합니다. 다음 예제는 배치 파일에서 사용되는 명령에 대해이를 보여줍니다.

set targetdir=c:\example
del /q %targetdir%\*
for /d %%x in (%targetdir%\*) do @rd /s /q ^"%%x^"

파일을 삭제하려면 :

del PATH_TO_FILE

모든 파일이있는 폴더를 삭제하려면 :

rmdir /s /q PATH_TO_FOLDER

특정 폴더에서 모든 파일을 삭제하는 것은 (폴더 자체를 삭제하지 않고) 약간 복잡합니다. del /s *.*폴더를 삭제할 수 없지만 모든 하위 폴더에서 파일을 제거합니다. 따라서 두 가지 명령이 필요합니다.

del /q PATH_TO_FOLDER\*.*
for /d %i in (PATH_TO_FOLDER\*.*) do @rmdir /s /q "%i"

다음과 같이 원하는 파일 (폴더 또는 파일)을 삭제하는 스크립트를 만들 수 있습니다 mydel.bat.

@echo off
setlocal enableextensions

if "%~1"=="" (
    echo Usage: %0 path
    exit /b 1
)

:: check whether it is folder or file
set ISDIR=0
set ATTR=%~a1
set DIRATTR=%ATTR:~0,1%
if /i "%DIRATTR%"=="d" set ISDIR=1

:: Delete folder or file
if %ISDIR%==1 (rmdir /s /q "%~1") else (del "%~1")
exit /b %ERRORLEVEL%

사용의 몇 가지 예 :

mydel.bat "path\to\folder with spaces"
mydel.bat path\to\file_or_folder

One easy one-line option is to create an empty directory somewhere on your file system, and then use ROBOCOPY (http://technet.microsoft.com/en-us/library/cc733145.aspx) with the /MIR switch to remove all files and subfolders. By default, robocopy does not copy security, so the ACLs in your root folder should remain intact.

Also probably want to set a value for the retry switch, /r, because the default number of retries is 1 million.

robocopy "C:\DoNotDelete_UsedByScripts\EmptyFolder" "c:\temp\MyDirectoryToEmpty" /MIR /r:3

I had an index folder with 33 folders that needed all the files and subfolders removed in them. I opened a command line in the index folder and then used these commands:

for /d in (*) do rd /s /q "%a" & (
md "%a")

I separated them into two lines (hit enter after first line, and when asked for more add second line) because if entered on a single line this may not work. This command will erase each directory and then create a new one which is empty, thus removing all files and subflolders in the original directory.


Navigate to the parent directory

Line1 pushd "Parent Directory"

Delete the sub folders

Line2 rd /s /q . 2>nul

https://superuser.com/questions/173859/how-can-i-delete-all-files-subfolders-in-a-given-folder-via-the-command-prompt

참고URL : https://stackoverflow.com/questions/1502913/how-to-delete-all-files-and-folders-in-a-folder-by-cmd-call

반응형