删除 Windows 上非常非常深的子目录树?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8460268/
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
Delete a very, very deep tree of subdirectories on windows?
提问by elite5472
I was testing a file management program today and while looking to solve something else my program ended up not checking for moving a directory to the same directory.
我今天正在测试一个文件管理程序,在寻求解决其他问题时,我的程序最终没有检查将目录移动到同一目录。
The result is a possibly endless recursion of dir1s. I need to commit the changes so I'm desperate to get these out of my repository.
结果可能是 dir1s 的无限递归。我需要提交更改,所以我非常希望将这些更改从我的存储库中删除。
Any ideas?
有任何想法吗?
Basically.. what I got is:
基本上..我得到的是:
dir/dir/dir/dir........./dir/dir/dir It's probably on the thousands.
dir/dir/dir/dir........../dir/dir/dir 大概上千吧。
回答by JohnD
Are you just trying to delete the directory and all subdirectories? RMDIR /S /Q [dirname]
from a command prompt should do the trick.
你只是想删除目录和所有子目录吗? RMDIR /S /Q [dirname]
从命令提示符应该可以解决问题。
UpdateTry this as a workaround:
更新试试这个作为解决方法:
At the top level of the problem directory, create another directory called 'dummy_dir'
Run
robocopy dummy_dir problem_dir /purge
在问题目录的顶层,创建另一个名为“dummy_dir”的目录
跑
robocopy dummy_dir problem_dir /purge
回答by Scott
Install Git Bash or some other form of linux shell emulator. Works perfectly without any hassle.
安装 Git Bash 或其他形式的 linux shell 模拟器。完美运行,没有任何麻烦。
rm -rf /c/offending/dir
rm -rf /c/offending/dir
回答by Salil Kothadia
Thanks to Meghraj Choudhary for the script to delete the files on windows. https://github.com/dev-mraj/fdel
感谢 Meghraj Choudhary 编写的删除 Windows 文件的脚本。 https://github.com/dev-mraj/fdel
He claims it shortens the path and thus help to delete them Helpful for node developers
他声称它缩短了路径,从而有助于删除它们 对节点开发人员有帮助
npm install fdel -g
fdel ./node_modules
回答by Chris Diphoorn
You may use this VBScript I have created - it works very well!
您可以使用我创建的这个 VBScript - 它运行良好!
'Recursively Removes Sub Folders from a Deep Nested Folder Structure.
'By C.Diphoorn March 2020
'
'Usage: cscript RemoveNestedFolders.vbs
'
' Copy This script to the base of the Folder Structure.
' If the Script fails as the subfolders are not deep enough then increase the MaxLimit Variable
If UCase( Right( WScript.FullName, 12 ) ) <> "\CSCRIPT.EXE" Then
X=MsgBox("This script must be run from Admin Command Prompt." & VbCrLf &"Usage: cscript.exe " & WScript.ScriptName )
wscript.quit
end if
Dim fso,oShell, stdout,a,b,Length, SleepSpeed,f,ff
Set oShell = CreateObject("WScript.Shell")
Set fso = CreateObject ("Scripting.FileSystemObject")
'Set stdout = fso.GetStandardStream (1)
StartFolder=oShell.CurrentDirectory
RecursiveSubFolder=""
SleepSpeed = 500
MaxLimit = 99999
' Display the current folders in the current directory
WScript.echo "*** Warning you are about to remove subfolders from within this current folder!"
WScript.echo ""
SET f = fso.GetFolder(StartFolder)
SET ff = f.SubFolders
For Each f1 in ff
wscript.echo " " & f1.name
Next
WScript.echo ""
WScript.echo "Enter the Subfolder Name to Remove:"
RecursiveSubFolder = WScript.StdIn.ReadLine
if RecursiveSubFolder <> "" then
if fso.FolderExists(StartFolder+"\"+RecursiveSubFolder) then
oShell.CurrentDirectory = StartFolder
'Max Length of sub folder path of 255 characters
Length=int((255-len(StartFolder)) /(len(RecursiveSubFolder)+1))
if Length <= 0 then Length =1
a=1
on error resume next
Err.Clear
do
oShell.CurrentDirectory = ".\" & RecursiveSubFolder
if Err.Number >0 then exit do
a=a+1
wscript.echo a
loop until a>=MaxLimit
on error goto 0
' Now move back and remove all subfolders undeath it
do
for b = 1 to length
oShell.CurrentDirectory = ".."
if oShell.CurrentDirectory = StartFolder then exit for
next
FSO.DeleteFolder(RecursiveSubFolder),true
' Wait a Sec to ensure the operating system has a chnace to remove the sub folders
wscript.sleep(SleepSpeed)
a=a-Length
wscript.echo a
loop until a<=1 or oShell.CurrentDirectory = StartFolder
wscript.echo "Complete."
else
wscript.echo "Folder: " & StartFolder & "\" & RecursiveSubFolder & " Not Found."
end if
else
end if
Set oShell = Nothing
Set FSO = Nothing
Set stdout = Nothing
回答by BicaBicudo
In the post of Daniel Santos, just initialize the var del with false and in the next loop add | !del condition:
在 Daniel Santos 的帖子中,只需用 false 初始化 var del 并在下一个循环中添加 | !del 条件:
For example:
例如:
boolean del = false;
while (... | !del) {
...
..
.
Works FINE!
工作正常!
回答by Oli
The robocopy solution works great. Make sure the command line prompt has Admin Rights (you would see "Access denied" msgs, if not).
robocopy 解决方案效果很好。确保命令行提示符具有管理员权限(如果没有,您将看到“拒绝访问”消息)。
BTW: When we had a similar problem, i was using the already installed Webserver (Coldfusion) to recursively delete the folder structure accidentally created by another cfm script. The following code could ev. be rewritten in Java or other lang. - as installing a Webserver might be a bit overkill to overcome your issue ;-)
顺便说一句:当我们遇到类似问题时,我正在使用已经安装的 Webserver (Coldfusion) 递归删除另一个 cfm 脚本意外创建的文件夹结构。下面的代码可以 ev。用 Java 或其他语言重写。- 因为安装网络服务器可能有点矫枉过正来解决您的问题;-)
<!--- Tag to delete all the wrongly created subdirectories by the buggy search index script version
--->
<cfdirectory directory="webroot" name="dirQuery" type="dir" action="LIST" filter="somefilter" recurse="yes">
<cfloop query="dirQuery">
<cfoutput>#directory#\#name#</cfoutput><br/>
<cftry>
<cfdirectory action = "delete" directory = "#directory#\#name#" recurse="yes">
<cfcatch>
<cfoutput>could not delete.</cfoutput><br/>
</cfcatch>
</cftry>
</cfloop>
回答by Daniel Santos
I had to build a java program to do the job. It is important the Threa.sleep(100) that you will see in the next lines because that lets the Windows OS have time to delete the very deeeeeeep directory. This worked for me. I had more than 700 child dirs with the same name and in some cases with files inside.
我必须构建一个 java 程序来完成这项工作。您将在下几行中看到的 Threa.sleep(100) 很重要,因为这让 Windows 操作系统有时间删除 deeeeeeeep 目录。这对我有用。我有超过 700 个同名的子目录,在某些情况下里面还有文件。
Check it out:
一探究竟:
import java.util.Stack;
public class DelDir {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
String path = "C:/your_path/problematic_dir";
File file = new File(path);
File[] files = null;
Stack<File> stack = new Stack<File>();
do {
files = file.listFiles();
if (files != null && files.length > 0){
if (files.length == 1){
stack.push(files[0]);
file = files[0];
System.out.println(file.getPath());
} else {
for (File afile : files){
if (afile.isDirectory()){
stack.push(afile);
file = afile;
System.out.println(file.getPath());
break;
}
}
}
}
}while(files != null && files.length > 0);
File toDel;
boolean del;
System.out.println(String.format("Files to delete: %1$d", stack.size()));
while (stack.size() > 0){
toDel = stack.pop();
del = toDel.delete();
Thread.sleep(100);
if (del){
System.out.println(String.format("[DELETED:OK] %1$s", toDel.getPath()));
}else{
System.out.println(String.format("[DELETED:ERR] %1$s", toDel.getPath()));
}
}
}
}
回答by wileymab
Update: Jan 09, 2015 12:49
更新:2015 年 1 月 9 日 12:49
So after realizing the hard way that my recursion bug was still there (see original post below) I stumbled across this post: https://superuser.com/a/425666. It involves writing a batch file to perform a rename move delete repeat
loop over the problem directory. This actually solves the problem fairly elegantly as it is a top-down delete rather than a bottom-up.
因此,在意识到我的递归错误仍然存在的艰难方式之后(请参阅下面的原始帖子),我偶然发现了这篇文章:https: //superuser.com/a/425666。它涉及编写批处理文件以rename move delete repeat
在问题目录上执行循环。这实际上相当优雅地解决了问题,因为它是自上而下的删除而不是自下而上的删除。
I'd like to note that I tried all of the solutions offered in this thread to no avail. I think I just had a very extreme case (not unlike the OP). The link from superuser.com (above) however performed beautifully.
我想指出的是,我尝试了此线程中提供的所有解决方案均无济于事。我想我只是遇到了一个非常极端的情况(与 OP 不同)。然而,来自 superuser.com(上图)的链接表现出色。
Original Response
原始回复
I had a similar issue today with a recursion bug creating an absurdly huge tree of files in my project structure. It was officially deemed an "undeletable" directory structure in the Microsoft Knowledge Base. (I've since lost the link that I'm quoting there. I if I find it I will update this post to include it.)
我今天遇到了一个类似的问题,一个递归错误在我的项目结构中创建了一个巨大的文件树。在 Microsoft 知识库中,它被正式视为“不可删除”的目录结构。(我已经丢失了我在那里引用的链接。如果我找到它,我会更新这篇文章以包含它。)
Now, I didn't have this structure committed to any repo, but it was grinding my SVN utility and IntelliJ's indexing procedure to complete stand-stills. When I waited long enough both applications eventually bombed out trying to traverse this horrible directory structure.
现在,我没有将此结构提交给任何存储库,但它正在磨练我的 SVN 实用程序和 IntelliJ 的索引程序以完成停顿。当我等待足够长的时间时,两个应用程序最终都试图遍历这个可怕的目录结构。
However, I was able to resolve the issue by simply moving the uppermost directory of the offending directory tree. For example, given this path:
但是,我能够通过简单地移动有问题的目录树的最上面的目录来解决这个问题。例如,给定这个路径:
.../SomeProject/src/test/resources/com/myCompany/myApp/topOfBadTree/bad/bad/.../bad/...
I just created an _undeletable
directory completely outside my project structure and performed a simple GUI-based Cut and Paste of the topOfBadTree
folder - meaning I Cut it from my project and Pasted it in the _undeletable
directory.
我刚刚在_undeletable
我的项目结构之外创建了一个目录,并对topOfBadTree
文件夹执行了一个简单的基于 GUI 的剪切和粘贴- 这意味着我从我的项目中剪切它并将其粘贴到_undeletable
目录中。
Suddenly, IntelliJ and Tortoise SVN were both back to normal, and I was back to debugging. Granted this doesn't necessarily resolve the issue, it sidesteps it, but if the goal is to get the project structure back in order and working again, this should do the trick.
突然,IntelliJ 和 Tortoise SVN 都恢复了正常,我又回到了调试状态。当然,这不一定能解决问题,它会回避问题,但如果目标是让项目结构恢复正常并重新工作,这应该可以解决问题。
This project and all applications discussed were running on Windows 7 Enterprise 64-bit.
该项目和讨论的所有应用程序都在 Windows 7 Enterprise 64 位上运行。
I lost a day to this. Hopefully this helps somebody else NOT do the same.
我为此失去了一天。希望这有助于其他人不要做同样的事情。
回答by Justin.W
None of the above solution works for me. My nested folders are 8000+ deeps at least before any app that counts it crashed too. robocopy crashed on me after 30 minutes or so. What works for me is to create a bat file that will delete these deep nested folders in chunks. The trick is to figure out the repetition pattern within your deep nested.
以上解决方案都不适合我。在任何计算它崩溃的应用程序之前,我的嵌套文件夹至少有 8000 多个深度。30 分钟左右后,robocopy 撞到了我。对我有用的是创建一个 bat 文件,该文件将分块删除这些深层嵌套的文件夹。诀窍是找出深层嵌套中的重复模式。
IE: if your directory structure are \dirA\dirB\dirC\dirD\dirE\dirF\dirA\dirB\dirC\...
, then your repetition pattern is \dirA\dirB\dirC\dirD\dirE\dirF\
. Paste that into the the code snippet below. For my problem my repetition pattern is \demo\demoApp\app\src\main\java\com\utilities\
. There will always be a pattern, so you have to figure this one out, that is how these thing got created in the first place.
IE:如果您的目录结构是\dirA\dirB\dirC\dirD\dirE\dirF\dirA\dirB\dirC\...
,那么您的重复模式是\dirA\dirB\dirC\dirD\dirE\dirF\
。将其粘贴到下面的代码片段中。对于我的问题,我的重复模式是\demo\demoApp\app\src\main\java\com\utilities\
. 总会有一个模式,所以你必须弄清楚这个,这就是这些东西最初是如何被创造出来的。
After that, make sure you call your script again to recursively divide and destroy these once and for all. Mine is located at C:\Users\<your user name>\Desktop\deleteRecursive.bat
, so make sure you correctly define your bat file.
之后,请确保再次调用您的脚本以递归地分割和一劳永逸地销毁这些脚本。我的位于C:\Users\<your user name>\Desktop\deleteRecursive.bat
,因此请确保您正确定义了 bat 文件。
Hope this helps.
希望这可以帮助。
C:
REM CD define the repetition pattern here!
cd C:\demo\demoApp\app\src\main\java\com\utilities\
REM Move the rest of the problem dir to a temporary parent folder
move /-Y demo C:\b
REM CD to the temp folder
cd C:\b
REM delete the section of problem-dir above
rd /s/q C:\demo
REM Move the rest of problem-dir back to origin
move /-Y C:\b\demo C:\
REM Call the script again until problem directory is gone!
call C:\Users\<your user name>\Desktop\delete.bat