【问题标题】:Bad syntax in Batch File批处理文件中的语法错误
【发布时间】:2014-02-23 07:35:51
【问题描述】:

已经很晚了,看着这个我累了。有人能解释一下我在这个脚本中哪里可能有错误的语法吗?

该脚本会查看computers.txt 文件中的活动机器上是否存在旧安装。如果是这样,它应该卸载它,复制新安装,然后安装它。如果有任何失败,请记录到其各自的日志文件。

@echo off
:CheckifLogsExist
if NOT exist Uninstall.log (
    copy /y nul Uninstall.log
) else (
    del Uninstall.log && copy /y nul Uninstall.log
)

if NOT exist WMIC.log (
    copy /y nul WMIC.log
) else (
    del WMIC.log && copy /y nul WMIC.log
)

if NOT exist Copying.log (
    copy /y nul Copying.log
) else (
    del Copying.log && copy /y nul Copying.log
)

if NOT exist Install.log (
    copy /y nul Install.log
) else (
    del Install.log && copy /y nul Install.log
)

:checkifalive
for /F %%I IN (computers.txt) DO 
(
    ping -n 1 %%I 
    if NOT %errorlevel%==0 echo Machine offline && goto:EOF

    :Uninstall
    echo "Uninstalling previous version of Symantec Endpoint Protection"
    psexec \\%%I -s wmic failfast:on product where name="Symantec Endpoint Protection" call uninstall /nointeractive
    if NOT %errorlevel%==0 echo %%I  -  %errorlevel% >> Uninstall.log

    :copy
    echo "Finding out which processor is in the machine"
    wmic cpu list brief > temp.out
    findstr /I "86" temp.out && goto copy86 || goto copy64
    if NOT %errorlevel%==0 echo %%I  -  %errorlevel% >> WMIC.log

    :copy86
    echo "Copying the installation to the local machine"
    copy "C:\installation.exe" \\%%I
    if NOT %errorlevel%==0 echo %%I  -  %errorlevel% >> Copying.log
    goto Install86

    :copy64
    echo "Copying the installation to the local machine"
    copy "C:\installation.exe" \\%%I
    if NOT %errorlevel%==0 echo %%I  -  %errorlevel% >> Copying.log
    goto Install64

    :Install86
    echo "Installing upgraded Symantec Endpoint Protection"
    psexec \\%%I -s "C:\installation.exe /s"
    if NOT %errorlevel%==0 echo %%I  -  %errorlevel% >> Install.log
    goto Finish

    :Install64
    echo "Installing upgraded Symantec Endpoint Protection"
    psexec \\%%I -s "C:\installation.exe /s"
    if NOT %errorlevel%==0 echo %%I  -  %errorlevel% >> Install.log
    goto Finish

    :Finish
)

【问题讨论】:

  • 很抱歉,太晚了,您很累,但如果您需要帮助,您需要提供更多信息。究竟是什么错误,在脚本中的哪一点?您是否尝试过使用 @echo on 运行它以查看它在每一行上的作用?
  • 另外,我认为这不会导致语法错误,但会导致它不符合您的预期 - 我认为您不能像批量那样在 for 循环中使用 goto .对于实际安装部分,您可能会使用子例程做得更好。
  • @Vicky 是对的:goto 打破了循环。但是您还有另一个问题:您需要延迟扩展才能在块内使用变量。 %errorlevel% 不是,正如您所期望的,它与之前的 for 值相同,并且不会在 () 之间变化。

标签: batch-file if-statement for-loop command-prompt


【解决方案1】:

您的语法错误是这些需要在同一行,do 后面有一个空格

请注意 Stephan 的评论,您还需要 enable delayed expansion 或如 Vicky 所说,使用 subroutine forindo 命令将 call

for /F %%I IN (computers.txt) DO 
(

【讨论】:

    【解决方案2】:
    @echo off
    :clear_logfiles   :: Label not needed
    :: copy command doesn't care, if the file exists or not, it just (re)creates it with size 0:
    copy /y nul Uninstall.log
    copy /y nul WMIC.log
    copy /y nul Copying.log
    copy /y nul Install.log
    
    :checkifalive  :: Label not needed
    for /F %%I IN (computers.txt) DO ( call DoIt %%i )
    echo all done.
    goto :eof
    
    REM this is the subroutine
    :DoIt
      ping -n 1 %1
      if NOT %errorlevel%==0 (
        echo Machine offline
        goto :EOF
      )
      REM Uninstall
      echo "Uninstalling previous version of Symantec Endpoint Protection"
      psexec \\%1 -s wmic failfast:on product where name="Symantec Endpoint Protection" call uninstall /nointeractive
      if NOT %errorlevel%==0 echo %1  -  %errorlevel% >> Uninstall.log
    
      REM copy
      echo "Finding out which processor is in the machine"
      wmic cpu list brief | findstr /I "86" 
      :: why checking for 86/64 if the code is exactly the same for both? Anyway - here we go:
      if %errorlevel%==0 ( call copy86 ) else ( call copy64 )
    goto :eof
    
    :copy86
      echo "Copying the installation to the local machine"
      copy "C:\installation.exe" \\%1
      if NOT %errorlevel%==0 (
        echo %1  -  %errorlevel% >> Copying.log
      ) else (
        REM Install86
        echo "Installing upgraded Symantec Endpoint Protection"
        psexec \\%1 -s "C:\installation.exe /s"
        if NOT %errorlevel%==0 echo %1  -  %errorlevel% >> Install.log
      )
    goto :eof
    
    :copy64
      echo "Copying the installation to the local machine"
      copy "C:\installation.exe" \\%1
      if NOT %errorlevel%==0( 
        echo %1  -  %errorlevel% >> Copying.log 
      ) else (
        REM Install64
        echo "Installing upgraded Symantec Endpoint Protection"
        psexec \\%1 -s "C:\installation.exe /s"
        if NOT %errorlevel%==0 echo %1  -  %errorlevel% >> Install.log 
      )
    goto :eof
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-26
      • 2010-11-12
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 2018-04-21
      • 2016-04-09
      • 1970-01-01
      相关资源
      最近更新 更多