简体   繁体   中英

Batch File To Delete Folders Not Contained In a List

I have a group of folders that I would like to keep and would like to delete the rest. I have a .txt file that contains the names of the folders that I would like to keep.

So here is what I have:

In C:\\test I have the following folders:

AAA
BBB
CCC
DDD
EEE

In my text file exclude.txt I have listed the following folder names:

AAA
BBB
CCC

Thus, I would like my end product to be that C:\\test should only have the folders AAA , BBB , and CCC after the batch file.

Here is what I have so far but not having any luck:

for /d %%a in ("C:\test\*") do findstr /i /x /c:"%%~nxa" exclude.txt || rd /s /q %%a

Looks like you're pretty close. The following works for me... (just added a '\\' character to your directory name)

for /d %%a in ("C:\test\*") do findstr /i /x /c:"%%~nxa" exclude.txt || rd /s /q %%a

I had the same issue as the guy above that it was only working for the first line in my exclusion file.

after a while i realised it was atually just not wokring for the last line in my exclusion list but because i was testing it with only 2 lines i couldnt tell the difference.

when i put real data in with 84 exclusions i realise what was happening and my workaround was to put a dummy exclusion at the end of the file, knowing it gets ignored anyway

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM