Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for getram.bat

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

  1. @ECHO OFF
  2. :: Enable command extensions and save initial environment
  3. VERIFY OTHER 2>NUL
  4. SETLOCAL ENABLEEXTENSIONS
  5. IF ERRORLEVEL 1 (
  6. 	ECHO Unable to enable command extensions
  7. 	GOTO End
  8. )
  9.  
  10. :: Check parameters
  11. SET PC=%ComputerName%
  12. ECHO.%1 | FIND "\\" >NUL
  13. IF NOT ERRORLEVEL 1 (
  14. 	FOR /F "tokens=* delims=\" %%A IN ('ECHO.%~1') DO SET PC=%%A
  15. 	SHIFT
  16. )
  17. IF /I [%1]==[]   SET SWITCH=/V
  18. IF /I [%1]==[/Q] SET SWITCH=/S
  19. IF /I [%1]==[/S] SET SWITCH=/S
  20. IF /I [%1]==[/V] SET SWITCH=/V
  21. IF [%SWITCH%]==[] GOTO Syntax
  22.  
  23. :: Check Windows version
  24. VER | FIND "Windows NT" >NUL
  25. IF NOT ERRORLEVEL 1 GOTO NT4
  26. VER | FIND "Windows 2000" >NUL
  27. IF NOT ERRORLEVEL 1 GOTO W2K
  28. GOTO Syntax
  29.  
  30. :NT4
  31. :: Check if remote computer was specified by accident
  32. IF DEFINED PC GOTO Syntax
  33. :: Use WINMSD.EXE to check the amount of RAM installed
  34. START /W WINMSD.EXE /S
  35. :: Read the amount of RAM installed from WINMSD's report
  36. FOR /F "tokens=2 delims=: " %%A IN ('TYPE %COMPUTERNAME%.TXT ^| FIND "Total:" ^| FIND /V "\"') DO SET RAM=%%A
  37. :: Delete WINMSD's report
  38. DEL %COMPUTERNAME%.TXT
  39. :: Add 1023 to round up, or
  40. :: add 512 for "mathematical" rounding, or
  41. :: add 0 (or "rem out" next line) to round down
  42. SET /A RAM = %RAM:,=% + 1023
  43. SET /A RAM = %RAM:,=% / 1024
  44. :: Use switch to determine display format
  45. IF [%SWITCH%]==[/S] (
  46. 	ECHO %RAM%
  47. ) ELSE (
  48. 	ECHO.
  49. 	ECHO Total amount of RAM installed: %RAM% MB
  50. )
  51. GOTO End
  52.  
  53. :W2K
  54. :: Delete old temporary file if it exists
  55. IF EXIST "%Temp%.\%PC%.txt" DEL "%Temp%.\%PC%.txt"
  56. :: Use WINMSD.EXE and MSINFO32.EXE to check the amount of RAM installed
  57. START /W WINMSD.EXE /computer %PC% /categories +SystemSummary /report "%Temp%.\%PC%.txt"
  58. :: Wait for the temporary report file to be generated -- though START /W did
  59. :: wait for WINMSD.EXE to complete, WINMSD.EXE only started MSINFO32.EXE and
  60. :: then terminated without waiting for MSINFO32.EXE to finish
  61. :Wait
  62. IF NOT EXIST "%Temp%.\%PC%.txt" (
  63. 	PING 1.1.1.1 -n 1 -w 5000 >NUL
  64. 	GOTO :Wait
  65. )
  66. :: Parse the relevant data from the temporary report file
  67. :: Delims is a TAB followed by a SPACE
  68. FOR /F "tokens=4,5 delims=	 " %%A IN ('TYPE "%Temp%.\%PC%.txt" ^| FIND /I "Total Physical memory"') DO (
  69. 	SET RAM=%%A
  70. 	SET Multiplier=%%B
  71. )
  72. :: Wait and retry if the file existed but was still being written
  73. IF NOT DEFINED RAM GOTO :Wait
  74. :: Parse the result and convert to MB
  75. SET RAM=%RAM:,=%
  76. SET RAM=%RAM:.=%
  77. SET Div=1048576
  78. SET Mup=1
  79. SET Rnd=1048575
  80. IF "%Multiplier%"=="KB" (
  81. 	SET Div=1024
  82. 	SET Rnd=1023
  83. )
  84. IF "%Multiplier%"=="MB" (
  85. 	SET Mup=1
  86. 	SET Div=1
  87. 	SET Rnd=
  88. )
  89. IF "%Multiplier%"=="GB" (
  90. 	SET Mup=1024
  91. 	SET Div=1
  92. 	SET Rnd=
  93. )
  94. SET /A RAM = %RAM% + %Rnd%
  95. SET /A RAM = %RAM% * %Mup% / %Div%
  96. :: Use switch to determine display format
  97. IF [%SWITCH%]==[/S] (
  98. 	ECHO %RAM%
  99. ) ELSE (
  100. 	ECHO.
  101. 	ECHO Total amount of RAM installed on \\%PC%: %RAM% MB
  102. )
  103. GOTO End
  104.  
  105. :Syntax
  106. ECHO.
  107. ECHO GetRAM.bat, Version 2.00 for Windows NT 4 / 2000
  108. ECHO Displays the amount of RAM in MB installed on this PC ^(or a remote PC, in
  109. ECHO Windows 2000 only!^)
  110. ECHO.
  111. ECHO Idea: Mike Castle
  112. ECHO Posted on alt.msdos.batch.nt, April 26, 2001
  113. ECHO Extended by Rob van der Woude
  114. ECHO http://www.robvanderwoude.com
  115. ECHO.
  116. ECHO Usage:  %~n0  [ \\remote_pc ]  [ /Q ^| /S ^| /V ]
  117. ECHO.
  118. ECHO Switches:  \\remote_pc           remote computer name ^(Windows 2000 only!^)
  119. ECHO            /Q          ^(quiet^)   display the amount of RAM installed on the
  120. ECHO                                  computer as a number only
  121. ECHO            /S          ^(silent^)  same as /Q
  122. ECHO            /V          ^(verbose^) also tells what it is displaying ^(default^)
  123. ECHO.
  124. ECHO This batch file uses NT's native WINMSD.EXE to check the amount of RAM
  125. ECHO installed.
  126. ECHO If you find it too slow and you have a copy of the Windows NT 4 Resource
  127. ECHO Kit you can use MEMORY.BAT instead.
  128. ECHO MEMORY.BAT uses PSTAT.EXE to check the amount of RAM installed, which is a
  129. ECHO lot faster.
  130. ECHO MEMORY.BAT is available at http://www.robvanderwoude.com too.
  131.  
  132. :End
  133. ENDLOCAL & SET RAM=%RAM%
  134.  

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