Die thumbs.db, die!

Someone just told me that they were going to download a program that would clean their windows hard drive of “thumbs.db” files, so I gave them this command line instead:

for /f "tokens=*" %a in ('dir /b /s /aSH thumbs.db') do @(
  echo %a
  del /f /aSH "%a"
)

This very quickly (under 1 minute for my whole C: drive) scans directories recursively for thumbs.db files, removes the “hidden” and “system” attributes and then deletes them, forcing deletion even if the files are read-only.

Of course, you would “cd” to the correct directory first (e.g. cd \ to do the whole drive), if you want to put it in a batch file you need to double up the percent symbols (%a becomes %%a) and you will need appropriate permissions to delete the files it finds (and maybe to change their attributes), so you may need to run this from an elevated command prompt if you are not running it in a directory that you own.

You should also note that the “/aSH” part of the “dir” command (there is no space, as this may also match files and directories called “sh”) assumes that the files are hidden and system files (as they are by default but probably not if you have extracted them from something like a zip file where someone has left them in). If the files are just hidden, just system or neither (just normal files) they will not appear in the list and will not be deleted. An alternative implementation could either run it once with this set and once with just “dir /b /s thumbs.db” or could run it once to remove the S and H attributes (or separately for each of these) and then after they are all “normal” files, run it through again to delete them.

The “echo” line is optional and just shows a verbose output as it deletes things. If you remove it you can also remove the parenthesis and put the whole thing on 1 line if you so wish.

Update:
Better:

for /f "tokens=*" %a in ('dir /b /s /aS thumbs.db') do @attrib -S "%a"
for /f "tokens=*" %a in ('dir /b /s /aH thumbs.db') do @attrib -H "%a"
for /f "tokens=*" %a in ('dir /b /s /aSH thumbs.db') do @attrib -S -H "%a"
 
for /f "tokens=*" %a in ('dir /b /s thumbs.db') do @(
  echo %a
  del /f "%a"
)

Tags:

Leave a Reply