[英]using a call menu in for loop in batch kills the set variable
:start
CLS
echo.
echo ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
for /f "usebackq tokens=*" %%A in ("%~dp0pxhosts.txt") do (
del "\\%%A\C$\Windows\Downloaded Program Files\Olympus.exe" /F /S /Q >nul 2>&1
echo %%A
pause
if exist "\\%%A\C$\Program Files\Common Files\Olympus Shared" call :skip
if not exist "\\%%A\C$\Program Files\Common Files\Olympus Shared" call :fix
if not exist "\\%%A\C$\Program Files (x86)\Common Files\Olympus Shared" call :fix
:fix
echo fix needed on %%A
pause
esentutl /y "Olympus.exe" /d "\\%%A\C$\Windows\Downloaded Program Files\Olympus.exe"
"%~dp0psexec.exe" \\%%A -accepteula "\\%%A\C$\Windows\Downloaded Program Files\Olympus.exe"
:skip
echo not needed on %%A
pause
)
對此感到困惑,需要一些幫助。 這是漫長的一天,我敢肯定這很容易,但我看不到。 回聲%% A(第3行)將從文本文件中的PC列表中回顯計算機名稱,我正在運行驅動程序安裝補丁。 到達“如果存在”(如果不存在)部分的那一刻,%% A變量將被破壞,並僅報告%A,導致其余內容失敗。
我嘗試了通常的goto選項,但很快發現它們中斷了循環。 我很高興發現使用調用例程可以使for循環繼續進行,但現在剩下的變量殘破了。
下一個代碼段顯示調用子例程 邏輯 (但是我無法證明esentutl
和psexec
使用的正確性):
for /f "usebackq tokens=*" %%A in ("%~dp0pxhosts.txt") do (
del "\\%%A\C$\Windows\Downloaded Program Files\Olympus.exe" /F /S /Q >nul 2>&1
echo %%A
pause
if exist "\\%%A\C$\Program Files\Common Files\Olympus Shared\" (
call :skip %%A
) else (
call :fix %%A
)
if not exist "\\%%A\C$\Program Files (x86)\Common Files\Olympus Shared\" call :fix %%A
)
rem skip procedures
goto :SomethingNext
:fix
echo fix needed on %1
pause
esentutl /y "Olympus.exe" /d "\\%1\C$\Windows\Downloaded Program Files\Olympus.exe"
"%~dp0psexec.exe" \\%1 -accepteula "\\%1\C$\Windows\Downloaded Program Files\Olympus.exe"
rem return from a subroutine
exit /B
:skip
echo not needed on %1
pause
rem return from a subroutine
exit /B
:SomethingNext
要建立64位或32位程序文件結構(但不能同時):
for /f "usebackq tokens=*" %%A in ("%~dp0pxhosts.txt") do (
del "\\%%A\C$\Windows\Downloaded Program Files\Olympus.exe" /F /S /Q >nul 2>&1
echo %%A
pause
rem suppose that no folder exists and (therefore) fix is necessary
set "pf32="
set "pf86="
set "needFix=yes"
rem deny the premise if either folder exists
if exist "\\%%A\C$\Program Files\Common Files\Olympus Shared\" (
set "needFix="
set "pf32=pf32"
)
if exist "\\%%A\C$\Program Files (x86)\Common Files\Olympus Shared\" (
set "needFix="
set "pf86=pf86"
)
rem resume the premise if both folders exist
if defined pf32 if defined pf64 set "needFix=yes"
if defined needFix ( call :fix %%A ) else ( call :skip %%A )
)
rem skip procedures
goto :SomethingNext
for /f "usebackq tokens=*" %%A in ("%~dp0pxhosts.txt") do (
del "\\%%A\C$\Windows\Downloaded Program Files\Olympus.exe" /F /S /Q >nul 2>&1
echo %%A
pause
if exist "\\%%A\C$\Program Files\Common Files\Olympus Shared" call :skip "%%A"
if not exist "\\%%A\C$\Program Files\Common Files\Olympus Shared" call :fix "%%A"
if not exist "\\%%A\C$\Program Files (x86)\Common Files\Olympus Shared" call :fix "%%A"
)
goto :eof
:fix
echo fix needed on %~1
pause
esentutl /y "Olympus.exe" /d "\\%~1\C$\Windows\Downloaded Program Files\Olympus.exe"
"%~dp0psexec.exe" \\%%A -accepteula "\\%~1\C$\Windows\Downloaded Program Files\Olympus.exe"
goto :eof
:skip
echo not needed on %~1
pause
goto :eof
批處理僅執行命令,直到到達goto
, exit
或文件結束。 它沒有“過程”或“函數”的概念,並且:name
只是一個標簽-並且在block語句中不允許使用標簽。
名義上, metavariable
%% A在子例程中不可見(實際上,這似乎可行),因此%%A
作為引用的參數傳遞給了每個子例程。 在子例程中, %~1
檢索並取消引用第一個參數,從而訪問該元變量。
請注意, goto :eof
用於跳過批處理文件中其余的物理代碼。 批處理將其理解為“轉到文件末尾”-標簽:eof
是語法的必需部分,但不應在代碼中作為標簽輸入。
但我建議您檢查一下自己的邏輯。 if exist
將跳過,但if not exist
則將修復,因此可在缺少“ ... Olympus Shared”的情況下應用此修復, if exist
則跳過該修復。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.