windows 如何通过cmd调用删除文件夹中的所有文件和文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1502913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete all files and folders in a folder by cmd call
提问by ufukgun
I use Windows.
我使用 Windows。
I want to delete all files and folders in a folder by system call.
我想通过系统调用删除文件夹中的所有文件和文件夹。
I may call like that:
我可以这样称呼:
>rd /s /q c:\destination
>md c:\destination
Do you know an easier way?
你知道更简单的方法吗?
回答by Joey
No, I don't know one.
不,我一个都不认识。
If you want to retain the original directory for some reason (ACLs, &c.), and instead really want to empty it, then you can do the following:
如果您出于某种原因(ACL,&c.)想保留原始目录,而实际上想清空它,那么您可以执行以下操作:
del /q destination\*
for /d %x in (destination\*) do @rd /s /q "%x"
This first removes all files from the directory, and then recursively removes all nested directories, but overall keeping the top-level directory as it is (except for its contents).
这首先从目录中删除所有文件,然后递归删除所有嵌套目录,但总体上保持顶级目录不变(除了其内容)。
Note that within a batch file you need to double the %
within the for
loop:
请注意,在批处理文件中,您需要%
在for
循环内加倍:
del /q destination\*
for /d %%x in (destination\*) do @rd /s /q "%%x"
回答by Sean
del c:\destination\*.* /s /q
worked for me. I hope that works for you as well.
del c:\destination\*.* /s /q
对我来说有效。我希望这也适用于你。
回答by Banan
I think the easiest way to do it is:
我认为最简单的方法是:
rmdir /s /q "C:\FolderToNotToDelete\"
The last "\" in the path is the important part.
路径中的最后一个“\”是重要的部分。
回答by Rosberg Linhares
Yes! Use Powershell:
是的!使用 Powershell:
powershell -Command "Remove-Item 'c:\destination\*' -Recurse -Force"
回答by fractor
If the subfolder names may contain spaces you need to surround them in escaped quotes. The following example shows this for commands used in a batch file.
如果子文件夹名称可能包含空格,您需要用转义引号将它们括起来。以下示例为批处理文件中使用的命令显示了这一点。
set targetdir=c:\example
del /q %targetdir%\*
for /d %%x in (%targetdir%\*) do @rd /s /q ^"%%x^"
回答by Maxim Suslov
To delete file:
删除文件:
del PATH_TO_FILE
To delete folder with all files in it:
要删除包含所有文件的文件夹:
rmdir /s /q PATH_TO_FOLDER
To delete all files from specific folder (not deleting folder itself) is a little bit complicated. del /s *.*
cannot delete folders, but removes files from all subfolder. So two commands are needed:
从特定文件夹中删除所有文件(不删除文件夹本身)有点复杂。del /s *.*
不能删除文件夹,但会从所有子文件夹中删除文件。所以需要两个命令:
del /q PATH_TO_FOLDER\*.*
for /d %i in (PATH_TO_FOLDER\*.*) do @rmdir /s /q "%i"
You can create a script to delete whatever you want (folder or file) like this mydel.bat
:
您可以创建一个脚本来删除您想要的任何内容(文件夹或文件),如下所示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%
Few example of usage:
几个用法示例:
mydel.bat "path\to\folder with spaces"
mydel.bat path\to\file_or_folder
回答by BateTech
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.
一个简单的单行选项是在文件系统的某处创建一个空目录,然后使用ROBOCOPY
( http://technet.microsoft.com/en-us/library/cc733145.aspx) 和/MIR
开关删除所有文件和子文件夹。默认情况下,robocopy 不复制安全性,因此根文件夹中的 ACL 应保持不变。
Also probably want to set a value for the retry switch, /r
, because the default number of retries is 1 million.
也可能想为重试开关设置一个值/r
,因为默认的重试次数是 100 万次。
robocopy "C:\DoNotDelete_UsedByScripts\EmptyFolder" "c:\temp\MyDirectoryToEmpty" /MIR /r:3
回答by Ynotinc
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:
我有一个包含 33 个文件夹的索引文件夹,需要删除其中的所有文件和子文件夹。我在 index 文件夹中打开了一个命令行,然后使用了这些命令:
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.
我将它们分成两行(在第一行之后按 Enter,当要求更多时添加第二行),因为如果在一行上输入,这可能不起作用。此命令将擦除每个目录,然后创建一个新的空目录,从而删除原始目录中的所有文件和子文件夹。
回答by Developer
Navigate to the parent directory
导航到父目录
Line1 pushd "Parent Directory"
Line1 推送“父目录”
Delete the sub folders
删除子文件夹