通过批处理文件设置环境变量意外退出

时间:2014-11-26 14:58:17

标签: batch-file

我正在创建一个批处理文件来为JSHOP2设置环境变量。代码接受两个用户输入并将它们与现有的ClassPath变量进行比较,如果它不存在则设置环境变量。 这是我的代码:

@echo off
color 0a
echo ............................Welcome to path to shop............................ 
echo .... Make sure you provide exact paths with full file name when prompted.
echo .... Once given the path of the file make sure you don't change the location of the file in        your computer. 
set /p "antlrPath=Enter the path of the antlr.jar file in your computer: "
set /p "jshop2Path=Enter the path of the compiled JSHOP2 source file (SomeName.jar) in your  computer: "
set @specifiedPath= %antlrPath%;%jshop2Path%
echo %@specifiedPath% %CLASSPATH% ::code runs fine upto this line then cmd exits unexpectedly
if %@specifiedPath% == %CLASSPATH% 
goto isAlreadySet
goto isNotAlreadySet
:isAlreadySet
echo yesss!
:isNotAlreadySet
echo no
pause

有什么问题?

编辑:根据aphoria的要求更新代码。

@echo off
color 0a
echo ............................Welcome to path to shop............................ 
echo .... Make sure you provide exact paths with full file name when prompted.
echo .... Once given the path of the file make sure you don't change the location of the file in    your computer.
set /p "antlrPath=Enter the path of the antlr.jar file in your computer: "
set /p "jshop2Path=Enter the path of the compiled JSHOP2 source file (SomeName.jar) in your  computer: " 
set @specifiedPath= %antlrPath%;%jshop2Path%
echo %@specifiedPath% %CLASSPATH%
if "%@specifiedPath%" == "%CLASSPATH%" (
goto isAlreadySet
) ELSE (
goto isNotAlreadySet
)
:isAlreadySet
echo yesss!
:isNotAlreadySet
echo no
pause

2 个答案:

答案 0 :(得分:1)

谢谢你们的支持。我的问题终于解决了。对于所有在这里遇到此问题的人来说,这是我的最终代码。

@echo off
color 0a
echo ............................Welcome to path to shop............................ 
echo .... Make sure you provide exact paths with full file name when prompted.
echo .... Once given the path of the file make sure you don't change the location of the file in your computer.
set /p "antlrPath=Enter the path of the antlr.jar file in your computer: "
set /p "jshop2Path=Enter the path of the compiled JSHOP2 source file (SomeName.jar) in your computer: "
set @specifiedPath=%antlrPath%;%jshop2Path%
echo %@specifiedPath% %CLASSPATH%
if "%@specifiedPath%" == "%CLASSPATH%" (
goto isAlreadySet
) ELSE (
goto isNotAlreadySet
)
:isAlreadySet
echo yesss!
goto end
:isNotAlreadySet
echo no
:end
pause

再次感谢:)

答案 1 :(得分:0)

IF语句必须全部在同一行,或者在多行中使用括号。

<强>更新

  • 添加引号以处理路径中的空格。
  • /I语句添加了IF选项,以防止不区分大小写。

你可以这样做:

if /I "%@specifiedPath%" == "%CLASSPATH%" goto isAlreadySet
goto isNotAlreadySet

或者这个:

if /I "%@specifiedPath%" == "%CLASSPATH%" (
  goto isAlreadySet
) ELSE (
  goto isNotAlreadySet
)