Windows批处理脚本获取当前驱动器名称

发布于 2024-11-03 21:22:26 字数 143 浏览 1 评论 0原文

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

例如,如果它是 E:\mybatch.bat,则在打开它时,它应该找到 E:\ 与 F:\、G:\ 等相同的内容。

我怎样才能在批处理脚本中做到这一点。 (视窗)

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

寄居人 2024-11-10 21:22:27

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

示例:

echo %CD%

打印

E:\

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

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

%CD:~0,3%

这将导致 E:\,例如,驱动器上的任何位置。

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

Example:

echo %CD%

prints

E:\

on a flash drive mounted to E:.

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%

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

少女的英雄梦 2024-11-10 21:22:27

M$ 文档“使用批处理参数< /a>” 说:

修饰符:%~d0

描述:将 %0 扩展为驱动器号。

M$ documentation "Using batch parameters" says:

Modifier: %~d0

Description: Expands %0 to a drive letter.

江城子 2024-11-10 21:22:27

请注意,

此问题的现有答案并不承认该问题实际上是在询问两个不同的事情:

  1. 当前工作目录的驱动器(“获取当前驱动器名称” );以及
  2. 批处理文件的驱动器(“我需要知道批处理所在的驱动器名称”)。

当批处理文件从工作目录而不是其驻留位置启动时,这些可能会有所不同。因此,在确定哪种解决方案适合他们的案例之前,读者应该清楚其中的差异。

当前工作目录的驱动器

%CD:~0,2%

这将获取完整的当前工作目录并将其缩减为前两个字符,这将是驱动器号和冒号,例如 C:

批处理文件本身的驱动器

%~d0

这会将批处理文件的完整路径(在 %0 中)修剪为仅一个驱动器号和一个冒号,例如 C:< /代码>。


(这是我的拒绝编辑Kai K的回答

Beware

The existing answers to this question don't acknowledge that the question is actually asking about two different things:

  1. the drive of the current working directory (“Get Current Drive name”); and
  2. the drive of the batch file (“I need to know the drive name the batch is in”).

These can be different when the batch file is started from a working directory other than its residing location. Readers should therefore be clear on the difference before determining which solution is relevant to their case.

Drive of the current working directory

%CD:~0,2%

This takes the full current working directory and trims it down to the first two characters, which will be a drive letter and a colon, e.g. C:.

Drive of the batch file itself

%~d0

This trims the full path of the batch file (in %0) to just a drive letter and a colon, e.g. C:.


(this is an expanded version of my rejected edit to Kai K's answer)

穿透光 2024-11-10 21:22:27

如果从 .CMD/.BAT 文件内部运行,您可以使用 %~dp0 获取当前/工作目录。这个更安全一些,因为它知道 UNC 路径等。该变量语法的参考位于此处

If run from inside a .CMD/.BAT file, you can use %~dp0 to get the current/working directory. This one is a little safer as it is aware of UNC paths and such. Reference for the syntax of that variable is available here.

沫尐诺 2024-11-10 21:22:27

您可以使用此功能找到任何驱动器中的所有 USB 驱动器盘符。

@echo off

for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (

  if %%l equ 2 (
    echo %%i is a USB drive.
  )
)

You can find all USB drive letters from any drive with this.

@echo off

for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (

  if %%l equ 2 (
    echo %%i is a USB drive.
  )
)
后来的我们 2024-11-10 21:22:27
%CD:~0,2%

这将为您提供格式为 C: 的当前驱动器,即当前工作目录中的前 2 个字符

C:\Users\ashish>ECHO %CD:~0,2%
C:

D:\projects>ECHO %CD:~0,2%
D:

D:\projects>ECHO %CD%
D:\projects
%CD:~0,2%

This will give you the current drive in the format C: , i.e. first 2 chars from the current working directory

C:\Users\ashish>ECHO %CD:~0,2%
C:

D:\projects>ECHO %CD:~0,2%
D:

D:\projects>ECHO %CD%
D:\projects
呆° 2024-11-10 21:22:27

非常感谢@Sparky3489,如果我只有一个USB闪存驱动器,我将其放入您的算法中,紧接着

echo %%i is a USB drive.
Set FlashDrive=%%I

我还将标识符的措辞更改为

Echo %%i is a USB Flash Drive~!

然后,之后,{和外部}算法,我可以添加闪存Drive Path比如...

Set FlashPath=%FlashDrive%\Users\Public\Documents

然后通过设置其他Path
例如,

Set SourcePath=C:\Users\Public\Documents

我可以为闪存驱动器制作一个批处理文件备份(可以通过 Windows 快捷方式在快速启动窗口中使用相关图标进行调用 ~ 如果对我所说的内容有疑问,请搜索“快速启动”) 。

Rem  * * * * * * * * * Start Batch File * * * * * * * * * *
@Echo OFF
cls
Echo FlashDrive UpDater for
Echo.
Echo Excel, Word ...
Echo * * * * * * * * * ~ Excel SpreadSheets ~ * * * * * * * * *
XCopy /D /I /V /Y /U /S "%SourcePath%\Excel Documents\*.*" "%FlashPath%\Excel Documents\"
Echo * * * * * * * * * ~ Word Documents ~ * * * * * * * * *
XCopy /D /I /V /Y /U /S "%SourcePath%\Word Documents\*.*" "%FlashPath%\Word Documents\"
Echo.
Echo.
Echo FlashDrive = %FlashDrive%
Echo FlashPath = %FlashPath%
Echo.
Echo * Bonus Switch Info * * * * *
Echo * XCopy Switch  /D ~ Copies Files Changed On or After the Specified Date.
Echo *          {If no Date is Given, Copies only those Files whose
Echo *          Source Time is Newer than the Destination Time}.
Echo * XCopy Switch  /I ~ Copies More than One File to Destination (Assumes Destination is a Directory)
Echo * XCopy Switch  /S ~ Copies Directories and Subdirectories Except Empty Ones
Echo * XCopy Switch  /V ~ Verifies Each New File.
Echo * XCopy Switch  /U ~ Copies only Files that Already Exist in Destination.
Echo * XCopy Switch  /Y ~ Suppresses Prompting to Confirm You Want to Overwrite an Existing Destination File.
Echo.
Rem for More Info on XCopy Switches GoTo http://support.microsoft.com/kb/128756
Echo Directory Path = %~DP0
Echo.
Echo * Batch File Name = %0 *
Echo.
Rem Echo %CD:~0,2%, {Returns "Drive Letter & Colon"}
Rem Echo %CD:~0,3%, {Returns "Drive Letter & Colon & BackSlash"}
Pause
cls
Pause
Exit
Rem  * * * * * * * * * End Batch File * * * * * * * * * *

Thanks Very Much @Sparky3489, if I have just one USB Flash Drive, I put this within your Algorithm, Right After the

echo %%i is a USB drive.
Set FlashDrive=%%I

I also changed the Wording of the Identifier to

Echo %%i is a USB Flash Drive~!

Then, After, {and Outside} the Algorithm, I can add the Flash Drive Path such as...

Set FlashPath=%FlashDrive%\Users\Public\Documents

Then by setting up other Paths
Such as

Set SourcePath=C:\Users\Public\Documents

I can make an Batch File BackUp For the Flash Drive, (can be Called Via Windows Short-Cut with an Associated Icon in your Quick Launch Window ~ Search "Quick Launch", if in Doubt to what I'm Talking About).

Rem  * * * * * * * * * Start Batch File * * * * * * * * * *
@Echo OFF
cls
Echo FlashDrive UpDater for
Echo.
Echo Excel, Word ...
Echo * * * * * * * * * ~ Excel SpreadSheets ~ * * * * * * * * *
XCopy /D /I /V /Y /U /S "%SourcePath%\Excel Documents\*.*" "%FlashPath%\Excel Documents\"
Echo * * * * * * * * * ~ Word Documents ~ * * * * * * * * *
XCopy /D /I /V /Y /U /S "%SourcePath%\Word Documents\*.*" "%FlashPath%\Word Documents\"
Echo.
Echo.
Echo FlashDrive = %FlashDrive%
Echo FlashPath = %FlashPath%
Echo.
Echo * Bonus Switch Info * * * * *
Echo * XCopy Switch  /D ~ Copies Files Changed On or After the Specified Date.
Echo *          {If no Date is Given, Copies only those Files whose
Echo *          Source Time is Newer than the Destination Time}.
Echo * XCopy Switch  /I ~ Copies More than One File to Destination (Assumes Destination is a Directory)
Echo * XCopy Switch  /S ~ Copies Directories and Subdirectories Except Empty Ones
Echo * XCopy Switch  /V ~ Verifies Each New File.
Echo * XCopy Switch  /U ~ Copies only Files that Already Exist in Destination.
Echo * XCopy Switch  /Y ~ Suppresses Prompting to Confirm You Want to Overwrite an Existing Destination File.
Echo.
Rem for More Info on XCopy Switches GoTo http://support.microsoft.com/kb/128756
Echo Directory Path = %~DP0
Echo.
Echo * Batch File Name = %0 *
Echo.
Rem Echo %CD:~0,2%, {Returns "Drive Letter & Colon"}
Rem Echo %CD:~0,3%, {Returns "Drive Letter & Colon & BackSlash"}
Pause
cls
Pause
Exit
Rem  * * * * * * * * * End Batch File * * * * * * * * * *
仙女山的月亮 2024-11-10 21:22:27

就像其他人所说的 %~d0

没有必要再重复一遍,但是如果您偶然发现了这个线程,那么这将 cd 到批处理文件的目录。

@echo off
set "fullDir=C:\Program Files"
cd /d %fullDir%                                &rem changes to the full directory 'C:\Program Files'.
echo You're now cd' in the '%cd%' directory.
pause

另外,如果您想从另一个驱动器(例如 Z:)运行批处理文件,那么这个就可以做到。

cd /d %~d0                                     &rem Changes to the current directory
echo You're now cd' in the '%cd%' directory.   &rem This is now your full path of the batch file cd' into.
pause

Like others have said %~d0

There's no point in going over that again, but if you have stumbled into this thread then this will cd to the directory of the batch file.

@echo off
set "fullDir=C:\Program Files"
cd /d %fullDir%                                &rem changes to the full directory 'C:\Program Files'.
echo You're now cd' in the '%cd%' directory.
pause

Also if you want to run a batch file from another drive such as Z: then this one will do that.

cd /d %~d0                                     &rem Changes to the current directory
echo You're now cd' in the '%cd%' directory.   &rem This is now your full path of the batch file cd' into.
pause
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文