简体   繁体   中英

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. So in this case only folders from 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

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; 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

List the files/folders you want to delete to a file with dir and call my program on that file.

The following command will produce a plain text file containing only Folder Names (change the C:\\ to suit your path).

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

The folders.txt file will be in your current folder and can be edit easily. The /b switch tells DIR to include only bare information.

DELTREE is not valid in Windows 7 and I'm not sure when it was dropped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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