Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @ECHO OFF
- REM Argument handling
- REM -----------------
- REM Checks for GUID indicating batch was re-launched
- IF '%1'=='55f47682-5b9a-4b50-80c1-e1b6c859ccdf' ( SHIFT & GOTO gotPrivileges )
- REM Checks for no arguments or more than one argument
- IF "%~1"=="" ( GOTO help )
- IF NOT "%~2"=="" ( GOTO help )
- IF "%1"=="-m" ( GOTO privilege )
- IF "%1"=="-u" ( GOTO privilege )
- IF "%1"=="-d" ( GOTO delete )
- GOTO help
- :privilege
- REM Checks & gets admin rights
- REM --------------------------
- CLS
- REM checks privileges
- NET FILE 1>NUL 2>NUL
- IF '%errorlevel%' == '0' ( GOTO gotPrivileges ) else ( GOTO getPrivileges )
- :getPrivileges
- REM Constructs a vbscript that launches this batch file prompting for elevated privilege status
- SETLOCAL DisableDelayedExpansion
- SET "batchPath=%~0"
- SETLOCAL EnableDelayedExpansion
- ECHO Set UAC = CreateObject^("Shell.Application"^) > %temp%\20cfcc7a-64a8-4f4d-a903-a08c6e0489ab
- ECHO UAC.ShellExecute "!batchPath! ", "55f47682-5b9a-4b50-80c1-e1b6c859ccdf %1", "", "runas", 1 >> %temp%\20cfcc7a-64a8-4f4d-a903-a08c6e0489ab
- REM Launches the vbscript and then deletes it for system hygiene
- CScript //E:vbscript "%temp%\20cfcc7a-64a8-4f4d-a903-a08c6e0489ab"
- DEL %temp%\20cfcc7a-64a8-4f4d-a903-a08c6e0489ab
- EXIT /B
- :gotPrivileges
- SETLOCAL & PUSHD .
- REM End of automatically check & get admin rights section
- REM Continue with the script requiring admin rights below this line
- REM ---------------------------------------------------------------
- CLS
- SETLOCAL EnableDelayedExpansion
- IF "%1"=="-m" ( GOTO mount )
- IF "%1"=="-u" ( GOTO unmount )
- REM Search for first unmounted drive and mounts the RAMDisk on it
- REM -------------------------------------------------------------
- :mount
- REM Checks if RAMDisk register file exists and creates it if needed
- SET strterm=_
- IF NOT EXIST %temp%\112198e3-9752-4549-92a2-b379635b44b0 (
- ECHO %strterm%>%temp%\112198e3-9752-4549-92a2-b379635b44b0
- )
- REM Reads 'wmic os get freephysicalmemory /format:csv' output and trims and stores the free memory available into "m" variable
- FOR /F "skip=2 tokens=2 delims=," %%p IN ('wmic os get freephysicalmemory /format:csv') DO (
- SET m=%%p
- )
- REM Displays current available system memory and prompts for the RAMDisk size
- FOR /F %%a IN ('Powershell %m% / 1024 / 1024') DO ( SET availablememory=%%a )
- FOR /F "tokens=1,2 delims=.," %%a IN ("%availablememory%") DO (
- SET integer=%%a
- SET fraction=%%b
- SET availablememory=!integer!.!fraction:~0,2!
- )
- :input
- ECHO The current available system memory is !availablememory!GB.
- ECHO.
- SET /P "input=Please enter the desired RAMDisk size in GB: "
- ECHO.
- REM Validates input is a valid number expression
- ECHO %input%|findstr /r /c:"^[0-9]*\.*[0-9]*$" >nul
- IF '%errorlevel%' == '1' (
- ECHO The input is not valid. The only valid inputs are positive integers
- ECHO or decimal numbers with a dot (.^) as decimal mark.
- ECHO.
- GOTO input
- )
- REM Validates input is not a zero number expression
- ECHO %input%|findstr /r /c:"^[0]*\.*[0]*$" >nul
- IF '%errorlevel%' == '0' (
- ECHO The input is not valid. The only valid inputs are positive integers
- ECHO or decimal numbers with a dot (.^) as decimal mark.
- ECHO.
- GOTO input
- )
- REM Calculates input memory size in MB and KB (uses Powershell to allow for non integer inputs)
- FOR /F %%a IN ('Powershell %input% * 1024') DO ( SET sizeMB=%%a )
- FOR /F %%a IN ('Powershell %input% * 1024 * 1024') DO ( SET sizeKB=%%a )
- REM Removes fractions
- FOR /F "tokens=1 delims=.," %%a IN ("%sizeMB%") DO ( SET /A sizeMB=%%a )
- FOR /F "tokens=1 delims=.," %%a IN ("%sizeKB%") DO ( SET /A sizeKB=%%a )
- REM Checks there is more than the required free memory
- IF %m% GTR %sizeKB% ( GOTO availabledrives ) ELSE ( GOTO nofreeram )
- :availabledrives
- REM Constructs a vbscript that when launched generates a file with a list of mapped drives in the system
- ECHO strComputer = "." > %temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0
- ECHO Set objWMIService = GetObject("winmgmts:\\" ^& strComputer ^& "\root\cimv2") >> %temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0
- ECHO Set objFSO = CreateObject("Scripting.FileSystemObject") >> %temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0
- ECHO Set objOutFile = objFSO.CreateTextFile("%temp%\ba9e1cc7-9302-469b-b16e-0114c2da0436") >> %temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0
- ECHO Set colDrives = objWMIService.ExecQuery _ >> %temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0
- ECHO ("Select * From Win32_LogicalDisk") >> %temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0
- ECHO For Each objDrive in colDrives >> %temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0
- ECHO objOutFile.WriteLine(objDrive.DeviceID) >> %temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0
- ECHO Next >> %temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0
- ECHO objOutFile.Close >> %temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0
- REM Launches the vbscript and then deletes it for system hygiene
- CScript //E:vbscript "%temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0"
- DEL %temp%\76e267da-34c1-4715-86e6-dc1ea847c6c0
- REM Reads vbscript's output file and trims and packs mounted drives into "mounteddrives" variable then deletes the output file for system hygiene
- SET "mounteddrives="
- FOR /F "tokens=1 delims=:" %%a IN ('type %temp%\ba9e1cc7-9302-469b-b16e-0114c2da0436') DO (
- SET var=%%a
- SET letter=!var:~0,1!
- SET "mounteddrives=!mounteddrives!!letter!"
- )
- DEL %temp%\ba9e1cc7-9302-469b-b16e-0114c2da0436
- REM Loop that finds first unmapped drive letter in the system (A and B omitted)
- SET strterm=_
- SET drives=CDEFGHIJKLMNOPQRSTUVWXYZ_
- :loop
- REM Gets first character from drives
- SET drive=%drives:~0,1%
- REM Removes first character from drives
- SET drives=%drives:~1%
- REM Exits loop on string terminator
- IF "%drive%" == "%strterm%" ( GOTO nofreedrive )
- REM Mounts RAMDisk in first unmapped drive letter
- IF "!mounteddrives:%drive%=!" EQU "!mounteddrives!" (
- GOTO mountdrive
- ) ELSE ( GOTO loop )
- :mountdrive
- REM Registers drive to ramdisk register file serving as a LIFO stack shared with the unmount batch file and mounts the RAMDisk
- SET /P "register="<%temp%\112198e3-9752-4549-92a2-b379635b44b0
- SET output=%register%%drive%
- ECHO %output%>%temp%\112198e3-9752-4549-92a2-b379635b44b0
- imdisk -a -s %sizeMB%M -m %drive%: -p "/fs:ntfs /q /y"
- GOTO:eof
- REM Unmounts most recently mounted RAMDisk registered in the RAMDisk register file
- REM ------------------------------------------------------------------------------
- :unmount
- REM Checks if RAMDisk register file exists
- IF NOT EXIST %temp%\112198e3-9752-4549-92a2-b379635b44b0 ( GOTO noregister )
- REM Reads RAMDisk register file and unmounts most recently RAMDisk registered in it.
- SET /P "register="<%temp%\112198e3-9752-4549-92a2-b379635b44b0
- SET strterm=_
- IF "%register%" == "%strterm%" ( GOTO noramdisk )
- SET rest=%register:~1,-1%
- SET drive=%register:~-1%
- SET output=%strterm%%rest%
- IF "%output%" == "%strterm%" (
- DEL %temp%\112198e3-9752-4549-92a2-b379635b44b0
- ) ELSE (
- ECHO %output%>%temp%\112198e3-9752-4549-92a2-b379635b44b0
- )
- imdisk -D -m %drive%:
- GOTO:eof
- REM Deletes RAMDisk register file
- REM -----------------------------
- :delete
- REM Checks if RAMDisk register file exists and deletes it if so
- IF NOT EXIST %temp%\112198e3-9752-4549-92a2-b379635b44b0 ( GOTO:eof )
- DEL %temp%\112198e3-9752-4549-92a2-b379635b44b0
- GOTO:eof
- REM Error exits
- REM -----------
- :nofreedrive
- ECHO Unable to mount RAMDisk due to the following reason:
- ECHO There aren't any free drive letters.
- TIMEOUT /T -1
- GOTO:eof
- :nofreeram
- ECHO Unable to mount RAMDisk due to the following reason:
- ECHO No enough free physical memory available.
- TIMEOUT /T -1
- GOTO:eof
- :noramdisk
- ECHO Unable to unmount RAMDisk due to the following reason:
- ECHO The RAMDisk register file is empty.
- TIMEOUT /T -1
- GOTO:eof
- :noregister
- ECHO Unable to unmount RAMDisk due to the following reason:
- ECHO The RAMDisk register file is missing.
- TIMEOUT /T -1
- GOTO:eof
- :help
- ECHO A ImDisk wrapper batch to mount/unmount ad-hoc NTFS formatted RAMDisks in the
- ECHO first available drive letter.
- ECHO The program automatically detects privilege status and prompts user to elevate
- ECHO it if needed. It also prompts for desired RAMDisk size in GB units (fraction
- ECHO inputs are allowed. For example type "0.5" when prompted for size to create
- ECHO a 500MB RAMDisk).
- ECHO.
- ECHO Syntax:
- ECHO RAMDISK [-m ^| -u ^| -d] /?
- ECHO.
- ECHO -m Mounts a NTFS formatted RAMDisk on the first free drive letter.
- ECHO -u Unmounts last registered RAMDisk in the RAMDisk register file.
- ECHO -d Deletes the RAMDisk register file.
- ECHO -? Displays this help message.
- ECHO.
- ECHO The RAMDisk register file is stored on TEMP and works as a LIFO stack. Each
- ECHO time the program is invoked with the -m switch a RAMDisk is created, mounted
- ECHO and its drive letter is registered in the RAMDisk register file. Invoking the
- ECHO program with the -u switch initiates a search for the last entry in the
- ECHO RAMDisk register file and unmounts the RAMDisk corresponding to it and then
- ECHO updates the RAMDisk register file accordingly.
- ECHO.
- ECHO The -d switch is provided because failure to properly unmount with the -u
- ECHO switch mounted RAMDisks that were created with the -m switch will result in a
- ECHO RAMDisk register file in an unsynchronized state. This most often occurs when
- ECHO an user shutdowns the system before unmounting all ad-hoc RAMDisks (a frequent
- ECHO occurrence due to the non persistent nature of such RAMDisks) or in the case of
- ECHO unexpected system shutdown or in the case a mounted RAMDisk that was created
- ECHO with the -m switch is unmounted via other methods (such as the IMDisk Toolkit's
- ECHO RamDisk Configuration Tool).
- ECHO.
- ECHO As such it is recommended to include an entry in the system Task Scheduler to
- ECHO run the program with the -d switch on system startup if TEMP resides in a
- ECHO persistent location.
- ECHO.
- ECHO The recommended usage is to create two shortcuts. One invoking "RAMdisk -m" and
- ECHO the other invoking "RAMdisk -u". Thus every time the first shortcut is launched
- ECHO a new ad-hoc RAMDisk is created (after prompting user for privilege status and
- ECHO size) and every time the second shortcut is launched the last created ad-hoc
- ECHO RAMDisk is destroyed (again after prompting user for privilege status).
- ECHO.
- TIMEOUT /T -1
- GOTO:eof
- REM Credits
- REM
- REM Automatically check & get admin rights
- REM http://stackoverflow.com/a/12264592
- REM
- REM List of mounted drives (including network shares)
- REM http://superuser.com/a/135755
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement