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. IF "%~1"=="/?" GOTO Syntax
  7. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  8.  
  9. :: Go to start directory
  10. :: The original version did not use doublequotes for %CD%, making the code vulnerable to code
  11. :: insertion exploits, as explained at http://www.thesecurityfactory.be/command-injection-windows.html
  12. :: As we will use %StartDir% only for the following PUSHD operation, doublequotes do not inhibit
  13. :: the code from working properly - problem solved.
  14. SET StartDir="%CD%"
  15. IF NOT "%~1"=="" SET StartDir="%~1"
  16. PUSHD %StartDir%
  17.  
  18. :: Display header
  19. ECHO Directory	Space used (MB)
  20. ECHO.=========	===============
  21.  
  22. :: Display disk usage for start directory
  23. IF NOT EXIST *.* GOTO SubDirs
  24. FOR /F "tokens=3,4*" %%A IN ('DIR "%~1" /A-D /-C ^| FIND /I "File(s)"') DO SET .\=%%A
  25. SET /A .\=.\+524288
  26. SET /A .\=.\/1048576
  27. ECHO..\	%.\%
  28. SET .\=
  29.  
  30. :: Display disk usage for every subdirectory
  31. :SubDirs
  32. FOR /D %%A IN (*.*) DO CALL :List "%%~A"
  33.  
  34. :: Done
  35. POPD
  36. GOTO End
  37.  
  38.  
  39. :List
  40. :: Set variable value to bytes used by directory
  41. FOR /F "tokens=3,4*" %%B IN ('DIR /A /-C /S "%~1" ^| FIND /I "File(s)"') DO SET ListDir=%%~B
  42. :: Add 0.5MB in order to properly round the value when integer divided by 1MB
  43. SET /A ListDir=%ListDir%+524288
  44. :: Integer divide by 1MB
  45. SET /A ListDir=%ListDir%/1048576
  46. :: Display resulting value in MB
  47. ECHO.%~1	%ListDir%
  48. :: Clear variable
  49. SET ListDir=
  50. GOTO:EOF
  51.  
  52.  
  53. :Syntax
  54. ECHO.
  55. ECHO DiskUse, Version 4.20 for Windows NT 4 / 2000 / XP
  56. ECHO Display disk space used by subdirectories (tab delimited)
  57. ECHO.
  58. ECHO Usage:  DISKUSE  [ startdir ]
  59. ECHO.
  60. ECHO Where:  "startdir"  is the directory containing subdirectories to be
  61. ECHO                     inventoried (default is the current directory)
  62. ECHO.
  63. ECHO Note:   Due to batch math limitations this batch file will return negative
  64. ECHO         numbers if the disk space used by subdirectories exceeds 2GB.
  65. ECHO         For Windows 2000/XP, upgrade to version 5 and use its /L switch.
  66. ECHO.
  67. ECHO Written by Rob van der Woude
  68. ECHO http://www.robvanderwoude.com
  69.  
  70. :End
  71. IF "%OS%"=="Windows_NT" ENDLOCAL
  72.  

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