Windows batch script to delete everything in a folder except one

You could try to hide the folders before the for-loop, and unhide them afterwards, like this:

ATTRIB +H D:\myfolder\keepit
FOR /D %%i IN ("D:\myfolder\*") DO RD /S /Q "%%i" DEL /Q "D:\myfolder\*.*"
ATTRIB -H D:\myfolder\keepit

Leave a Comment