First, a caution. An incomplete whitelist may cause deletion of important files. Furthermore, these 'junk folders' are often required for the correct operation of software that created them. If you still opt for this solution, here's a one-liner, with line breaks for readability:
pushd C:\Users\John\Documents &&
for /D %i in (*) do @(
echo %i| findstr /V /I /R /C:"^Important$" /C:"^Keep me$" > NUL &&
echo rmdir /S /Q %i
) & popd
In its current form, this statement does not do anything harmful. The second echo displays, rather than executes the 'rmdir' command and is included as a precaution. Check whether the script would work as intended, then remove the second occurence of 'echo'.
A brief explenation. pushd
temporarily changes directory to the documents folder. for /D
lists all directories in there. For each directory, findstr
matches its name against the whitelist. The switches /V /I /R
print only non-matching entries, make the search case-insensitive and enable regular expressions. Every directory that is to be kept is then listed as /C:"^dirname$"
. Directories not on the whitelist will make findstr
return errorlevel zero, thus executing the command after the double ampersands, i.e. rmdir /S /Q %i
, deleting that directory, empty or not, without asking for confirmation.
If run from a batch file instead of directly from the command line, replace every %i
with %%i
.
1Vbs and powershell will have no issue what so ever scripting this. Batch you would need a lot of work arounds to do it. Couple of quick things to add, does it need to be recursive? Does the list of folders to keep need to change? – 50-3 – 2013-09-14T08:57:42.143
It just needs to work and preferably have an easy to maintain array for other people to adjust what folders they want to whitelist, writing something static and not easy to update wouldn't be worth it for others who want to do the same thing after we've finished and forgotten about it. And yes, by change I mean we edit the file and easily whitelist a folder. By delete I mean move to the recycling bin. VBS seems more viable than batch though I'm not very familiar with it. – John – 2013-09-14T10:34:47.513
1You can use Vbs to open and read a txt file and build an array using the split function. Then it's just a if statement based off the output of a folder listing from the my documents location – 50-3 – 2013-09-14T11:03:09.437
possible duplicate of How to delete files from a folder using a list of file names in windows?
– Ƭᴇcʜιᴇ007 – 2013-09-14T19:28:47.110