我有一个批处理文件存储在USB钥匙中。我需要知道该批处理文件所在的驱动器名称。
例如,如果它在E:\mybatch.bat中,它应该找到E:\,F:\,G:\等打开时相同。
如何在批处理脚本中实现?(Windows)
我有一个批处理文件存储在USB钥匙中。我需要知道该批处理文件所在的驱动器名称。
例如,如果它在E:\mybatch.bat中,它应该找到E:\,F:\,G:\等打开时相同。
如何在批处理脚本中实现?(Windows)
%CD%
就是你要找的东西。它会打印批处理文件或运行它的命令的当前工作目录。如果你的批处理文件在驱动器的根目录上,它将只打印驱动器字母,否则你将不得不解析前两个字符。
例如:
echo %CD%
打印
E:\
在挂载到E盘的闪存驱动器上。
更新:正如Andriy在评论中所说,如果您只需要路径的前三个字符,则可以使用这个代替%CD%:
%CD:~0,3%
E:\
。%CD:~0,2%
的结果是当前驱动器的字母加上冒号。因此, %CD:~0,3%
的结果除了包含驱动器字母和冒号外,还会加上反斜杠。 - Andriy M%__CD__%
,您总是在结尾处得到反斜杠。 - mousio%~dp0
,不要带上百分号。 - jeb%
符号。但在这种情况下,我们仍保留 %~d0
而不是 %~dp0
,因为我们不需要路径。 - Alex Peters%~dp0
来获取当前/工作目录。这个命令更安全一些,因为它能够识别UNC路径等内容。有关该变量的语法参考可在这里找到。@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.
)
)
echo %%i is a USB drive.
Set FlashDrive=%%I
我还将标识符的命名方式进行了更改
Echo %%i is a USB Flash Drive~!
然后,在算法之后、之间和之外,我可以添加闪存驱动器路径,例如...
Set FlashPath=%FlashDrive%\Users\Public\Documents
然后通过设置其他路径,例如
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 * * * * * * * * * *
就像其他人所说的%~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
%CD:~0,2%
这将以格式 C:
的形式提供当前驱动器,即当前工作目录的前两个字符
C:\Users\ashish>ECHO %CD:~0,2%
C:
D:\projects>ECHO %CD:~0,2%
D:
D:\projects>ECHO %CD%
D:\projects