<http://www.netikka.net/tsneti/info/tscmd113.htm>
Copyright © 2003-2010 by Prof. Timo Salmi  
Last modified Thu 15-Apr-2010 12:07:50

 
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.



113} How do I find all *.TXT files on the C: drive sorted by size?

  @echo off & setlocal enableextensions enabledelayedexpansion
  if exist "%temp%\temp.dir" del "%temp%\temp.dir"
  for /f "delims=" %%f in (
    'dir /a:-d /b /s c:\*.txt') do (
    set filename_="%%~ff"
    set filesize_=0000000000%%~zf
    set filesize_=!filesize_:~-10!
    echo !filesize_! !filename_! >>"%temp%\temp.dir"
    )
  sort /r "%temp%\temp.dir"
  if exist "%temp%\temp.dir" del "%temp%\temp.dir"
  endlocal & goto :EOF

The output might start with something like
  0000401272 "c:\INF2\FAQPAS.TXT"
  0000389257 "c:\BAT\1BATFAQ.TXT"
  0000359101 "c:\CHYDE\INDEX.TXT"
  0000272243 "c:\CMD\1CMDFAQ.TXT"

Should one wish to get rid of the leading filesize:
  @echo off & setlocal enableextensions enabledelayedexpansion
  if exist "%temp%\temp.dir" del "%temp%\temp.dir"
  for /f "delims=" %%f in (
    'dir /a:-d /b /s c:\*.txt') do (
    set filename_="%%~ff"
    set filesize_=0000000000%%~zf
    set filesize_=!filesize_:~-10!
    echo !filesize_! !filename_! >>"%temp%\temp.dir"
    )
  ::
  for /f "delims=" %%a in ('sort /r "%temp%\temp.dir"') do (
    set filename_=%%a
    set filename_=!filename_:~11!
    echo !filename_!
  )
  ::
  if exist "%temp%\temp.dir" del "%temp%\temp.dir"
  endlocal & goto :EOF

Then the output would be something like
  "c:\_F\INF2\FAQPAS.TXT"
  "c:\_F\BAT\1BATFAQ.TXT"
  "c:\_F\CHYDE\INDEX.TXT"
  "c:\_F\CMD\1CMDFAQ.TXT"

Consider again the first solution code. The problem with that solution is that file names with exclamation marks (!) get dropped because of the enabled delayedexpansion. To counter that problem use a subroutine
  @echo off & setlocal enableextensions disabledelayedexpansion
  set tempdir_=%temp%\temp.dir
  if exist "%tempdir_%" del "%tempdir_%"
  for /f "delims=" %%f in (
    'dir /a:-d /b /s c:\*.txt') do (
    call :NameSizeToTemp "%%~ff"
    )
  sort /r "%tempdir_%"
  if exist "%tempdir_%" del "%tempdir_%"
  endlocal & goto :EOF
  ::
  :NameSizeToTemp
  set filename_="%~f1"
  set filesize_=0000000000%~z1
  set filesize_=%filesize_:~-10%
  echo %filesize_% %filename_% >>"%tempdir_%"
  goto :EOF

The above could also be written as
  @echo off & setlocal enableextensions disabledelayedexpansion
  set tempdir_=%temp%\temp.dir
  if exist "%tempdir_%" del "%tempdir_%"
  for /r "C:\" %%f in ("*.TXT") do call :NameSizeToTemp "%%~ff"
  sort /r "%tempdir_%"
  if exist "%tempdir_%" del "%tempdir_%"
  endlocal & goto :EOF
  ::
  :NameSizeToTemp
  set filename_="%~f1"
  set filesize_=0000000000%~z1
  set filesize_=%filesize_:~-10%
  echo %filesize_% %filename_% >>"%tempdir_%"
  goto :EOF

A small part of the output might look something like
  :
  0000123770 "C:\MAIL\BIKES.TXT"
  0000112198 "C:\ACCOUNTS\FEB2008.TXT"
  0000108029 "C:\TEACH\PARAG\ACCLAW.TXT"
  :