Batch file that deletes folders from My Documents except those in an array

0

I'm absolutely positively sick of random junk folders (CyberPower, VirtualBox, etc) being dumped in to the My Documents folder in Windows. I'm using Windows 7 for clarification. No, it's not the "users file" it's My Documents, I'm also not going to call the Xbox 3 the wrong number. Now that we've clarified that I'd like to figure out a way to make a small batch program that will decimate unknown junk folders and then I'll stick it in the one place I actively allow programs to be run at startup: the startup folder in the all-users folder.

If arrays (or this in general) can not be done via a batch what would be a good platform neutral language to use to approach this problem?

John

Posted 2013-09-14T08:43:56.833

Reputation: 1 490

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

Answers

2

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.

Marcks Thomas

Posted 2013-09-14T08:43:56.833

Reputation: 5 749