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. SET StartDir=%CD%
  19. IF NOT "%~1"=="" IF /I NOT "%~1"=="/L" SET StartDir=%~1
  20. PUSHD "%StartDir%"
  21. IF ERRORLEVEL 1 GOTO Syntax
  22.  
  23. :: Display header
  24. ECHO Directory	Space used (MB)
  25. ECHO.=========	===============
  26.  
  27. :: Display disk usage for start directory
  28. IF NOT EXIST *.* GOTO SubDirs
  29. FOR /F "tokens=3,4*" %%A IN ('DIR %1 /A-D /-C ^| FIND /I "File(s)"') DO SET ListDir=%%A
  30. :: Different procedures depending on /L switch
  31. IF "%LongFormat%"=="1" GOTO LongFormat
  32. SET /A ListDir=%ListDir%+524288
  33. SET /A ListDir=%ListDir%/1048576
  34. ECHO..\	%ListDir%
  35. SET ListDir=
  36. GOTO SubDirs
  37. :LongFormat
  38. :: Strip last 6 digits from value
  39. SET ListDir=%ListDir:~0,-6%
  40. IF NOT DEFINED ListDir SET ListDir=0
  41. :: Display resulting value in MB
  42. ECHO..\	%ListDir%
  43. :: Clear variable
  44. SET ListDir=
  45.  
  46.  
  47. :: Display disk usage for every subdirectory
  48. :SubDirs
  49. FOR /D %%A IN (*.*) DO CALL :List%LongFormat% "%%~A"
  50.  
  51. :: Done
  52. POPD
  53. GOTO End
  54.  
  55.  
  56. :List0
  57. :: Set variable value to bytes used by directory
  58. FOR /F "tokens=3,4*" %%B IN ('DIR /A /-C /S "%~1" ^| FIND /I "File(s)"') DO SET ListDir=%%~B
  59. :: Add 0.5MB in order to properly round the value when integer divided by 1MB
  60. SET /A ListDir=%ListDir%+524288
  61. :: Integer divide by 1MB
  62. SET /A ListDir=%ListDir%/1048576
  63. :: Display resulting value in MB
  64. ECHO.%~1	%ListDir%
  65. :: Clear variable
  66. SET ListDir=
  67. GOTO:EOF
  68.  
  69.  
  70. :List1
  71. :: Set variable value to bytes used by directory
  72. FOR /F "tokens=3,4*" %%B IN ('DIR /A /-C /S "%~1" ^| FIND /I "File(s)"') DO SET ListDir=%%~B
  73. :: Strip last 6 digits from value
  74. SET ListDir=%ListDir:~0,-6%
  75. IF NOT DEFINED ListDir SET ListDir=0
  76. :: Display resulting value in MB
  77. ECHO.%~1	%ListDir%
  78. :: Clear variable
  79. SET ListDir=
  80. GOTO:EOF
  81.  
  82.  
  83. :Syntax
  84. ECHO.
  85. ECHO DiskUse, Version 5.01 for Windows 2000 / XP
  86. ECHO Display disk space used by subdirectories (tab delimited)
  87. ECHO.
  88. ECHO Usage:  DISKUSE  [ startdir ]  [ /L ]
  89. ECHO.
  90. ECHO Where:  "startdir"  is the directory containing subdirectories to be
  91. ECHO                     inventoried (default is the current directory)
  92. ECHO         "/L"        is used for large numbers, over 2GB, to prevent return
  93. ECHO                     of negative numbers due to batch math limitations
  94. ECHO                     (integer division by 1000000 instead of properly
  95. ECHO                     rounded mathematical division by 1048576)
  96. ECHO.
  97. ECHO Written by Rob van der Woude
  98. ECHO http://www.robvanderwoude.com
  99.  
  100. :End
  101. IF "%OS%"=="Windows_NT" ENDLOCAL
  102.