Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for diskuse.bat

(view source code of diskuse.bat as plain text)

  1. @ECHO OFF
  2. :: Use local variables
  3. IF "%OS%"=="Windows_NT" SETLOCAL
  4.  
  5. :: Check command line arguments and Windows version
  6. ECHO.%1 | FIND "/" >NUL
  7. IF NOT ERRORLEVEL 1 IF /I NOT "%~1"=="/L" GOTO Syntax
  8. ECHO.%1 | FIND "?" >NUL
  9. IF NOT ERRORLEVEL 1 GOTO Syntax
  10. ECHO.%1 | FIND "*" >NUL
  11. IF NOT ERRORLEVEL 1 GOTO Syntax
  12. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  13. IF NOT "%~1"=="" IF /I NOT "%~1"=="/L" IF NOT EXIST "%~1" GOTO Syntax
  14. SET LongFormat=1
  15. IF /I NOT "%~1"=="/L" IF /I NOT "%~2"=="/L" SET LongFormat=0
  16.  
  17. :: Go to start directory
  18. :: The original version did not use doublequotes for %CD%, making the code vulnerable to code
  19. :: insertion exploits, as explained at http://www.thesecurityfactory.be/command-injection-windows.html
  20. :: As we will use %StartDir% only for the following PUSHD operation, doublequotes do not inhibit
  21. :: the code from working properly - problem solved.
  22. SET StartDir="%CD%"
  23. IF NOT "%~1"=="" IF /I NOT "%~1"=="/L" SET StartDir="%~1"
  24. PUSHD %StartDir%
  25. IF ERRORLEVEL 1 GOTO Syntax
  26.  
  27. :: Display header
  28. ECHO Directory	Space used (MB)
  29. ECHO.=========	===============
  30.  
  31. :: Display disk usage for start directory
  32. IF NOT EXIST *.* GOTO SubDirs
  33. FOR /F "tokens=3,4*" %%A IN ('DIR "%~1" /A-D /-C ^| FIND /I "File(s)"') DO SET ListDir=%%A
  34. :: Different procedures depending on /L switch
  35. IF "%LongFormat%"=="1" GOTO LongFormat
  36. SET /A ListDir=%ListDir%+524288
  37. SET /A ListDir=%ListDir%/1048576
  38. ECHO..\	%ListDir%
  39. SET ListDir=
  40. GOTO SubDirs
  41. :LongFormat
  42. :: Strip last 6 digits from value
  43. SET ListDir=%ListDir:~0,-6%
  44. IF NOT DEFINED ListDir SET ListDir=0
  45. :: Display resulting value in MB
  46. ECHO..\	%ListDir%
  47. :: Clear variable
  48. SET ListDir=
  49.  
  50.  
  51. :: Display disk usage for every subdirectory
  52. :SubDirs
  53. FOR /D %%A IN (*.*) DO CALL :List%LongFormat% "%%~A"
  54.  
  55. :: Done
  56. POPD
  57. GOTO End
  58.  
  59.  
  60. :List0
  61. :: Set variable value to bytes used by directory
  62. FOR /F "tokens=3,4*" %%B IN ('DIR /A /-C /S "%~1" ^| FIND /I "File(s)"') DO SET ListDir=%%~B
  63. :: Add 0.5MB in order to properly round the value when integer divided by 1MB
  64. SET /A ListDir=%ListDir%+524288
  65. :: Integer divide by 1MB
  66. SET /A ListDir=%ListDir%/1048576
  67. :: Display resulting value in MB
  68. ECHO.%~1	%ListDir%
  69. :: Clear variable
  70. SET ListDir=
  71. GOTO:EOF
  72.  
  73.  
  74. :List1
  75. :: Set variable value to bytes used by directory
  76. FOR /F "tokens=3,4*" %%B IN ('DIR /A /-C /S "%~1" ^| FIND /I "File(s)"') DO SET ListDir=%%~B
  77. :: Strip last 6 digits from value
  78. SET ListDir=%ListDir:~0,-6%
  79. IF NOT DEFINED ListDir SET ListDir=0
  80. :: Display resulting value in MB
  81. ECHO.%~1	%ListDir%
  82. :: Clear variable
  83. SET ListDir=
  84. GOTO:EOF
  85.  
  86.  
  87. :Syntax
  88. ECHO.
  89. ECHO DiskUse, Version 5.10 for Windows 2000 and later
  90. ECHO Display disk space used by subdirectories (tab delimited)
  91. ECHO.
  92. ECHO Usage:  DISKUSE  [ startdir ]  [ /L ]
  93. ECHO.
  94. ECHO Where:  "startdir"  is the directory containing subdirectories to be
  95. ECHO                     inventoried (default is the current directory)
  96. ECHO         "/L"        is used for large numbers, over 2GB, to prevent return
  97. ECHO                     of negative numbers due to batch math limitations
  98. ECHO                     (integer division by 1000000 instead of properly
  99. ECHO                     rounded mathematical division by 1048576)
  100. ECHO.
  101. ECHO Written by Rob van der Woude
  102. ECHO http://www.robvanderwoude.com
  103.  
  104. :End
  105. IF "%OS%"=="Windows_NT" ENDLOCAL
  106.  

page last modified: 2024-04-16; loaded in 0.0059 seconds