强制批处理文件在运行之前加载到RAM [英] Force batch file to load to RAM before running

查看:52
本文介绍了强制批处理文件在运行之前加载到RAM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在便携式驱动器的管理分区中有一个批处理文件,在该驱动器的根目录上链接有一个快捷方式.该文件的目的是卸载驱动器并将其重新安装为指定的字母(主要是为了方便起见).

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).

打开文件时,相对于当前字母而不是卷ID来打开文件,因此自然地,当发生卸载时,命令处理器不知道下一步该做什么,因为它会根据需要读取文件而不是缓存它.

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. 在执行之前使文件缓存到RAM中
  2. 使文件相对于卷ID而不是挂载点运行(使用{VOLID} \ file尝试,其中{VOLID}是卷ID,但文件在那里但找不到文件(导航到{VOLID} \正确打开了目录,但是尝试打开文件并没有正确打开文件.

推荐答案

尽管有其他答案,将整个批处理脚本缓存到RAM还是很简单的.

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.

但是块有一些缺点,无法进行百分比扩展,因此您需要使用延迟扩展.
无法使用 call goto ,因为它们会尝试再次从文件中读取.

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
)

这里的(goto)2> nul& 似乎很奇怪,但是在 SO:How中解释了删除批处理文件?.
它也可以在没有 goto 的情况下工作,但是脚本以错误消息结尾

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

这篇关于强制批处理文件在运行之前加载到RAM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆