Advertisement
Guest User

ad-hoc NTFS formatted RAMDisks batch

a guest
Oct 13th, 2013
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 10.53 KB | None | 0 0
  1. @ECHO OFF
  2.  
  3. REM Argument handling
  4. REM -----------------
  5.  
  6. REM Checks for GUID indicating batch was re-launched
  7. IF '%1'=='55f47682-5b9a-4b50-80c1-e1b6c859ccdf' ( SHIFT & GOTO gotPrivileges )
  8.  
  9. REM Checks for no arguments or more than one argument
  10. IF "%~1"=="" ( GOTO help )
  11. IF NOT "%~2"=="" ( GOTO help )
  12.  
  13. IF "%1"=="-m" ( GOTO privilege )
  14. IF "%1"=="-u" ( GOTO privilege )
  15. IF "%1"=="-d" ( GOTO delete )
  16. GOTO help
  17.  
  18. :privilege
  19. REM Checks & gets admin rights
  20. REM --------------------------
  21.  
  22. CLS
  23.  
  24. REM checks privileges
  25. NET FILE 1>NUL 2>NUL
  26. IF '%errorlevel%' == '0' ( GOTO gotPrivileges ) else ( GOTO getPrivileges )
  27.  
  28. :getPrivileges
  29. REM Constructs a vbscript that launches this batch file prompting for elevated privilege status
  30. SETLOCAL DisableDelayedExpansion
  31. SET "batchPath=%~0"
  32. SETLOCAL EnableDelayedExpansion
  33. ECHO Set UAC = CreateObject^("Shell.Application"^) > %temp%\20cfcc7a-64a8-4f4d-a903-a08c6e0489ab
  34. ECHO UAC.ShellExecute "!batchPath! ", "55f47682-5b9a-4b50-80c1-e1b6c859ccdf %1", "", "runas", 1 >> %temp%\20cfcc7a-64a8-4f4d-a903-a08c6e0489ab
  35. REM Launches the vbscript and then deletes it for system hygiene
  36. CScript //E:vbscript "%temp%\20cfcc7a-64a8-4f4d-a903-a08c6e0489ab"
  37. DEL %temp%\20cfcc7a-64a8-4f4d-a903-a08c6e0489ab
  38.  
  39. EXIT /B
  40.  
  41. :gotPrivileges
  42. SETLOCAL & PUSHD .
  43.  
  44. REM End of automatically check & get admin rights section
  45. REM Continue with the script requiring admin rights below this line
  46. REM ---------------------------------------------------------------
  47.  
  48. CLS
  49.  
  50. SETLOCAL EnableDelayedExpansion
  51.  
  52. IF "%1"=="-m" ( GOTO mount )
  53. IF "%1"=="-u" ( GOTO unmount )
  54.  
  55. REM Search for first unmounted drive and mounts the RAMDisk on it
  56. REM -------------------------------------------------------------
  57. :mount
  58.  
  59. REM Checks if RAMDisk register file exists and creates it if needed
  60. SET strterm=_
  61. IF NOT EXIST %temp%\112198e3-9752-4549-92a2-b379635b44b0 (
  62.     ECHO %strterm%>%temp%\112198e3-9752-4549-92a2-b379635b44b0
  63. )
  64.  
  65. REM Reads 'wmic os get freephysicalmemory /format:csv' output and trims and stores the free memory available into "m" variable
  66. FOR /F "skip=2 tokens=2 delims=," %%p IN ('wmic os get freephysicalmemory /format:csv') DO (
  67.   SET m=%%p
  68. )
  69.  
  70. REM Displays current available system memory and prompts for the RAMDisk size
  71. FOR /F %%a IN ('Powershell %m% / 1024 / 1024') DO ( SET availablememory=%%a )
  72. FOR /F "tokens=1,2 delims=.," %%a IN ("%availablememory%") DO (
  73.      SET integer=%%a
  74.      SET fraction=%%b
  75.      SET availablememory=!integer!.!fraction:~0,2!
  76. )
  77.  
  78. :input
  79. ECHO The current available system memory is !availablememory!GB.
  80. ECHO.
  81. SET /P "input=Please enter the desired RAMDisk size in GB: "
  82. ECHO.
  83.  
  84. REM Validates input is a valid number expression
  85. ECHO %input%|findstr /r /c:"^[0-9]*\.*[0-9]*$" >nul
  86. IF '%errorlevel%' == '1' (
  87.     ECHO The input is not valid. The only valid inputs are positive integers
  88.     ECHO or decimal numbers with a dot (.^) as decimal mark.
  89.     ECHO.
  90.     GOTO input
  91. )
  92.  
  93. REM Validates input is not a zero number expression
  94. ECHO %input%|findstr /r /c:"^[0]*\.*[0]*$" >nul
  95. IF '%errorlevel%' == '0' (
  96.     ECHO The input is not valid. The only valid inputs are positive integers
  97.     ECHO or decimal numbers with a dot (.^) as decimal mark.
  98.     ECHO.
  99.     GOTO input
  100. )
  101.  
  102. REM Calculates input memory size in MB and KB (uses Powershell to allow for non integer inputs)
  103. FOR /F %%a IN ('Powershell %input% * 1024') DO ( SET sizeMB=%%a )
  104. FOR /F %%a IN ('Powershell %input% * 1024 * 1024') DO ( SET sizeKB=%%a )
  105. REM Removes fractions
  106. FOR /F "tokens=1 delims=.," %%a IN ("%sizeMB%") DO ( SET /A sizeMB=%%a )
  107. FOR /F "tokens=1 delims=.," %%a IN ("%sizeKB%") DO ( SET /A sizeKB=%%a )
  108.  
  109. REM Checks there is more than the required free memory
  110. IF %m% GTR %sizeKB% ( GOTO availabledrives ) ELSE ( GOTO nofreeram )
  111.  
  112. :availabledrives
  113. REM Constructs a vbscript that when launched generates a file with a list of mapped drives in the system
  114. ECHO strComputer = "." > %temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0
  115. ECHO Set objWMIService = GetObject("winmgmts:\\" ^& strComputer ^& "\root\cimv2") >> %temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0
  116. ECHO Set objFSO = CreateObject("Scripting.FileSystemObject") >> %temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0
  117. ECHO Set objOutFile = objFSO.CreateTextFile("%temp%\ba9e1cc7-9302-469b-b16e-0114c2da0436") >> %temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0
  118. ECHO Set colDrives = objWMIService.ExecQuery _ >> %temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0
  119. ECHO     ("Select * From Win32_LogicalDisk") >> %temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0
  120. ECHO For Each objDrive in colDrives >> %temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0
  121. ECHO     objOutFile.WriteLine(objDrive.DeviceID) >> %temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0
  122. ECHO Next >> %temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0
  123. ECHO objOutFile.Close >> %temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0
  124. REM Launches the vbscript and then deletes it for system hygiene
  125. CScript //E:vbscript "%temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0"
  126. DEL %temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0
  127.  
  128. REM Reads vbscript's output file and trims and packs mounted drives into "mounteddrives" variable then deletes the output file for system hygiene
  129. SET "mounteddrives="
  130. FOR /F "tokens=1 delims=:" %%a IN ('type %temp%\ba9e1cc7-9302-469b-b16e-0114c2da0436') DO (
  131.     SET var=%%a
  132.     SET letter=!var:~0,1!
  133.     SET "mounteddrives=!mounteddrives!!letter!"
  134. )
  135. DEL %temp%\ba9e1cc7-9302-469b-b16e-0114c2da0436
  136.  
  137. REM Loop that finds first unmapped drive letter in the system (A and B omitted)
  138. SET strterm=_
  139. SET drives=CDEFGHIJKLMNOPQRSTUVWXYZ_
  140.  
  141. :loop
  142. REM Gets first character from drives
  143. SET drive=%drives:~0,1%
  144. REM Removes first character from drives
  145. SET drives=%drives:~1%
  146. REM Exits loop on string terminator
  147. IF "%drive%" == "%strterm%" ( GOTO nofreedrive )
  148. REM Mounts RAMDisk in first unmapped drive letter
  149. IF "!mounteddrives:%drive%=!" EQU "!mounteddrives!" (
  150.     GOTO mountdrive
  151. ) ELSE ( GOTO loop )
  152.  
  153. :mountdrive
  154. REM Registers drive to ramdisk register file serving as a LIFO stack shared with the unmount batch file and mounts the RAMDisk
  155. SET /P "register="<%temp%\112198e3-9752-4549-92a2-b379635b44b0
  156. SET output=%register%%drive%
  157. ECHO %output%>%temp%\112198e3-9752-4549-92a2-b379635b44b0
  158.  
  159. imdisk -a -s %sizeMB%M -m %drive%: -p "/fs:ntfs /q /y"
  160. GOTO:eof
  161.  
  162.  
  163. REM Unmounts most recently mounted RAMDisk registered in the RAMDisk register file
  164. REM ------------------------------------------------------------------------------
  165. :unmount
  166.  
  167. REM Checks if RAMDisk register file exists
  168. IF NOT EXIST %temp%\112198e3-9752-4549-92a2-b379635b44b0 ( GOTO noregister )
  169.  
  170. REM Reads RAMDisk register file and unmounts most recently RAMDisk registered in it.
  171. SET /P "register="<%temp%\112198e3-9752-4549-92a2-b379635b44b0
  172. SET strterm=_
  173.  
  174. IF "%register%" == "%strterm%" ( GOTO noramdisk )
  175.  
  176. SET rest=%register:~1,-1%
  177. SET drive=%register:~-1%
  178. SET output=%strterm%%rest%
  179.  
  180. IF "%output%" == "%strterm%" (
  181.     DEL %temp%\112198e3-9752-4549-92a2-b379635b44b0
  182. ) ELSE (
  183.     ECHO %output%>%temp%\112198e3-9752-4549-92a2-b379635b44b0
  184. )
  185.  
  186. imdisk -D -m %drive%:
  187. GOTO:eof
  188.  
  189.  
  190. REM Deletes RAMDisk register file
  191. REM -----------------------------
  192. :delete
  193.  
  194. REM Checks if RAMDisk register file exists and deletes it if so
  195. IF NOT EXIST %temp%\112198e3-9752-4549-92a2-b379635b44b0 ( GOTO:eof )
  196.  
  197. DEL %temp%\112198e3-9752-4549-92a2-b379635b44b0
  198. GOTO:eof
  199.  
  200.  
  201. REM Error exits
  202. REM -----------
  203.  
  204. :nofreedrive
  205. ECHO Unable to mount RAMDisk due to the following reason:
  206. ECHO There aren't any free drive letters.
  207. TIMEOUT /T -1
  208. GOTO:eof
  209.  
  210. :nofreeram
  211. ECHO Unable to mount RAMDisk due to the following reason:
  212. ECHO No enough free physical memory available.
  213. TIMEOUT /T -1
  214. GOTO:eof
  215.  
  216. :noramdisk
  217. ECHO Unable to unmount RAMDisk due to the following reason:
  218. ECHO The RAMDisk register file is empty.
  219. TIMEOUT /T -1
  220. GOTO:eof
  221.  
  222. :noregister
  223. ECHO Unable to unmount RAMDisk due to the following reason:
  224. ECHO The RAMDisk register file is missing.
  225. TIMEOUT /T -1
  226. GOTO:eof
  227.  
  228. :help
  229. ECHO A ImDisk wrapper batch to mount/unmount ad-hoc NTFS formatted RAMDisks in the
  230. ECHO first available drive letter.
  231. ECHO The program automatically detects privilege status and prompts user to elevate
  232. ECHO it if needed. It also prompts for desired RAMDisk size in GB units (fraction
  233. ECHO inputs are allowed. For example type "0.5" when prompted for size to create
  234. ECHO a 500MB RAMDisk).
  235. ECHO.
  236. ECHO Syntax:
  237. ECHO     RAMDISK [-m ^| -u ^| -d] /?
  238. ECHO.
  239. ECHO        -m    Mounts a NTFS formatted RAMDisk on the first free drive letter.
  240. ECHO        -u    Unmounts last registered RAMDisk in the RAMDisk register file.
  241. ECHO        -d    Deletes the RAMDisk register file.
  242. ECHO        -?    Displays this help message.
  243. ECHO.
  244. ECHO The RAMDisk register file is stored on TEMP and works as a LIFO stack. Each
  245. ECHO time the program is invoked with the -m switch a RAMDisk is created, mounted
  246. ECHO and its drive letter is registered in the RAMDisk register file. Invoking the
  247. ECHO program with the -u switch initiates a search for the last entry in the
  248. ECHO RAMDisk register file and unmounts the RAMDisk corresponding to it and then
  249. ECHO updates the RAMDisk register file accordingly.
  250. ECHO.
  251. ECHO The -d switch is provided because failure to properly unmount with the -u
  252. ECHO switch mounted RAMDisks that were created with the -m switch will result in a
  253. ECHO RAMDisk register file in an unsynchronized state. This most often occurs when
  254. ECHO an user shutdowns the system before unmounting all ad-hoc RAMDisks (a frequent
  255. ECHO occurrence due to the non persistent nature of such RAMDisks) or in the case of
  256. ECHO unexpected system shutdown or in the case a mounted RAMDisk that was created
  257. ECHO with the -m switch is unmounted via other methods (such as the IMDisk Toolkit's
  258. ECHO RamDisk Configuration Tool).
  259. ECHO.
  260. ECHO As such it is recommended to include an entry in the system Task Scheduler to
  261. ECHO run the program with the -d switch on system startup if TEMP resides in a
  262. ECHO persistent location.
  263. ECHO.
  264. ECHO The recommended usage is to create two shortcuts. One invoking "RAMdisk -m" and
  265. ECHO the other invoking "RAMdisk -u". Thus every time the first shortcut is launched
  266. ECHO a new ad-hoc RAMDisk is created (after prompting user for privilege status and
  267. ECHO size) and every time the second shortcut is launched the last created ad-hoc
  268. ECHO RAMDisk is destroyed (again after prompting user for privilege status).
  269. ECHO.
  270. TIMEOUT /T -1
  271. GOTO:eof
  272.  
  273. REM Credits
  274. REM
  275. REM Automatically check & get admin rights
  276. REM http://stackoverflow.com/a/12264592
  277. REM
  278. REM List of mounted drives (including network shares)
  279. REM http://superuser.com/a/135755
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement