0

I have a SSD with several partitions.

I need to work at J:\ and I need to delete everything inside J:\, except the following folders:

J:\System Volume Information  
J:\$RECYCLE.BIN  
J:\New  
J:\Old Stuff  

And the following sub-folders:

J:\Papers\Folder  
J:\Room\Main Documents  
J:\Year\Origin\Part  
J:\Year\Origin\Part Two  

After reading a lot, I wrote:

@ECHO OFF 
SETLOCAL 
SET "sourcedir=J:\"
SET "keepdir=System Volume Information"
SET "keepdir=$RECYCLE.BIN"
SET "keepdir=New"
SET "keepdir=Old Stuff"
SET "keepdir=Papers\Folder"
SET "keepdir=Room\Main Documents"
SET "keepdir=Year\Origin\Part"
SET "keepdir=Year\Origin\Part Two"
FOR /d %%a IN ("%sourcedir%\*") DO IF /i NOT "%%~nxa"=="%keepdir%" RD /S /Q "%%a"
GOTO :EOF

Unfortunately, the code above deletes everything inside J:\.

Compo
  • 36,585
  • 5
  • 27
  • 39
Lepefe
  • 15
  • 3
  • 1
    Every time you `set keepdir=...`, you're overwriting the prior `keepdir`. You might try to adapt this solution: https://stackoverflow.com/a/52017655/1024832 – Marc Jun 15 '23 at 20:17
  • I never wrote a batch before. Please, wow I need to write the code in order to avoid the overwriting of set keepdir? Thks – Lepefe Jun 15 '23 at 20:22
  • 1
    @Lepefe - that would be true in any programming language, not just batch. – SomethingDark Jun 16 '23 at 00:49

1 Answers1

1

One simple solution would be to use robocopy which is part of Windows:

md J:\empty
robocopy /mir J:\empty J:\ /xd keep /xd keep2

This will mirror an otherwise empty directory into the target, thus deleting everything. EXCEPT for those paths listed as exclusions (/xd path).

user1016274
  • 4,071
  • 1
  • 23
  • 19
  • 1
    I never wrote a batch before. Please, what exactly will be "keep" or "keep2"? I need to replace "keep" and "keep2" with the full path (ie J:\\Year\\Origin\\Part Two)? Or is it enough "Year\\Origin\\Part Two" etc? Please, how to exactly write the exceptions? Thks – Lepefe Jun 15 '23 at 20:31
  • Open a Command Prompt window, type `%SystemRoot%\System32\Robocopy.exe /?`, then press the `[ENTER]` key, to find out @Lepefe. – Compo Jun 16 '23 at 13:04
  • `keep` is just an example. To be on the safe side, use absolute paths, like `J:\keep`. In your case, `"J:\Old Stuff"` - remember to put quotes around paths which contain spaces. – user1016274 Jun 16 '23 at 21:21
  • @user1016274 thank you! Now I understand how to use paths in your code. Please, last question: I thank you for your code and I understand this could be the right way to do what I need. But please, I still want to know, why the code I posted doesn't work? What need to be fixed in order to make my posted code work? Thank you for your patience. – Lepefe Jun 16 '23 at 23:01
  • Already mentioned, that only the last assignment to `keepdir` is effective - you overwrite it many times. Then, the for-loop starts at the top level folder. If that folder is NOT excluded, all subfolders will be deleted. Even if one of those matches your `%keepdir%`. If you want to SEE what happens, replace `RD /S /Q` with `echo`, and remember that each printed folder is completely removed before the loop enters a subfolder. – user1016274 Jun 17 '23 at 13:22
  • @user1016274 amazing answer and help, thank you once again. I'm working really hard using your first solution (md J:\empty). Thanks to your great help, I'm very close to solve my needs! I opened a new request for help, and it'll amazing if you can continue to help me: https://stackoverflow.com/questions/76496613/batch-to-delete-and-copy-backup-specific-folders-and-sub-folders – Lepefe Jun 17 '23 at 14:23
  • @Lepefe: if my post answered your question then please mark it as the answer, for guidance to others visiting this topic later. Glad I could help. – user1016274 Jun 17 '23 at 19:57