Home > front end >  How can i delete multiple specific folders from a directory with a bat file?
How can i delete multiple specific folders from a directory with a bat file?

Time:06-04

Kind of a continuation of a previous question, but i have 570 folders, where some of them contain another folder called "autosaves", and i would like to create a bat file which finds these and deletes them (the autosaves folder, not the actual folder which contains the autosaves folder), but after a little trial and error to no avail, I figured I would come back here as i got a very good result last time.

So basically.

 MainFolder
 ---ExampleFolder1   (does not contain an autosaves folder)
 ---ExampleFolder2   (contains an autosaves folder)
 ---ExampleFolder3   (contains an autosaves folder)
 ---ExampleFolder4   (does not contain an autosaves folder)

I need this to become:

 MainFolder
 ---ExampleFolder1   (does not contain an autosaves folder)
 ---ExampleFolder2   (does not contain an autosaves folder)
 ---ExampleFolder3   (does not contain an autosaves folder)
 ---ExampleFolder4   (does not contain an autosaves folder)

so if anyone knows that would be great :)

CodePudding user response:

FOR /f "delims=" %%b IN ('dir /s /b /ad "u:\your files" ^|sort /r') DO IF /i "%%~nxb"=="autosave" ECHO RD /s /q "%%b"

where u:\your files gets replaced by your directory.

Find all directorynames in full, sort them in reverse order and if the leaf is named autosave, remove it.

The rd command is merely echoed for testing purposes. When verified, remove the echo keyword to activate.

The list is sorted in reverse order so that any autosave directory containing other directories is processed after those subdirectories, hence the code should not attempt to delete a subdirectory of an already-deleted directory.

NB This code will remove all subdirectories named autosave in the tree, so a subdirectory example\something\anotherthing\autosave\somethingelse will be lopped after anotherthing

  • Related