windows 递归删除所有以
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1807425/
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
Recursively delete all folders starting with
提问by glmxndr
I need to write a command in a .bat file that recursively deletes all the folders starting with a certain string. How may I achieve this ?
我需要在 .bat 文件中编写一个命令,以递归方式删除以某个字符串开头的所有文件夹。我怎样才能做到这一点?
回答by Marco Demaio
This is the complete answer you are looking for:
这是您正在寻找的完整答案:
FOR /D /R %%X IN (certain_string*) DO RD /S /Q "%%X"
where obviously you need to replace certain_string
with the string your folders start with.
显然,您需要certain_string
用文件夹开头的字符串替换。
This deletes RECURSIVELYas you asked (I mean it goes throught all folders and subfolders).
这会按照您的要求重复删除(我的意思是它会遍历所有文件夹和子文件夹)。
回答by Greg Hewgill
How about:
怎么样:
for /d %a in (certain_string*) do rd /s %a
This will work from the command prompt. Inside a batch file, you would have to double the %
s, as usual:
这将在命令提示符下工作。在批处理文件中,您必须%
像往常一样将s加倍:
@echo off
for /d %%a in (certain_string*) do rd /s %%a
回答by Joey
Unfinished, I think. If you meant "Recursively go down a directory hierarchy to delete all folders starting with a certain string", then the following might suffice:
未完成,我想。如果您的意思是“递归地沿着目录层次结构删除以某个字符串开头的所有文件夹”,那么以下内容可能就足够了:
for /f "delims=" %%x in ('dir /b /ad abc*') do rd /s /q "%%x"
This will recurse into the directory tree, finding all folders starting with "abc", iterate over that list and removing each folder.
这将递归到目录树中,找到所有以“abc”开头的文件夹,遍历该列表并删除每个文件夹。
Maybe you need to wrap an if exist
around the rd
depending on the order in which directories are found and returned. In general, iterating over something and changing it at the same time is rarely a good idea but sometimes it works :-)
也许你需要一个包if exist
的周围rd
取决于其目录找到并归还的顺序。一般来说,迭代某些东西并同时更改它很少是一个好主意,但有时它会起作用:-)
回答by shyamsundar Ankam
rm -rf -- "Directory name"
rm -rf -- "目录名"
Ex : rm -rf -- "-2096378"
例如:rm -rf -- "-2096378"
Above command will deletes the folders/directories starting with - or wildcard characters
以上命令将删除以 - 或通配符开头的文件夹/目录
回答by Shaleen Sinha
FOR /F "tokens=*" %i IN ('DIR **[[SearchTerm]]** /A:D /s /b') do rd /S / Q %i