Windows 批处理文件嵌套 IF 语句运行 IF 和 THEN

Windows 批处理文件嵌套 IF 语句运行 IF 和 THEN

不确定为什么这个嵌套的 IF 语句无法正常工作。我找不到任何问题。最后一个代码块实际上可以正常工作,不确定为什么第一个代码块不能正常工作。

第一个脚本正在运行第一个 tracert,但同时也打印出主 IF 语句的 ELSE 命令。我的脚本中相同代码的第二个实例(完全相同,只是使用 sCSHOSTNAME 而不是 sCSCLIENTNAME)仅打印出主 IF 语句的 else 命令。

sCSCLIENTNAME is a remote server address
sCSHOSTNAME is a remote server address
dBASEHOME is the root folder where the cvping file exists
DIAGFolder is a destination folder where all of the outputs are being dumpe

tracert 线路(带有一堆变量的线路)单独工作得很好,省去了那些线路,不会让它太长。

IF EXIST sCSCLIENTNAME (
   ECHO Checking network connectivity to %sCSCLIENTNAME%...
   ::cvping
   IF EXIST "%dBASEHOME%\cvping.exe" (
     "%dBASEHOME%\cvping" %sCSCLIENTNAME%> "%DIAGFolder%\cvping.txt"
   ) ELSE (
     @ECHO CVPing test skipped due to cvping not being found>> "%DIAGFolder%\cvping.txt"
   )
   ::ping
   ping %sCSCLIENTNAME%>> "%DIAGFolder%\PingResults.txt"
   :: Traceroute
   (9>"%lockFile%" tracert %sCSCLIENTNAME%>> "%DIAGFolder%\traceroute.txt") | <nul >nul 2>&1 (cmd /v /q /c "%loop% ( %wait% & %rotateSpin% & (( %check% )&&( %atEnd% )||( %progress% )))")
) ELSE (
  ECHO sCSCLIENTNAME not defined in registry, skipping tests to that value...>> "%DIAGFolder%\PingResults.txt"
  ECHO sCSCLIENTNAME not defined in registry, skipping tests to that value...>> "%DIAGFolder%\traceroute.txt"
)

奇怪的是,下面的方法确实有效:

IF DEFINED dBASEHOME (
   ECHO\
   ECHO Checking network connectivity to %sCSHOSTNAME%...
   ::CVPING
   IF EXIST "%dBASEHOME%\cvping.exe" "%dBASEHOME%\cvping" %sCSHOSTNAME%> "%DIAGFolder%\cvping.txt"
   ::ping
   ping %sCSHOSTNAME%>> "%DIAGFolder%\PingResults.txt"
   :: Traceroute
   (9>"%lockFile%" tracert %sCSHOSTNAME%>> "%DIAGFolder%\traceroute.txt") | <nul >nul 2>&1 (cmd /v /q /c "%loop% ( %wait% & %rotateSpin% & (( %check% )&&( %atEnd% )||( %progress% )))")
   ECHO\
   ECHO Checking network connectivity to %sCSCLIENTNAME%...
   IF EXIST "%dBASEHOME%\cvping.exe" "%dBASEHOME%\cvping" %sCSCLIENTNAME%> "%DIAGFolder%\cvping.txt"
   ping %sCSCLIENTNAME%>> "%DIAGFolder%\PingResults.txt"
   (9>"%lockFile%" tracert %sCSCLIENTNAME%>> "%DIAGFolder%\traceroute.txt") | <nul >nul 2>&1 (cmd /v /q /c "%loop% ( %wait% & %rotateSpin% & (( %check% )&&( %atEnd% )||( %progress% )))")
   ECHO\
) ELSE (
   ECHO No defined servers found, skipping network connectivity tests.
   ECHO Traceroutes not run due to Application not appearing to be installed>  "%DIAGFolder%\No_Tracert.txt"
)

我从底部代码开始,然后将其拆分为 2 个 IF 语句,分别检查 sCSCLIENTNAME 和 sCSHOSTNAME(我只放置了上面的一个 IF 语句,另一个完全相同,但是是不同的变量),但也在 cvping 行中放置了一个 IF 语句来确定该文件是否首先存在。

自包含代码示例...

@ECHO OFF
cls

SET sCSCLIENTNAME=localhost
SET sCSHOSTNAME=www.google.com
SET dBASEHOME=%SystemRoot%\system32
SET DIAGFolder=%temp%\%computername%
rmdir "%DIAGFolder%" /s /q >nul 2>&1
mkdir "%DIAGFolder%" >nul 2>&1

IF EXIST sCSHOSTNAME (
   ECHO\
   ECHO Checking network connectivity to %sCSHOSTNAME%...
   ::CVPING
   IF EXIST "%dBASEHOME%\cvping.exe" (
    "%dBASEHOME%\cvping" %sCSHOSTNAME%> "%DIAGFolder%\cvping.txt"
   ) ELSE (
    @ECHO CVPing test to %sCSHOSTNAME% skipped due to cvping not being found> "%DIAGFolder%\cvping.txt"
   )
   ::ping
   ping %sCSHOSTNAME%>> "%DIAGFolder%\PingResults.txt"
   :: Traceroute
   tracert %sCSHOSTNAME%>> "%DIAGFolder%\traceroute.txt"
) ELSE (
  ECHO sCSHOSTNAME not defined in registry, skipping tests to that value...>> "%DIAGFolder%\PingResults.txt"
  ECHO sCSHOSTNAME not defined in registry, skipping tests to that value...>> "%DIAGFolder%\traceroute.txt"
)

IF EXIST sCSCLIENTNAME (
   ECHO Checking network connectivity to %sCSCLIENTNAME%...
   ::CVPING
   IF EXIST "%dBASEHOME%\cvping.exe" (
     "%dBASEHOME%\cvping" %sCSCLIENTNAME%> "%DIAGFolder%\cvping.txt"
   ) ELSE (
     @ECHO CVPing test skipped due to cvping not being found>> "%DIAGFolder%\cvping.txt"
   )
   ::ping
   ping %sCSCLIENTNAME%>> "%DIAGFolder%\PingResults.txt"
   :: Traceroute
   tracert %sCSCLIENTNAME%>> "%DIAGFolder%\traceroute.txt"
) ELSE (
  ECHO sCSCLIENTNAME not defined in registry, skipping tests to that value...>> "%DIAGFolder%\PingResults.txt"
  ECHO sCSCLIENTNAME not defined in registry, skipping tests to that value...>> "%DIAGFolder%\traceroute.txt"
)
pause

相关内容