Fixing the drive letter for RAMDisk without saving and loading drive image – A solution for Dataram RAMDisk

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

Using RAMDisk

One of the most popular RAMDisk solution is Dataram RAMDisk. It is FREE to setup a RAMDisk of max. 4GB size, running in both 32-bit & 64bit Windows systems (Usually other RAMDisk solutions would charge quite much for a 64bit version.) with promising performance. If you are using a 32bit Windows, Dataram RAMDisk  can access the RAM outside the address range(the so-called 4GB RAM limit in 32bit Windows) so your RAM wouldn’t be wasted. I have been using Dataram RAMDisk since I upgraded to 64bit Windows 7, and I’m totally satisfied with its performance and stability.

The problem

My only complain about Dataram RAMDisk is that the drive letter of the RAMDisk is dynamically assigned on each time it runs. So today I have “K:” as my RAMDisk and have my browser cache folder configured to “K:”. On the next morning if I have plugged in an USB drive (or assigned another network drive) before the RAMDisk being created, my USB / network drive will occupy the drive letter “K:” so the RAMDisk will become “L:”. This will be a big disaster because all the cache will be written into the USB / network drive instead of the RAMDisk. So I want the RAMDisk keeps a consistent drive letter all the time.

Official solution

In the user manual, Dataram provides a solution through saving the RAMDrive into a drive image file once and loading it back on every time the computer starts. I personally don’t like it because :

  1. It creates a large disk image file (same size as the RAMDisk) on the harddisk, and
  2. It takes quite much overhead to load the disk image file on every booting.

My solution : A DOS Batch script

Instead, I write my own solution using just a 3-line (without comments) DOS batch script. Thanks to the feature that Dataram RAMDisk allows users to specify a Drive Label and keep it consistent all the time. So my trick is: When the system starts, the script will find the dynamic RAMDisk drive letter (e.g. “K:”) according to the disk label, then create a consistent drive letter (e.g. “R:”) that mounts to the dynamic drive letter (“K:”).

Step-by-step walk-through

To make it simple, I post the walk-through here:

1. Create the batch file.

Create a new .bat file (e.g. C:\StartUp.bat) with the following content:

(Update: Please note that batch script takes line-break as “end of command”, so if you find the script doesn’t work at your PC, try removing extra line-breaks. See the comments for more details.)

@rem assign the key variables.
SET _label=RAMDISK
SET _ramdrive=R:

@rem Below is the magic happens : Find the drive letter which has volume name set as _label, 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 if you want to copy some files or mount your cache folders, do it after this line.

If you are interested in how this magic script works, read below. Otherwise you can safely skip it and go on step 2.

The command :

wmic logicaldisk where "VOLUMENAME='RAMDISK'" get caption

will run once, and gives such result :

Caption
K:

This result is returned as a string. Then a for-loop will go through the result string line-by-line. For each line, the string is split by “:” into parts and the first part will be stored into variable %%a.

The for-loop options explained:

“skip=1” : Skip the first line (i.e. “Caption”). The first line is the header, we are not interested in it.

“tokens=1” : Only get the first part after the split. The first part is the drive letter.

“delims=:” : Use “:” as the delimiter to split the string.

So at the end the variable  %%a would be assigned with value “K”.  Then the subst command becomes

subst R: K:\

which will mount the R: to the K:

2. Specify the Drive Label for RAMDisk

Open the Dataram RAMDisk Configuration Utility, check the “Disk Label” box and enter a disk label “RAMDISK” in the text box below.

You may assign name other than “RAMDisk”, but if you do, remember to update the DOS batch script with

SET _label=TheNewDriveLabel

3. Assign the startup script

So that the computer will run the StartUp.bat each time when the computer boots. There are a few steps to do this.

3.1 Press Win + R (or find “Run” on start menu), a “Run” dialog will appear. Enter “gpedit.msc” and select the “gpedit” in Programs.

Note: For Win7 Home version where the gpedit.msc is not bundled, you may consider using the Task Scheduler to launch the script after Windows starts. Please visit this page for the details: http://windows.microsoft.com/en-US/windows7/schedule-a-task

3.2 In the “Group Policy Editor”, click “Local Computer Policy” -> “Windows Settings” -> “Scripts(Startup/Shutdown)”  then on the right side, click “Startup”

Startup script

3.3 On the “Scripts” tab, click “Add…” and add the “C:\StartUp.bat” to the list. Click “OK” to finish.

4. That’s it!

Now restart the computer and there will be a “R:” drive that is mounted to the RAMDisk, no matter what drive letter the RAMDisk actually is.

Conclusion

The advantage of this solution over the Dataram’s solution is: this solution doesn’t require a large disk image file, thus the overhead of reading and loading the disk image is none. This reaults in a faster system booting and less disk space occupied.

A final note about this solution: There will be two drive letters assigned to the same drive. My solution creates a new virtual drive with consistent drive letter (“R:”), and mount it to the actual RAMDisk (“K:”). Although the RAMDisk can always be referred as “R:”,  the “K:” is still there. Currently I have no idea how to remove or hide the “K:” in the batch script. So you will end up with both “K:” and “R:” accessing to the same drive.

Comments are welcomed!

Updated-19/06/2011: Merijn (see comments below) suggested a very good approach using the “diskpart” command instead of “subst”. It is a better solution because it alters the drive letter of the RAMDisk directly, so at the end you won’t have two drives on “My Computer” accessing the same drive.

14 Replies to “Fixing the drive letter for RAMDisk without saving and loading drive image – A solution for Dataram RAMDisk”

  1. Hi, I just come across this page to find such useful information. After creating the batch file, I found I can not make it work for me,. The main error is from %%a, my system keeps complaining “%%a should not appear here”…. It seems a syntax error or something. I know a little bit programming but not such scripts.. 🙁
    Would you mind attach the ready-to-use BAT file for me to download ? I am using Win7 x64 pro version. Thanks!

    1. The version on the post is working in my Win7 x64 version.
      Pls note that the batch script is parsed line-by-line, so when the parser reads a line-break, it will consider that as the end of a command.

      The script on this post is word-wrapped, so if you copy the script from this page, you may need to remove the extra line-breaks (esp. the line-break in for-loop)
      You may try removing all those comments as well (not sure the comments matters or not in this problem).

      So you may finally have only 3 lines of code like this:
      ======
      (line-1) SET _label=RAMDISK
      (line-2) SET _ramdrive=R:
      (line-3) FOR /F “skip=1 tokens=1 delims=: ” %%a IN (‘wmic logicaldisk where “VOLUMENAME=’%_label%'” get caption’) DO ( subst %_ramdrive% %%a:\ )
      =======

      See if it helps.

  2. Thank you for your detailed instructions. With this solution I don’t need to search for an other solution than Dataram Ramdisk which works fine.

    The script makes use of subst to make an extra virtual drive. I modified/updated the script (based on http://community.spiceworks.com/scripts/show/266-change-drive-letter) to use diskpart instead to really change the drive letter. In that case the original drive letter can be used again for other devices. The script needs admin rights, but this is taken care of if you start it from the system group policies as explained above. I am using this update script in Win7x64 without problems.

    Maybe it is also usefull for someone else.

    UPDATED SCRIPT:

    @echo off
    :: Settings
    SET _ramdisklabel=RAMDISK
    SET _ramdiskletter=Z:

    :: Get ramdisk disk number in diskpart
    echo list volume > %systemdrive%\ListDrives.tmp
    diskpart /s %systemdrive%\ListDrives.tmp > %systemdrive%\DriveList.tmp
    FOR /F “tokens=2-4” %%a IN (‘type %systemdrive%\DriveList.tmp’) DO @IF /I “%%c”==”%_ramdisklabel%” @set _ramdisknum=%%a

    :: Create drive change script
    echo. > %systemdrive%\ChangeDrive.tmp
    if DEFINED _ramdisknum (
    echo select volume %_ramdisknum% >> %systemdrive%\ChangeDrive.tmp
    echo assign letter=%_ramdiskletter% >> %systemdrive%\ChangeDrive.tmp
    )

    :: Run diskpart using the new script file
    diskpart /s %systemdrive%\ChangeDrive.tmp

    :: Delete the script files
    del /q /f %systemdrive%\ListDrives.tmp
    del /q /f %systemdrive%\ChangeDrive.tmp
    del /q /f %systemdrive%\DriveList.tmp

    exit /b 0

  3. OK, I did try but I still can not get this thing work out for me. 🙁
    Can someone PLEASE make these 2 alternative scripts to separated .BAT files for download directly ?
    BIG THANKS!

  4. I don’t know if you noticed or not, but the image that is saved is compressed by default. In my case this means that a 2 GB image only takes up 956 KB on disk. It works well for loading the basic NTFS partition on startup in my testing so far.

    1. Yes, you’re right. The software provides the image file compression function by its recent update so that the image file for ram drive is much smaller. It’s totally possible to use the default “image-save-and-load” mechanism to get the drive letter fixed.

      However, the method I proposed here is much more flexible than the default method. You could do almost any file operations after the ram drive is ready. For example, you may:
      – redirect the Chrome browser’s cache folder to the ram drive,
      – save only certain files to harddisk before PC shutdown instead of imaging the whole ram drive. (usually the ram drive are for temporary data like cache files that we don’t want to save them to harddisk after PC shuting down.)

      You may read this post to see how it works.

  5. Hi Merijin,

    My ramdisk always installed as disk 0 in disk management window.
    is there a way to change it other than disk 0, because one of my applicatin could not recognized my harddisk MBR.

  6. Thanks , I’ve recently been looking for information about this topic for
    ages and yours is the best I have came upon till now.
    But, what concerning the bottom line? Are you positive in regards to the supply?

Leave a Reply

Your email address will not be published. Required fields are marked *