批处理文件中的语法错误

时间:2014-02-23 07:35:51

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

现在已经很晚了,我已经厌倦了看这个。有人可以解释一下我在这个脚本中可能有错误的语法吗?

脚本查看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
)

2 个答案:

答案 0 :(得分:1)

您的语法错误是这些需要位于同一行,do

之后有一个空格

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

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

答案 1 :(得分:1)

@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