I have a script to delete all subfolders and files in a folder:
FOR /D %%i IN ("D:\myfolder\*") DO RD /S /Q "%%i" & DEL /Q "D:\myfolder\*.*"
And it works great! Only problem is that I would like to exclude one or more folders, like the XCOPY exclude feature.
I just cant figure how I could add that to the script.
Deleting all files in the current Windows command line (DOS) directory, except files with a certain file extension, can best be done with the for command. In the example above, the command is deleting all files in the current directory, except files with the file extension . tiff and . jpg.
When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.
Select multiple files or folders that are not grouped together. Click the first file or folder, and then press and hold the Ctrl key. While holding Ctrl , click each of the other files or folders you want to select.
Deletes a directory. The rmdir command can also run from the Windows Recovery Console, using different parameters.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With