Windows批处理脚本获取当前驱动器名称 [英] Windows Batch Script Get Current Drive name

查看:277
本文介绍了Windows批处理脚本获取当前驱动器名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个位于USB密钥上的批处理文件.我需要知道批处理所在的驱动器名称.

I have a batch file which is on a usb key. I need to know the drive name the batch is in.

例如,如果是E:\ mybatch.bat,则在打开时应为F:\,G:\等找到E:\相同的东西.

Example, if it's E:\mybatch.bat it should find E:\ same thing for F:\, G:\ etc.. when it's opened.

如何在批处理脚本中执行此操作. (Windows)

How could I do that in batch script. (Windows)

推荐答案

%CD%是您要寻找的.它打印批处理文件或运行它的命令的当前工作目录.如果批处理文件位于驱动器的根目录下,它将仅打印驱动器号,否则,您必须解析前两个字符.

%CD% is what you're looking for. It prints the current working directory of the batch file or command running it. If your batch file is on the root of the drive, it will just print the drive letter, otherwise you'll have to parse the first 2 characters.

示例:

echo %CD%

打印

E:\

安装在E:上的闪存驱动器上.

on a flash drive mounted to E:.

更新:正如Andriy在评论中所说,如果您只是在寻找路径的前三个字符,请使用它代替%CD%:

Update: As Andriy said in the comments, if you are just looking for the first three characters of the path, then use this instead of %CD%:

%CD:~0,3%

这将导致E:\,例如驱动器上的任何地方.

This will result in E:\, for example, anywhere on the drive.

这篇关于Windows批处理脚本获取当前驱动器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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