Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

command line - Windows batch script to delete everything in a folder except one

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...