简体   繁体   English

从批处理文件中的子目录中删除文件夹

[英]Delete folders from child directories in a batch file

How to delete a list of folders relative to the folder in which the batch file is placed ?如何删除与放置批处理文件的文件相关的文件夹列表?

What I mean is that the script should walk through all child directories (from the current directory) and delete only the folders from the defined list.我的意思是脚本应该遍历所有子目录(从当前目录)并只删除定义列表中的文件夹。

The actual question is: what is the best way to define the list of folders so that it's easy to edit the list in the script?实际问题是:定义文件夹列表以便在脚本中轻松编辑列表的最佳方法是什么?

Example structure示例结构

Here's the example structure of folders:以下是文件夹的示例结构:

d:\root\my.bat
d:\root\do_not_remove\
d:\root\delete_this_folder\
d:\root\path\to\folder_to_delete\
d:\root\path\to\another\folder_to_remove\

my.bat should remove only the folders from child directories. my.bat应该只从子目录中删除文件夹。 So in this case only folders from d:\\root\\ .所以在这种情况下,只有来自d:\\root\\文件夹。

In this example, the following folders (and of course all files that are inside them) need to be removed:在此示例中,需要删除以下文件夹(当然还有其中的所有文件):

delete_this_folder\
path\to\folder_to_delete\
path\to\another\folder_to_remove\

List of folders文件夹列表

I need to create a script which is easy to edit because the list of folders may be long and may change very often.我需要创建一个易于编辑的脚本,因为文件夹列表可能很长并且可能经常更改。 Is it possible to define the list of folders at the beginning of the script?是否可以在脚本开头定义文件夹列表?

So the script would then delete the folders in a loop.因此,脚本将循环删除文件夹。

Also, some folders from the list may not exist - so this should not break the script.此外,列表中的某些文件夹可能不存在 - 因此这不应破坏脚本。 Script should delete all folders which exist.脚本应该删除所有存在的文件夹。

Close the console关闭控制台

Also, is it possible to close the console window when script finish its work?另外,脚本完成工作后是否可以关闭控制台窗口?

We suppose that your list.txt contains only the paths of folders to delete我们假设您的list.txt仅包含要删除的文件夹的路径

You can do like this :你可以这样做:

@echo off
FOR /f "delims=" %%A IN ('Type list.txt') Do Echo RMDIR /S /Q "%%A"
pause

So check it with Echo before and if you found it that did the trick;所以之前用Echo检查它,如果你发现它成功了; you should suppress it !你应该压制它!

I actually have the same need, but I solved it by creating a dedicated executable that takes a list of file/folder paths and deletes them.我实际上有同样的需求,但我通过创建一个专用的可执行文件来解决它,该可执行文件采用文件/文件夹路径列表并删除它们。

Just added my little gadget to GitHub : rmList刚刚将我的小工具添加到 GitHub: rmList

List the files/folders you want to delete to a file with dir and call my program on that file.将要删除的文件/文件夹列出到带有dir的文件中,并在该文件上调用我的程序。

The following command will produce a plain text file containing only Folder Names (change the C:\\ to suit your path).以下命令将生成一个仅包含文件夹名称的纯文本文件(更改 C:\\ 以适合您的路径)。

Dir C:\\ /A:D /b > folders.txt目录 C:\\ /A:D /b > folders.txt

The folders.txt file will be in your current folder and can be edit easily. folders.txt 文件将位于您当前的文件夹中,并且可以轻松编辑。 The /b switch tells DIR to include only bare information. /b 开关告诉 DIR 只包含裸信息。

DELTREE is not valid in Windows 7 and I'm not sure when it was dropped. DELTREE 在 Windows 7 中无效,我不确定它何时被删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM