从USB驱动器运行批处理文件时盘符发生变化 [英] Running batch file from USB drive when drive letter changes

查看:264
本文介绍了从USB驱动器运行批处理文件时盘符发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我做了一个批处理脚本,并执行多个可移植程序(例如,prog1.exe,prog2.exe等)。问题是,每当我将USB硬盘连接到另一台计算机,驱动器盘符更改,运行我.bat文件时,给我的错误。请大家帮我想想办法。谢谢你。

So, I have made a batch script and it executes multiple portable programs (e.g., prog1.exe, prog2.exe, etc). The problem is whenever I connect the USB drive to another computer, the drive letters change, giving me errors when running my .bat file. Please help me find a solution. Thank you.

推荐答案

%〜D0 为您提供了当前驱动器盘符(包括冒号)。如果批处理文件中所包含的USB驱动器上,就可以使用它。

%~d0 gives you the current drive letter (including the colon). If the batch file's contained on the USB drive, you can use that.

所以,举例来说,而不是

So, for instance, instead of

E:\PortablePrograms\ProgramName.exe

您会写

%~d0\PortablePrograms\ProgramName.exe

...或者你可以做这样的事情。

... or you could do something like this

::change directory to the script's directory's drive
pushd %~d0
::navigate from the drive to the relevant path(s)
cd PortablePrograms
::execute any programs
ProgramName.exe
SecondProgramName.exe
::just because I like to pair my pushes with pops; not required
popd

这篇关于从USB驱动器运行批处理文件时盘符发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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