Batch file 使用批处理文件检索文件的驱动器号
所以,正如大家所知,根据端口/计算机的不同,我将闪存驱动器插入其中,并识别出不同的驱动器号。我需要代码来找到我的flashdrive名称并向我报告驱动器的字母。这就是我所拥有的,所以你可以看看,试着找出我做错了什么Batch file 使用批处理文件检索文件的驱动器号,batch-file,Batch File,所以,正如大家所知,根据端口/计算机的不同,我将闪存驱动器插入其中,并识别出不同的驱动器号。我需要代码来找到我的flashdrive名称并向我报告驱动器的字母。这就是我所拥有的,所以你可以看看,试着找出我做错了什么 set DriveLabel=JERRYG225 for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do ( pause for /f "tokens=6 delims= " %%i in ('vol
set DriveLabel=JERRYG225
for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
pause
for /f "tokens=6 delims= " %%i in ('vol %%a: ^|find "drive"') do (
pause
echo %%i | find /i "%DriveLabel%" > nul
)
)
有一种更好的方法可以找到指定的驱动器号和卷名 使用给定的可用硬盘分区
C:
和卷名DriveC
,D:
和卷名DataDrive
,以及E:
和卷名BackupDisk
上的USB磁盘驱动器
wmic logicaldisk get caption^, volumename
生成(在命令提示下):
在批处理文件中使用它并删除列名:
for /f "skip=1 tokens=1* delims= " %%x in ('wmic logicaldisk get caption^, volumename') do (
echo %%x %%y
)
我将留给您来解决如何将
%%y
与驱动器标签
匹配,并保存匹配的%%x
(如果找到):-) 我为我的日常任务编写了一个类似的函数,它不依赖于需要管理权限的WNIC
。我使用了USB驱动器的序列号,因为标签不可靠
:GetDriveLetter %SerialNumber% DriveLetter
setlocal EnableDelayedExpansion
set "Letters=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for /L %%I in (0,1,25) do (
set Drive=!Letters:~%%I,1!:
if exist !Drive! (
dir !Drive!|findstr /L "%~1" >NUL
if not ERRORLEVEL 1 (
set Result=!Drive!
goto :GetDriveLetterRet
)
)
)
:GetDriveLetterRet
endlocal &set "%2=%Result%" &goto :EOF
要获取USB驱动器的驱动器号,请执行以下操作:
call GetDriveLetter ABCD-1234 Drive
if defined Drive (
pushd !Drive! 2>NUL && (
REM do things here
)
) else echo Can't find USB drive^^! & pause >NUL
编辑:要获取USB驱动器的序列号,只需在该驱动器上执行DIR
或VOL
命令。您当前的代码有什么问题?
call GetDriveLetter ABCD-1234 Drive
if defined Drive (
pushd !Drive! 2>NUL && (
REM do things here
)
) else echo Can't find USB drive^^! & pause >NUL