<http://www.netikka.net/tsneti/info/tscmd066.htm>
Copyright © 2003-2011 by Prof. Timo Salmi  
Last modified Tue 8-Nov-2011 16:34:23

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



66} How can I delete all the hidden Thumbs.db files from my system?

   

If you use the "View" | "Thumbnails" option in your Files and Folders windows, the hidden Thumbs.db system file in each such folder gradually builds up (if you have not chosen "Do not cache thumbnails" in Folder Options). To delete them you can use e.g. following script. It has the option of bypassing the personal photoalbums folders (mine are at C:\_H\PHOTO and its subfolders.)

  @echo off & setlocal enableextensions enabledelayedexpansion
  ::
  echo +-------------------------------------------------------+
  echo ^| THUMBS.CMD Clear the hidden Thumbs.db thumbnail files ^|
  echo ^| By Prof. Timo Salmi, Last modified Fri 18-Jun-2004    ^|
  echo +-------------------------------------------------------+
  ::
  :: Help wanted?

  if /i [%1]==[?] goto _usage
  if /i [%1]==[/?] goto _usage
  ::
  :: Include also the photo folders into the traversing

  if /i [%1]==[all] goto _all
  ::
  :: Ask in general

  set ask_=
  set /p ask_="Traverse some of the hidden Thumbs.db files [y/N]?"
  if /i not "%ask_%"=="y" (endlocal & goto :EOF)
  ::
  :: Delete one by one

  for /f "tokens=* delims=" %%f in (
    'dir /a:h /s /b c:\Thumbs.db^|find /i /v "_h\photo\"') do (
    call :DeleteOneThumb "%%f")
  endlocal & goto :EOF
  ::
  :_all
  :: Ask in general
  set ask_=
  set /p ask_="Traverse ALL hidden Thumbs.db files [y/N]?"
  if /i not "%ask_%"=="y" (endlocal & goto :EOF)
  ::
  :: Delete one by one

  for /f "tokens=* delims=" %%f in (
    'dir /a:h /s /b c:\Thumbs.db') do (
    call :DeleteOneThumb "%%f")
  endlocal & goto :EOF
  ::
  :_usage
  echo.
  echo Usage: THUMBS [all]
  echo all = traverse also the c:\_h\photo folders
  endlocal & goto :EOF
  ::
  :: =============================================

  :DeleteOneThumb
  dir /a:h %* 2>&1 | find "1 File(s)" > nul
  if %errorlevel% EQU 0 (
    echo.
    for /f "tokens=* delims=" %%f in (%*) do (
      set fsize_=%%~zf
      set fdate_=%%~tf
      set fattr_=%%~af
      )
    echo !fdate_! !fattr_! !fsize_!
    attrib -s -h %* >nul
    del /p %*
    attrib +s +h %* >nul)
  goto :EOF

The output could be something like
  C:\_D\TEST>cmdfaq
  +-------------------------------------------------------+
  | THUMBS.CMD Clear the hidden Thumbs.db thumbnail files |
  | By Prof. Timo Salmi, Last modified Fri 18-Jun-2004    |
  +-------------------------------------------------------+
  Traverse some of the hidden Thumbs.db files [y/N]?y

  04.01.2008 09:27 --ahs---- 4608
  c:\_G\WWW\Thumbs.db, Delete (Y/N)? y

  04.01.2008 08:59 --ahs---- 6144
  c:\_G\WWW\FIGS\Thumbs.db, Delete (Y/N)? y

  04.01.2008 09:27 --ahs---- 8704
  c:\_G\WWW\ICONS\Thumbs.db, Delete (Y/N)? y

The essential trick, since the c:\Thumbs.db are hidden/system files is to detect them. A straight dir will not do that. The /a:h (or /a:-d or /a) switch is needed. As an aside note how the /A switch alone can be interpreted as "ALL", undocumented by DIR /?.

The task could be also done with a single command using DEL to traverse the disk. (See DEL /?). However, I am a bit reticent about using such potent methods. Furthermore, the selective exclusion of groups of files is a problem in this option. Likewise giving the file size, file date and file attribute information prior asking for deletion. However, if you yet want to do it relatively simply, consider this intermediate script:
  @echo off & setlocal enableextensions
  for /f "usebackq delims=" %%f in (
    `dir /s /b /a:-dsh /o:n "C:\Thumbs.db"`) do (
      echo del /p "%%f")
  endlocal & goto :EOF
The echo and the /p switch are for safety.

References/Comments: (If a Google message link fails try the links within the brackets.)
  Google Groups Mar 7 2004, 12:08 am [M]
  Google Groups Jun 17 2004, 5:23 pm [M]