Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force batch file to load to RAM before running

I have a batch file in an administrative partition of my portable drive, with a shortcut symlinked to it on the root of the drive. The purpose of the file is to unmount the drive and remount it as the specified letter (mostly for convenience).

When the file is opened, it is opened relative to the current letter rather than to the volume ID, so naturally, when the unmount happens, the command processor has no idea what to do next as it reads the file as needed rather than caching it.

There are two foreseeable solutions that I can think of but can't figure out:

  1. Make the file get cached into RAM before executing
  2. Make the file run relative to the volume ID instead of the mountpoint (tried using {VOLID}\file where {VOLID} is the volume ID, but it couldn't find the file although it was there (navigating to {VOLID}\ correctly opened the directory, but trying to open the file didn't correctly open the file.
like image 880
Arthur Uzulin Avatar asked Jun 20 '16 23:06

Arthur Uzulin


People also ask

How do you run a batch file and wait until it finishes?

Use /WAIT to Wait for a Command to Finish Execution When we start a program in a Batch file using the START command, we can wait until the program is finished by adding /wait to the START command. Even if there are multiple commands, /wait can be used for each process to finish and move to the next one.

How do I create a task scheduler to run a batch file?

Step 1: Create a batch file you wish to run and place it under a folder where you have enough permissions, for example, under C drive. Step 2: Click on Start and under search, type in Task, and click open Task Scheduler. Step 3: Select Create Basic Task from the Action pane on the right of the window.


2 Answers

Despite of the other answers, it's trivial to cache a whole batch script to RAM.

You only need to build a single block, as blocks are parsed and cached before they can be executed.

But blocks have some drawbacks, percent expansion doesn't work, therefore you need to use delayed expansion.
call and goto can't be used, as they would try to read from the file again.

(goto) 2>nul & (
  echo The script is started 
  REM Need to change the directory, else the unmount doesn't work
  c:
  mountvol e: /p
  mountvol g: \\?\Volume{VOLID}\
  dir G:\
  echo The script will end now
  REM Here you need the goto 2>nul hack to avoid an error message
)

The (goto) 2>nul & seems strange here, but it's explained at SO:How to make a batch file delete itself?.
It works also without the goto, but then the scripts ends with an error message

like image 105
jeb Avatar answered Nov 15 '22 04:11

jeb


Have the batch file determine where it is running from see this. If it's running from the portable drive have it make a copy of itself to a permanent drive location (c:\temp for instance) then run that copy of the batch file.

When running a bath file there is no concept of running it from RAM. Windows command processor will always go back to the .bat file for the 'next' command to run. If you edit a batch file while it's running the command processor will pick up your changes.

like image 30
JJF Avatar answered Nov 15 '22 05:11

JJF