160) How do I get a list of all my folders with and sorted by size?
@echo off & setlocal enableextensions enabledelayedexpansion
::
:: Set the auxiliary file names
set temp_=%temp%
if defined mytemp set temp_=%mytemp%
set FolderContent=%temp_%\FullContent.dir
set SizeAndFolder=%temp_%\SizeAndFolder.dir
set FolderList=%temp_%\FolderList.dir
::
:: Make the folder list
dir /s /-c "C:\" > %FolderContent%
:: Traverse it
findstr ".Directory.of. .File(s)." %FolderContent%>%SizeAndFolder%
set Flag=
for /f "tokens=* delims= " %%a in (
'type %SizeAndFolder%') do (
if not defined Flag (
set Folder=%%a
set Flag=true
) else (
set Size= %%a
set Flag=
echo !Size:~-20! !Folder!>>%FolderList%
)
)
::
:: Show the result
sort %FolderList%
:: Clean up
:_out
for %%f in ("%FolderContent%" "%SizeAndFolder%" "%FolderList%") do (
if exist %%f del %%f
)
endlocal & goto :EOF
The output might end with something like
242904830 bytes Directory of C:\WINDOWS\system32\dllcache
359978283 bytes Directory of C:\WINDOWS\$NtServicePackUninstall$
429340336 bytes Directory of C:\WINDOWS\system32
497131051 bytes Directory of C:\WINDOWS\ServicePackFiles\i386
All the best, Timo
--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip
>DRAFT:
>
>160) How do I get a list of all my folders with and sorted by size?
A few comments, Timo.
I actually like the commas in the size as they are easier to read and they
sort ok.
The enabledelayedexpansion is a problem as you can't guarantee that folder
won't have a ! character in the name.
I altered the delims trying to use the file layout effectively but it might
not be a good idea. I also altered the logic in the loop to simplify it.
Finally it loads the file into notepad, with the largest folders at the
top, as an entire drive full of folders is a little cumbersome to view at
the prompt. :)
@echo off & setlocal
::
:: Set the auxiliary file names
set FolderContent="%temp%.\FullContent.dir"
set SizeAndFolder="%temp%.\SizeAndFolder.txt"
set FolderList="%temp%.\FolderList.dir"
:: delete any existing file
if exist %FolderList% del %FolderList%
:: set the drive letter
set drv=c
::
:: Make the folder list
dir /s "%drv%:\" > %FolderContent%
:: Traverse it
findstr ".Directory.of. .File(s)." %FolderContent%>%SizeAndFolder%
set "Folder="
for /f "tokens=2 delims=:)" %%a in (
'type %SizeAndFolder%') do call :next "%%a"
:: sort the result
sort /r < %FolderList% > %SizeAndFolder%
:: Clean up
:_out
for %%f in (%FolderContent% %FolderList%) do (
if exist %%f del %%f
)
:: show the result
start "" notepad %SizeAndFolder%
endlocal & goto :EOF
:next
if not defined Folder (
set Folder=%1
) else (
>>%FolderList% echo %~1 total for %drv%:%Folder%
set "Folder="
)
How about...
----- batch begins -------
[1]@echo off
[2]:: delete tempfiles
[3]for /l %%i in (1,1,3) do del dsz%%i.txt 2>nul >nul
[4]:: dir-list file & dir-size-file
[5]for /f "tokens=*" %%i in ( ' dir /s /b /a:d c:\106x ' ) do >>dsz1.txt
echo %%i&dir "%%i"|find /i "file(s)"|findstr /v ": / \">>dsz2.txt
[6]:: Use the ")" to generate filesize bytes:sequencenumber
[7]setlocal enabledelayedexpansion
[8]set ylp=1000000
[9]for /f "tokens=2delims=)" %%i in (dsz2.txt) do set /a ylp+=1&echo
%%i:!ylp!>>dsz3.txt
[10]endlocal
[11]:: simple sort puts filesize:sequencenumber in order;prefix by
[linenumber]
[12]sort <dsz3.txt |find /v /n "" >dsz2.txt
[13]:: prefix dirnames with [linenumber]
[14]type dsz1.txt|find /v /n "" >dsz3.txt
[15]:: now parse dsz2 to find size, linenumber+1M.
[16]:: find corresponding line in dsz3
[17]for /f "tokens=2,3delims=]:" %%i in (dsz2.txt) do for /f
"tokens=1*delims=[]" %%a in (dsz3.txt) do call :match %%a %%j&if not defined
ymr echo %%i %%b
[18]:: clean up
[19]set ymr=
[20]for /l %%i in (1,1,3) do del dsz%%i.txt 2>nul >nul
[21]goto :eof
[22]
[23]:match
[24]set /a ymr=1000000+%1-%2
[25]if %ymr%==0 set ymr=
[26]goto :eof
------ batch ends --------
Lines start [number] - any lines not starting [number] have been wrapped and
should be rejoined. The [number] that starts the line should be removed
The label :eof is defined in NT+ to be end-of-file but MUST be expressed as
:eof
The spaces surrounding the single-quotes are for emphasis only. The SPACES
are not required but the single-quotes ARE required.
C:\106x... is my test directory.
...which leaves the commas in place and seems immune to the normal gang of
nasties found in file/dirnames (well, I tried % ! and & - others left for
the avid experimenter)
>>>DRAFT:
>>>
>>>160) How do I get a list of all my folders with and sorted by size?
>How about...
>
>----- batch begins -------
>...which leaves the commas in place and seems immune to the normal gang of
>nasties found in file/dirnames (well, I tried % ! and & - others left for
>the avid experimenter)
For short filetrees this is simpler, but far slower on drives with many
folders.
@echo off
if exist "%temp%.\tmp.tmp" del "%temp%.\tmp.tmp"
for /f "tokens=*" %%a in ('dir /s /b /a:d "c:\"') do (
for /f "tokens=2 delims=)" %%b in (
'dir "%%a" -p 2^>nul ^|find " File(s) "'
) do echo>>"%temp%.\tmp.tmp" %%b "%%a"&echo processing "%%a"
)
sort /r <"%temp%.\tmp.tmp" >"%temp%.\Folders listed by size.txt"
start "" notepad "%temp%.\Folders listed by size.txt"