windows 如何在批处理文件中测试当前驱动器号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/866623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:29:28  来源:igfitidea点击:

How do I test the current drive letter in a batch file?

windowsbatch-file

提问by JimDel

I'm trying to write a batch file that takes the drive letter the batch file is being run from, and uses it an an IF statement. If the letter is M: for example, it will jump to the label :mSection.

我正在尝试编写一个批处理文件,该文件采用运行批处理文件的驱动器号,并将其用作 IF 语句。例如,如果字母是 M:,它将跳转到标签 :mSection。

Is this even possible?

这甚至可能吗?

回答by Dave Cluderay

You can use %~d0 to get the drive letter. Something like this:

您可以使用 %~d0 来获取驱动器号。像这样的东西:

IF "%~d0"=="M:" CALL :mSection

回答by user10786435

You can use %~d0 toget the drive letter...

您可以使用%~d0 to获取驱动器号...

It doesn't work whet the .bat is on different drive (somewhere in the PATH.)

如果 .bat 位于不同的驱动器上(在 PATH 中的某处),则它不起作用。

I'd suggest:

我建议:

echo %cd:0,2%