我正在執行一個批次文件,該文件檢查 Java\jre6 是否存在,然後將執行一種類型的命令,最後是一個 else 語句,但我發現所有 stmts 一起執行。
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(
僅當您除了 ECHO 的空白(或可能只有空白)之外沒有任何內容時才需要。但始終使用這種形式肯定沒有什麼壞處。- 可在同一台電腦上安裝多個 JRE 版本。您可以考慮顛倒順序,以便它始終準備好找到的最新版本。
- 除非您有其他程式碼未顯示在頂部附近,否則不需要 GOTO :JAVA
- 除非您在 :EXIT 之後有其他程式碼,否則最好使用
GOTO :EOF
或EXIT /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
我還可以做很多事情來改進程式碼,但現在就足夠了。