[ol]@echo off
setlocal enabledelayedexpansion
set targetLabel=八段锦
set targetFile=123\abc.exe
for %%d 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 (
if exist %%d:\ (
for /f "tokens=1-2 delims=:" %%a in ('fsutil volume diskfree %%d:\') do (
set "label="
for /f "usebackq tokens=2 delims==" %%c in (`wmic logicaldisk where DeviceID^="%%d:" get VolumeName /value`) do (
set "label=%%c"
)
if "!label!"=="%targetLabel%" (
echo 找到目标盘符: %%d:,正在运行 %targetFile%...
pushd %%d:\
call %targetFile%
popd
)
)
)
)
endlocal[/ol]复制代码该脚本会遍历所有盘符,搜索卷标为"八段锦"的优盘盘符,并自动运行该盘符下的123\abc.exe。注意:如果存在多个满足条件的盘符,脚本会逐个运行它们下面的123\abc.exe文件。如果只要找到一个满足条件的盘符并运行对应的文件,可以在找到目标盘符后添加一个exit语句来终止脚本的执行。
@echo off
chcp 936
set pg=123\abc.exe
set pf=八段锦
for %%i in (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 (set p=%%i&&call :ch)
echo 没找到对应程序&exit
:ch
echo %p%:
if not exist %p%: goto :bk
for /f "tokens=4 delims= " %%a in ('vol %p%: ^|find /i "%pf%"') do (set pt=%%a)
if 1%pt%==1%pf% (call "%p%:\%pg%" &&exit)
:bk
exit /b
@echo off setlocal enabledelayedexpansion set "volume_label=八段锦" set "executable=abc.exe" for /f "usebackq tokens=1,2" %%a in (`wmic logicaldisk where "drivetype=2" get caption^, volumename ^| findstr /i "%volume_label%"`) do ( set "drive_letter=%%a" set "volume_name=%%b" rem Remove trailing spaces from volume name for /l %%i in (1,1,30) do if "!volume_name:~-1!"==" " set "volume_name=!volume_name:~0,-1!" ) if defined drive_letter ( set "path=!drive_letter!\123\%executable%" if exist "!path!" ( echo Running !path! "!path!" ) else ( echo File not found: !path! ) ) else ( echo Specified volume label not found. ) endlocal