批处理文件如果存在,则执行所有循环

批处理文件如果存在,则执行所有循环

我正在执行一个批处理文件,它检查 Java\jre6 是否存在,然后将执行一种命令,最后执行一个 else 语句,但我发现所有的语句都一起执行。

echo.
echo. 
GOTO:Java

:Java
IF EXIST C:\Program Files\Java\jre6(
echo. Java Version 1.6 present
start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Java 6 thereeee"

if "%PROCESSOR_ARCHITECTURE%"=="x86" (
XCOPY "%CD%\Sec\*.*"  "%ProgramFiles%\Java\jre6\lib\security\" /s /Y
XCOPY "%CD%\Jar\*.*"  "%ProgramFiles%\Java\jre6\lib\ext\" /s /Y
)
else(
XCOPY "%CD%\Sec\*.*"  "%ProgramFiles(x86)%\Java\jre6\lib\security\" /s /Y
XCOPY "%CD%\Jar\*.*"  "%ProgramFiles(x86)%\Java\jre6\lib\ext\" /s /Y
)
start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Your Settings have been installed successfully for 6"
)

IF EXIST C:\Program Files\Java\jre7(
echo. Java Version 1.7 present
start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Java 7 thereeee"
if "%PROCESSOR_ARCHITECTURE%"=="x86" (
XCOPY "%CD%\Jar\*.*"  "%ProgramFiles%\Java\jre7\lib\ext\" /s /Y
XCOPY "%CD%\Sec7\*.*"  "%ProgramFiles%\Java\jre7\lib\security\" /s /Y
)
else
(
XCOPY "%CD%\Jar\*.*"  "%ProgramFiles(x86)%\Java\jre7\lib\ext\" /s /Y
XCOPY "%CD%\Sec7\*.*"  "%ProgramFiles(x86)%\Java\jre7\lib\security\" /s /Y
)
start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Your Settings have been installed successfully for 7"
)

IF EXIST C:\Program Files\Java\jre8(
echo. Java Version 1.8 present
start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Java 8 thereeee"
if "%PROCESSOR_ARCHITECTURE%"=="x86" (
XCOPY "%CD%\Sec\*.*"  "%ProgramFiles%\Java\jre8\lib\security\" /s /Y
XCOPY "%CD%\Jar\*.*"  "%ProgramFiles%\Java\jre8\lib\ext\" /s /Y
)
else(
XCOPY "%CD%\Sec\*.*"  "%ProgramFiles(x86)%\Java\jre8\lib\security\" /s /Y
XCOPY "%CD%\Jar\*.*"  "%ProgramFiles(x86)%\Java\jre8\lib\ext\" /s /Y
)
start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Your Settings have been installed successfully for 8"
) 
ELSE (
echo. Java NOT present
start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Java is not Installed In your system , install Java 1.8 and try to execute settings again"
GOTO:EXIT
)

:EXIT

答案1

必须解决的关键问题:

  • 如果 IF EXIST 文件名包含空格(或类似的有害字符&),则必须用引号引起来。
  • (开始代码块时必须有一个空格
  • )ELSE(必须全部出现在同一行
  • 您的逻辑是错误的 - 除非存在 1.8 版本,否则它将始终执行“Java NOT present”代码。您必须将整个构造变成一个大的 IF ... ELSE IF ... 链,或者在每个 IF 块的末尾使用 GOTO :SomeLabel

其他建议:

  • ECHO.不可靠。在特殊情况下,它可能会失败。最好养成使用 的习惯ECHO(,因为它总是有效的。我知道它看起来“不对劲”,就像它会混淆代码块一样,但它实际上非常可靠。
  • ECHO(仅当您没有其他空格(或可能只有空格)时才需要 ECHOED。但始终使用这种形式肯定没有坏处。
  • 同一台机器上可以安装多个 JRE 版本。您可以考虑颠倒顺序,以便始终准备找到的最新版本。
  • 除非您有未在顶部显示的附加代码,否则不需要 GOTO :JAVA
  • 除非在 :EXIT 之后有附加代码,否则最好使用GOTO :EOFEXIT /B直接退出脚本,而不需要在底部添加标签。
  • 缩进是你的朋友。它有助于看清代码的逻辑 - 使代码更容易理解。
echo(
echo( 
GOTO:Java

REM Presumably the GOTO above is conditional, and there is more code that goes here

:Java
IF EXIST "C:\Program Files\Java\jre6" (
  echo( Java Version 1.6 present
  start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Java 6 thereeee"

  if "%PROCESSOR_ARCHITECTURE%"=="x86" (
    XCOPY "%CD%\Sec\*.*"  "%ProgramFiles%\Java\jre6\lib\security\" /s /Y
    XCOPY "%CD%\Jar\*.*"  "%ProgramFiles%\Java\jre6\lib\ext\" /s /Y
  ) else (
    XCOPY "%CD%\Sec\*.*"  "%ProgramFiles(x86)%\Java\jre6\lib\security\" /s /Y
    XCOPY "%CD%\Jar\*.*"  "%ProgramFiles(x86)%\Java\jre6\lib\ext\" /s /Y
  )
  start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Your Settings have been installed successfully for 6"
) else IF EXIST "C:\Program Files\Java\jre7" (
  echo( Java Version 1.7 present
  start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Java 7 thereeee"
  if "%PROCESSOR_ARCHITECTURE%"=="x86" (
    XCOPY "%CD%\Jar\*.*"  "%ProgramFiles%\Java\jre7\lib\ext\" /s /Y
    XCOPY "%CD%\Sec7\*.*"  "%ProgramFiles%\Java\jre7\lib\security\" /s /Y
  ) else (
    XCOPY "%CD%\Jar\*.*"  "%ProgramFiles(x86)%\Java\jre7\lib\ext\" /s /Y
    XCOPY "%CD%\Sec7\*.*"  "%ProgramFiles(x86)%\Java\jre7\lib\security\" /s /Y
  )
  start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Your Settings have been installed successfully for 7"
) else IF EXIST "C:\Program Files\Java\jre8" (
  echo( Java Version 1.8 present
  start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Java 8 thereeee"
  if "%PROCESSOR_ARCHITECTURE%"=="x86" (
    XCOPY "%CD%\Sec\*.*"  "%ProgramFiles%\Java\jre8\lib\security\" /s /Y
    XCOPY "%CD%\Jar\*.*"  "%ProgramFiles%\Java\jre8\lib\ext\" /s /Y
  ) else (
    XCOPY "%CD%\Sec\*.*"  "%ProgramFiles(x86)%\Java\jre8\lib\security\" /s /Y
    XCOPY "%CD%\Jar\*.*"  "%ProgramFiles(x86)%\Java\jre8\lib\ext\" /s /Y
  )
  start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Your Settings have been installed successfully for 8"
) ELSE (
  echo( Java NOT present
  start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Java is not Installed In your system , install Java 1.8 and try to execute settings again"
  goto :EXIT
)
REM Presumably there is more code here

:EXIT
REM Presumably there is cleanup code here

我还可以做很多事情来改进代码,但是现在这就足够了。

相关内容