Author Topic: Deleting all files and subfolders in a specified directory via a batch script  (Read 2664 times)

sveakul

  • Sr. Member
  • ****
  • Posts: 2463
A while back I did a lot of checking on how to do this when I didn't realize that there already was a MusicBee option that did the same thing!  I was looking to clear the contents of the AlbumCovers directory--neatly done by Tools/Advanced/Reset Artwork Cache, which actually just deletes that whole directory (auto-recreated of course when needed).

Anyway, because there were a lot of unclear/varying/outright wrong examples of such a script floating around on Google, I thought I'd post this one because, 1) it WORKS, 2) you can use for any occasion, now that you know MusicBee was already a step ahead of the game  :)

This will clear the contents of any directory between the quotes in the "set" statement of both subfolders and files, including hidden ones.  It first deletes all the files, then any subdirectories (because the latter cannot be deleted first if they contain files).  The folder it is run against (the last one in your "set" statement) will be retained.  Copy it into a text file, and rename with a ".bat" extension, with the "set" path changed to whatever folder you plan to run it for:

Code
@ECHO OFF

Set dir="c:\Music\Test Files"

Echo Deleting all files from %dir%
del %dir%\* /F /Q

Echo Deleting all folders from %dir%
for /d %%p in (%dir%\*) Do rd /Q /S "%%p"
@echo Folder deleted.

exit

Tested here with Windows 7 SP1.