带有 FOR 和 IF EXIST 和 GOTO 问题的 Windows 批处理
Posted
技术标签:
【中文标题】带有 FOR 和 IF EXIST 和 GOTO 问题的 Windows 批处理【英文标题】:Windows batch with FOR and IF EXIST and GOTO issue 【发布时间】:2014-09-16 14:00:55 【问题描述】:好的,所以我正在编写一个批处理文件,通过替换客户端上的文件来帮助客户端迁移。
@ECHO OFF
ECHO Only type "clients" or "servers"
SET /P TYPE=clients or servers:
SET /P LIST=%TYPE% list filename:
SET /P SITE=Site:
SET SYMPATH=""
SET LIST2=""
SET N=
SET N2=0
Echo This will migrate %TYPE% in site %SITE% from list %LIST%, correct?
PAUSE
:STRT
SET /a N2=N2+1
SET LIST2=%LIST%%N%
FOR /F "tokens=*" %%v IN (%LIST2%) DO (
ECHO Checking %%v...
SET COMP=%%v
if exist "\\%%v\c$\program files (x86)\symantec\symantec endpoint protection\smc.exe" SET "SYMPATH=program files (x86)\symantec\symantec endpoint protection" & CALL :MIGRATE
if exist "\\%%v\c$\program files\symantec\symantec endpoint protection\smc.exe" SET "SYMPATH=program files\symantec\symantec endpoint protection" & CALL :MIGRATE
if exist "\\%%v\c$\program files (x86)\symantec antivirus\smc.exe" SET "SYMPATH=program files (x86)\symantec antivirus" & CALL :MIGRATE
if exist "\\%%v\c$\program files\symantec antivirus\smc.exe" SET "SYMPATH=program files\symantec antivirus" & CALL :MIGRATE
ECHO Could not find SEP on client %%v
ECHO %%v >> %LIST%%N2%
)
:MIGRATE
ECHO Migrating %COMP%...
psexec \\%COMP% -h "c:\%SYMPATH%\smc.exe" -p xxxxxx -stop
copy "%TYPE%_xml\My Company_Workplace_%TYPE%_%SITE%_sylink.xml" "\\%COMP%\c$\%SYMPATH%\SyLink.xml" /Y
psexec \\%COMP% -h "c:\%SYMPATH%\smc.exe" -start
ECHO Migrated %COMP% to SEP12 group %SITE% >> END_%SITE%.log
ECHO Migrated %COMP% to SEP12 group %SITE%
EXIT /b
SET /a N=N+1
ECHO End of the list, starting over with %LIST2%
GOTO STRT `
除了客户端被迁移后,它会跳回 CALL 并执行代码之外,大多数事情都可以工作:
ECHO Could not find SEP on client %%v
ECHO %%v >> %LIST%%N2%
这在代码方面是正确的,但是我希望它跳过这两行并继续下一个客户端,如下所示:
@ECHO OFF
ECHO Only type "clients" or "servers"
SET /P TYPE=clients or servers:
SET /P LIST=%TYPE% list filename:
SET /P SITE=Site:
SET SYMPATH=""
SET LIST2=""
SET N=
SET N2=0
Echo This will migrate %TYPE% in site %SITE% from list %LIST%, correct?
PAUSE
:STRT
SET /a N2=N2+1
SET LIST2=%LIST%%N%
FOR /F "tokens=*" %%v IN (%LIST2%) DO (
ECHO Checking %%v...
SET COMP=%%v
if exist "\\%%v\c$\program files (x86)\symantec\symantec endpoint protection\smc.exe" SET "SYMPATH=program files (x86)\symantec\symantec endpoint protection" & CALL :MIGRATE **& GOTO :NEXT**
if exist "\\%%v\c$\program files\symantec\symantec endpoint protection\smc.exe" SET "SYMPATH=program files\symantec\symantec endpoint protection" & CALL :MIGRATE **& GOTO :NEXT**
if exist "\\%%v\c$\program files (x86)\symantec antivirus\smc.exe" SET "SYMPATH=program files (x86)\symantec antivirus" & CALL :MIGRATE **& GOTO :NEXT**
if exist "\\%%v\c$\program files\symantec antivirus\smc.exe" SET "SYMPATH=program files\symantec antivirus" & CALL :MIGRATE **& GOTO :NEXT**
ECHO Could not find SEP on client %%v
ECHO %%v >> %LIST%%N2%
**:NEXT**
)
:MIGRATE
ECHO Migrating %COMP%...
psexec \\%COMP% -h "c:\%SYMPATH%\smc.exe" -p xxxxxx -stop
copy "%TYPE%_xml\My Company_Workplace_%TYPE%_%SITE%_sylink.xml" "\\%COMP%\c$\%SYMPATH%\SyLink.xml" /Y
psexec \\%COMP% -h "c:\%SYMPATH%\smc.exe" -start
ECHO Migrated %COMP% to SEP12 group %SITE% >> END_%SITE%.log
ECHO Migrated %COMP% to SEP12 group %SITE%
EXIT /b
SET /a N=N+1
ECHO End of the list, starting over with %LIST2%
GOTO STRT `
但这不起作用,因为我不能在代码块()中使用 GOTO 和标签
那么,有人有想法吗?
【问题讨论】:
【参考方案1】:FOR /F "tokens=*" %%v IN (%LIST2%) DO (
...
SET "SYMPATH="
if exist "\\%%v\c$\..." SET "SYMPATH=..." & CALL :MIGRATE
if exist "\\%%v\c$\..." SET "SYMPATH=..." & CALL :MIGRATE
if exist "\\%%v\c$\..." SET "SYMPATH=..." & CALL :MIGRATE
if exist "\\%%v\c$\..." SET "SYMPATH=..." & CALL :MIGRATE
if not defined SYMPATH (
ECHO Could not find SEP on client %%v
ECHO %%v >> %LIST%%N2%
)
)
对于每次迭代重置sympath
并进行测试。如果最后没有定义变量,则没有找到匹配项。
【讨论】:
【参考方案2】:for /f "tokens=*" %%v IN (%LIST2%) do (
echo Checking %%v...
set "COMP=%%~v"
set "SYMPATH="
for %%P in (
"program files (x86)\symantec\symantec endpoint protection"
"program files\symantec\symantec endpoint protection"
"program files (x86)\symantec antivirus\smc.exe"
"program files\symantec antivirus"
) do if not defined SYMPATH if exist "\\%%v\c$\%%~P" (
set "SYMPATH=%%~p"
call :MIGRATE
)
if not defined SYMPATH (
echo Could not find SEP on client %%v
echo %%v >> %LIST%%N2%
)
)
【讨论】:
以上是关于带有 FOR 和 IF EXIST 和 GOTO 问题的 Windows 批处理的主要内容,如果未能解决你的问题,请参考以下文章
[Golang]运算符,条件控制语句--[if]-[if-else]-[for]-[switch]-[goto]