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

Batch file 通过批处理文件设置环境变量意外退出,batch-file,Batch File,我正在创建一个批处理文件来设置JSHOP2的环境变量。代码接受两个用户输入,并将它们与现有的类路径变量进行比较,如果不存在,则设置环境变量。 这是我的密码: @echo off color 0a echo ............................Welcome to path to shop............................ echo .... Make sure you provide exact paths with full file name wh

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

@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

IF
语句必须全部位于同一行或跨多行使用括号

更新

  • 添加引号以处理路径中的空格
  • IF
    语句中添加了
    /I
    选项,用于区分大小写
您可以这样做:

if /I "%@specifiedPath%" == "%CLASSPATH%" goto isAlreadySet
goto isNotAlreadySet
或者这个:

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

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

@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

再次感谢:)

它什么也没做:/还是一样的问题。。。当我在进入第二条路径后按enter键时,它会崩溃。您是通过双击还是通过命令提示符运行此操作?打开命令提示符并运行它,以便查看错误消息。我已经创建了一个process.bat文件,正在编辑它。我通过双击它来运行它。打开命令提示符并运行process.bat文件。窗口将保持打开状态,以便您可以看到错误消息。非常感谢亲爱的。我没有足够的声誉来投票,否则你应得的:)在删除行后的空间,在行>代码> SETUnjialEddie= %ANTLRPATH %;jshop2Path%。它应该是
@specifiedPath=%antlrPath%;%jshop2Path%
没有空格。谢谢Ken您的建议:)