Cmd 当被调用的批位于不同的驱动器上时,如何获取调用批的驱动器号

Cmd 当被调用的批位于不同的驱动器上时,如何获取调用批的驱动器号,cmd,Cmd,我有两个windows批处理文件:a.bat和b.bat b、 蝙蝠在D:车道上,在我的路上 a、 bat位于E:驱动器上,其中包含以下内容: call b.bat echo %thedrive% IF %~d0==D: ( SET %thedrive=testdrive ) ELSE ( SET %thedrive=livedrive ) b、 bat中有如下内容: call b.bat echo %thedrive% IF %~d0==D: ( SET %thedrive=

我有两个windows批处理文件:a.bat和b.bat

b、 蝙蝠在D:车道上,在我的路上

a、 bat位于E:驱动器上,其中包含以下内容:

call b.bat
echo %thedrive%
IF %~d0==D: (
  SET %thedrive=testdrive
) ELSE (
  SET %thedrive=livedrive
)
b、 bat中有如下内容:

call b.bat
echo %thedrive%
IF %~d0==D: (
  SET %thedrive=testdrive
) ELSE (
  SET %thedrive=livedrive
)
问题是,%d0获取的是b.bat所在的驱动器号,而不是调用(a.bat)批处理文件所在的驱动器


如何获取调用批处理文件所在的驱动器?

b.bat无法知道它是从另一个批处理文件中调用的,更不用说调用批处理文件所在的位置了。b.bat能够知道的唯一方法是,如果信息作为参数传入,那么您将依赖调用者来传递信息

根据您发布的代码,您似乎希望.bat知道它在哪个驱动器上。如果是这样,您需要将代码从b.bat移动到a.bat

如果您有许多.bat脚本需要设置
dractive
变量,并且您不想在所有文件中包含所有逻辑,那么我将更改b.bat,如下所示:

IF /i %~d1==D: (
  SET thedrive=testdrive
) ELSE (
  SET thedrive=livedrive
)
每个a.bat脚本都必须使用以下命令调用b.bat:

call b.bat "%~f0"

因为b.bat在路径中,所以您只需检查b.bat中的当前目录。也就是说,b.bat应该是:

IF %cd:~0,2% == D: (
  SET thedrive=testdrive
) ELSE (
  SET thedrive=livedrive
)
也许我能帮你。