我有这个脚本,该脚本检查某个USB,然后在其上打开一个文件。但是,现在我只需要检查插入的USB的驱动器号即可。但是我真的不知道该怎么做,在线搜索没有任何帮助。 这是我现在拥有的脚本:
@echo off
:loop
if exist D:\ (goto Load) else (goto loop)
:Load
D:
start Loader1.exe
goto Finish
:Finish
exit
答案 0 :(得分:0)
如果您有Flash名称,请尝试以下操作:
@echo off
setlocal
set /p vName=Enter volume to search for:
set n=0
set ltrs=ABCDEFGHIJKLMNOPQRSTUVWXYZ
:Loop
call set ltr=%%ltrs:~%n%,1%%
set /a n+=1
vol %ltr%: 2>nul|find /i " %vname%">nul||if %n% lss 26 goto :loop
if %n% equ 26 (set "ltr="
echo No matching volume label found.
) else (echo Volume %1 is drive %ltr%:)
endlocal & set ltr=%ltr%
pause
另一种方式:
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.
)
)
Pause
或=
setlocal enabledelayedexpansion
Set "USB="
REM get removable loaded drives:
for /f "tokens=1-5" %%a in (
'wmic logicaldisk list brief'
) do if %%b Equ 2 if %%d gtr 0 Set USB=!USB! %%a
Echo:Usb removable drives:%USB%
EndLocal&Set USB=%USB:~1%
答案 1 :(得分:0)
以下是从任何驱动器获取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. )
)
答案不是@ scientist_7的副本,我是从Find USB Drive letter获得的。
答案 2 :(得分:0)
您正在寻找类似的东西吗?
这将仅接管可移动磁盘上的任务,并为每个任务分配相关的逻辑名称,以检查文件 Loader1.exe 是否存在以执行下一步操作。
drivetype
WMIC logicaldisk
的值含义
0 Unknown
1 No root directory
2 Removable disk
3 Local disk
4 Network drive
5 Compact disk
6 RAM disk
wmic logicaldisk get caption,drivetype|find "2"
This Result Removable disk :
D: 2
E: 2
F: 2
G: 2
- 标题 = D:
- DriveType = 可移动磁盘 == 2
之后,并确保存在 Loader1.exe
文件以启动/运行命令:
Driver:\Loader1.exe
最后一部分超时(1分钟)以执行下一个循环:
timeout 60 >nul && goto :loop
@echo off
:loop
for /f %%i in ('"wmic logicaldisk get caption,drivetype|find "2""')do if exist "%%~i\Loader1.exe" (
cd /d %%~i\ & start .\Loader1.exe && goto :Finish )
timeout 60 >nul & goto :loop)
:Finish
:: Or ::
@echo off
set "_cd=%cd%" && title <nul
:loop
for /f %%i in ('"wmic logicaldisk where drivetype=2 get name|find /v "Name""
')do if exist "%%~i\Loader1.exe" cd /d %%~i\ & start .\Loader1.exe & cd /d "%_cd%" & exit /b
timeout 60 >nul & goto :loop