Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for diskspc.bat

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

  1. @ECHO OFF
  2. :: Check Windows version
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4.  
  5. :: Use variables locally
  6. SETLOCAL
  7.  
  8. :: Check if FINDSTR and WMIC are available
  9. FINDSTR.EXE /? >NUL 2>&1
  10. IF ERRORLEVEL 1 (
  11. 	ECHO ERROR: FINDSTR.EXE not found.
  12. 	ECHO.
  13. 	GOTO Syntax
  14. )
  15. WMIC.EXE    /? >NUL 2>&1
  16. IF ERRORLEVEL 1 (
  17. 	ECHO ERROR: WMIC.EXE not found.
  18. 	ECHO.
  19. 	GOTO Syntax
  20. )
  21.  
  22. :: Set default values
  23. SET StartDir=%~dp0
  24. SET Error=0
  25. SET Format=TBL
  26. SET Header=1
  27. SET List=0
  28. SET Node=%ComputerName%
  29. SET ServerList=
  30.  
  31. :: Check command line arguments
  32. IF NOT "%~4"=="" (
  33. 	ECHO.ERROR: too many command line arguments: %*
  34. 	ECHO.
  35. 	GOTO Syntax
  36. )
  37. :: The command FOR %%A IN (/?) ... doesn't work, so we need a separate check on "?"
  38. ECHO.%* | FIND.EXE "?" >NUL && GOTO Syntax
  39. IF NOT "%~1"=="" (FOR %%A IN (%*) DO CALL :Parse %%A)
  40.  
  41. IF NOT "%Debug%"=="" (
  42. 	SET StartDir
  43. 	SET Error
  44. 	SET Format
  45. 	SET Header
  46. 	SET List
  47. 	SET Node
  48. 	SET ServerList
  49. )
  50.  
  51. :: Check validity of node name or existence of server list file
  52. IF NOT "%ServerList%"=="" (CALL SET ServerList=%ServerList:^"=%)
  53. IF "%ServerList%"=="" (
  54. 	ECHO.%Node% | FINDSTR.EXE /R /C:"[/?*,;:/\\&<>+]" >NUL
  55. 	IF NOT ERRORLEVEL 1 (
  56. 		ECHO.ERROR: invalid characters in computer name: %*
  57. 		ECHO.
  58. 		SET Error=1
  59. 	) ELSE (
  60. 		PING %Node% -n 2 2>NUL | FIND "TTL=" >NUL
  61. 		IF ERRORLEVEL 1 (
  62. 			ECHO.ERROR: computer %Node% cannot be reached; check the
  63. 			ECHO.       computer name and/or if the computer is on-line
  64. 			SET Error=1
  65. 		)
  66. 	)
  67. ) ELSE (
  68. 	IF NOT EXIST "%ServerList%" (
  69. 		ECHO.ERROR: file "%ServerList%" not found.
  70. 		ECHO.
  71. 		SET Error=1
  72. 	)
  73. )
  74. IF "%List%"=="1" SET Node="@%ServerList%"
  75.  
  76. :: If any error occurred, show help text
  77. IF "%Error%"=="1" GOTO Syntax
  78.  
  79. :: Query the specified computer(s)
  80. IF "%Header%"=="0" (
  81. 	IF "%Format%"=="CSV" WMIC.EXE /Node:%Node% LogicalDisk Where DriveType="3" Get DeviceID,FileSystem,FreeSpace,Size            /Format:csv   | MORE /E +2 | FINDSTR /R /C:"[ ,]"
  82. 	IF "%Format%"=="TBL" WMIC.EXE /Node:%Node% LogicalDisk Where DriveType="3" Get DeviceID,FileSystem,FreeSpace,Size,SystemName /Format:table | MORE /E +1 | FINDSTR /R /C:"[ ,]"
  83. ) ELSE (
  84. 	IF "%Format%"=="CSV" WMIC.EXE /Node:%Node% LogicalDisk Where DriveType="3" Get DeviceID,FileSystem,FreeSpace,Size            /Format:csv   | FINDSTR /R /C:"[ ,]"
  85. 	IF "%Format%"=="TBL" WMIC.EXE /Node:%Node% LogicalDisk Where DriveType="3" Get DeviceID,FileSystem,FreeSpace,Size,SystemName /Format:table | FINDSTR /R /C:"[ ,]"
  86. )
  87. IF "%Format%"=="HTL" WMIC.EXE /Node:%Node% LogicalDisk Where DriveType="3" Get DeviceID,FileSystem,FreeSpace,Size,SystemName /Format:hform
  88. IF "%Format%"=="HTT" WMIC.EXE /Node:%Node% LogicalDisk Where DriveType="3" Get DeviceID,FileSystem,FreeSpace,Size,SystemName /Format:htable
  89. IF "%Format%"=="LST" WMIC.EXE /Node:%Node% LogicalDisk Where DriveType="3" Get DeviceID,FileSystem,FreeSpace,Size,SystemName /Format:list
  90.  
  91. :: Done
  92. ENDLOCAL
  93. GOTO:EOF
  94.  
  95.  
  96.  
  97.  
  98. :Parse
  99. SET Var=%~1
  100. IF NOT "%Var:~0,1%"=="/" (
  101. 	SET Node=%1
  102. 	GOTO:EOF
  103. )
  104. IF /I NOT "%Var%"=="/C" IF /I NOT "%Var%"=="/HL" IF /I NOT "%Var%"=="/HT" IF /I NOT "%Var%"=="/L" IF /I NOT "%Var%"=="/NH" IF /I NOT "%Var%"=="/T" IF /I NOT "%Var:~0,3%"=="/SL" (
  105. 	ECHO.ERROR: invalid command line switch: %1
  106. 	ECHO.
  107. 	SET Error=1
  108. )
  109. IF /I "%Var%"=="/C"  SET Format=CSV
  110. IF /I "%Var%"=="/HL" SET Format=HTL
  111. IF /I "%Var%"=="/HT" SET Format=HTT
  112. IF /I "%Var%"=="/L"  SET Format=LST
  113. IF /I "%Var%"=="/NH" SET Header=0
  114. IF /I "%Var%"=="/T"  SET Format=TBL
  115. IF /I "%Var:~0,3%"=="/SL" SET List=1
  116. IF NOT "%List%"=="1" GOTO:EOF
  117. IF EXIST "%StartDir%servers.txt"  SET ServerList=%StartDir%servers.txt
  118. IF EXIST servers.txt              SET ServerList=servers.txt
  119. IF NOT "%Var:~3,1%"==":" IF NOT "%Var:~3,1%"=="" (
  120. 	ECHO.ERROR: invalid command line switch: %1
  121. 	ECHO.
  122. 	SET Error=1
  123. 	GOTO:EOF
  124. )
  125. IF "%Var:~3,1%"==":" SET ServerList=%Var:~4%
  126. GOTO:EOF
  127.  
  128.  
  129. :Syntax
  130. ECHO.
  131. ECHO DiskSpc.bat, Version 3.10 for Windows XP Professionaland later
  132. ECHO Display disk usage for a (WMI enabled) computer or for a list of computers
  133. ECHO.
  134. ECHO Usage:  DISKSPC     [computer^|/SL[:listfile]]  [/C^|/HL^|/HT^|/L^|/T]  [/NH]
  135. ECHO.
  136. ECHO Where:  "computer"  is the computer to be checked (default is local system)
  137. ECHO         /SL         use list of computers from text file
  138. ECHO         "listfile"  is the name of a text file with one computer name per line
  139. ECHO                     (default: "servers.txt" in current dir or batch's own dir)
  140. ECHO         /C          output format CSV
  141. ECHO         /HL         output format HTML List
  142. ECHO         /HT         output format HTML Table
  143. ECHO         /L          output format List
  144. ECHO         /T          output format Table
  145. ECHO         /NH         No Header line in CSV or Table output
  146. ECHO.
  147. ECHO Written by Rob van der Woude
  148. ECHO http://www.robvanderwoude.com
  149. IF "%OS%"=="Windows_NT" ENDLOCAL
  150. EXIT /B 1
  151.  

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