[Note: 我們另外為此文章撰寫了中文版本,請看這篇。]

Background

Since I wrote the post of Fixing the drive letter for RAMDisk without saving and loading drive image – A solution for Dataram RAMDisk, I received more response than I expected. From all the response, I find it really hard to explain without real sample scripts. So now I’m try to solve it by showing a sample script.

This post is supposed to give supplementary information about the original post. If you want to know more about the idea and technical details, you’re highly recommended to read the original post.

In this post I provides two sample scripts, one for the PC startup and one for shut down. I use my own PC’s setting to demonstrate the idea. So in order to run the script in YOUR PC, you may either change your PC’s setting to fit mine or change the script to fit your PC.

Here is some important assumptions / PC settings that you may need to know before we start:

  1. It assumes the disk label for the Dataram RAMDisk is “RAMDISK” (case-sensitive)
  2. It fixes the RAMDisk drive letter as R: .
  3. It loads the files stored in C:\MyRAMDiskFiles to the R: automatically. These files are the programs that I want to run at RAMDisk for better performance. For example, I put my Firefox at C:\MyRAMDiskFiles\Programs\Firefox\Firefox.exe,  So after the script, I can always launch firefox in R:\Programs\Firefox\Firefox.exe
  4. I also have Google Chrome installed in C:\Program Files. However, Chrome will always stores its cache files in the location where it installed. This script will mount the cache folder in the Chrome’s installation path to the RAMDisk drive, i.e.  R:\TEMP\BrowserCache . This can reduce the harddisk I/O frequency, and after the PC shutdown, the cache files will be erased automatically.
  5. Since I may change the program settings over time (e.g. updating the firefox extensions), I want these changes being maintained every time I starts the PC. So I want SOME files in the RAMDisk drive be saved to harddisk when I shutdown the PC.
  6. Also, all my downloaded files are stored in RAMDisk (R:\Download), but I don’t want to lose these files after reboot. So this script will also copy the files in R:\Download to C:\Download during PC shutting down.

Let’s start

Step 1

Create the two scripts: StartUp.bat and ShutDown.bat, and store them in C:\ (or whatever you like, as you will specify the files in latter steps)

Note: Line-break has important meaning in DOS Batch script, so if you copy & paste the script from this post, you must check if there is any extra line-breaks added by the text editor. Instead, you may consider download the zipped scripts : RAMDisk fix drive letter - sample scripts (Download is recommended.)

Script 1: C:\StartUp.bat

@echo off

@rem assign the key variables, see assumption #1 & #2
SET _label=RAMDISK
SET _ramdrive=R:

@rem Find the drive letter which has volume name set as RAMDISK, and mount it to _ramdrive
FOR /F "skip=1 tokens=1 delims=: " %%a IN ('wmic logicaldisk where "VOLUMENAME='%_label%'" GET CAPTION') DO (
	subst %_ramdrive% %%a:\
)

@rem initialize the ramdisk - automatically copy the files in C:\MyRamDriveFiles to the ramdisk after the Windows starts. see assumption #3
mkdir %_ramdrive%\Programs
xcopy C:\MyRamDriveFiles\*.* %_ramdrive%\Programs\ /E/Q/Y/R

@rem link the google chrome cache folder to ramdisk. Remeber to replace [Your-Windows-user-name] with your own, see assumption #4
mkdir %_ramdrive%\TEMP\BrowserCache
mklink /j "C:\Users\[Your-Windows-user-name]\AppData\Local\Google\Chrome\User Data\Default\Cache" "%_ramdrive%\TEMP\BrowserCache"

@rem create a dir in ramdisk to stores the downloaded files, you will need to set up Firefox to store downloaded files in R:\Download, see assumption #6
mkdir %_ramdrive%\Download

@echo on

Script 2: C:\ShutDown.bat

@echo off

@rem see assumption #2
SET _ramdrive=R:

@rem copy all program files back to harddisk -  see assumption #5
xcopy %_ramdrive%\Programs\*.* C:\MyRamDriveFiles\ /Q/Y/R/S/D/C

@rem copy all downloaded files back to harddisk  -  see assumption #6
mkdir C:\Download
xcopy %RAMDISK%\Download\*.* C:\Download\ /S/D/Q/R/C

rem disconnect the google chrome cache folder linking -  see assumption #4
rmdir "C:\Users[Your-Windows-user-name]\AppData\Local\Google\Chrome\User Data\Default\Cache"

rem delete the drive mounting
subst %RAMDISK% /D

echo on

Step 2

Go read the original post and do step 3 in that post.

Step 3

Now you have added the StartUp.bat script to the “Group Policy Editor”, so go back to step 3.2 in the original post, repeat the step but click ok “ShutDown” instead of “StartUp”, and add the “C:\ShutDown.bat” to the list.

So now you have “C:\StartUp.bat” added to “Startup” and “C:\ShutDown.bat” added to “Shutdown”.

Startup script

Step 4

Make sure you have met the assumptions above, and check if you have all the “C:\MyRAMDiskFiles” and “C:\Download” folders created. Then restart your computer and enjoy the RAMDisk.

Comments are welcomed!