我使用下面的批处理脚本来查找扩展名为 .exe 的文件。但是有些文件名有两个名称 -CenASintstall_4.0(DS-15-103-W).exe和CenASintstall_4.0(DS-15-103-W) (2).exe (并非所有文件都有两个文件名共存)
如果确实如此,我该如何修改代码以获得带有 (2) 的代码。请参阅我的批处理脚本和屏幕截图,以便于理解。
Setlocal EnableDelayedExpansion
:scan
Echo Auto-Detect Source
set pc=DS-15-103-W
goto pcname
:pcname
REM Locate the Drive contains the folder-"Site Information"
set mydrive=NOT_FOUND
for /f "skip=1" %%G in ('wmic logicaldisk get name') do (
if exist "%%G\Site Information" set mydrive=%%G
)
echo Possible USB Drive is %mydrive%
if %mydrive%==NOT_FOUND (goto nas) else (goto check)
:check
REM Locate the executable file with full path
set pathdrive=NOT_FOUND
for /f "tokens=* delims=\" %%a in ('dir "%mydrive%" /s /b ^| find /i "(%pc%).exe"') do set "pathdrive=%%~dpnxa"
echo. The file path is %pathdrive%
if "%pathdrive%"=="NOT_FOUND" (goto nas) else (goto extract)
:extract
REM Run file extraction
"%pathdrive%" /s /x /b" %systemdrive% /v" /qn"
echo %errorlevel%
pause
如果我将 for /f.. 更改为
for /f "tokens=* delims=\" %%a in ('dir "%mydrive%" /s /b ^| find /i "(%pc%)(2).exe"') do set "pathdrive=%%~dpnxa"
屏幕截图 4
答案1
答案2
@echo off && Setlocal EnableDelayedExpansion
:scan
echo/Auto-Detect Source
set "_pc=DS-15-103-W"
set "_my_drive=NOT_FOUND"
set "_file_path=NOT_FOUND"
:pcname
for /f usebackq^tokens^=4delims^=^<^> %%G in (
`wmic logicaldisk get name /format:xml ^| find "VALUE" ^| sort /r`
)do if exist "%%~G\Site Information\." set "_my_drive=%%~G" && goto :check
if "%_my_drive%" == "NOT_FOUND" goto :eOf
:check
for /f usebackq^delims^= %%a in (`2^>nul where /r "%_my_drive%" "*(%_pc%)*.exe"`
)do set /a "_i+=1+0" && call set "_file_path_%%_i%%=%%~a"
:extract
if "!_i!" == "" goto :eOf
for /l %%L in (1 1 !_i!)do echo. The file path is "!_file_path_%%~L!" && =;(
start "" /w "!_file_path_%%~L!" /s /x /b %systemdrive% /v /qn & !errorlevel!
);=
timeout -1 | endlocal
- 你的 :标签
:nas
您的 nas 标签在发布的代码中不存在,因此请将其替换为:eOf