PATH变量未在批处理文件中更新

时间:2015-10-28 18:52:40

标签: windows batch-file

首先,感谢您的任何建议。在对例程进行一些调整之后,代码看起来像这样:

REM ========================================================================
REM Parses out Path entries
REM ======================================================================= 
    SETLOCAL
    SET TEMPPATH="%PATH%"
    SET NEWPATH =""
    CALL :parse %TEMPPATH%
    GOTO :NEXT

:PARSE
    SET list=%1
    SET list=%list:"=%
    FOR /F "tokens=1* delims=;" %%a IN ("%list%") DO (
        REM ECHO A: %%a
        REM ECHO B: %%b
        IF NOT "%%a"=="%VAR_JAVA_HOME%" CALL :SUB "%%a"
        IF NOT "%%b"=="" CALL :PARSE "%%b"   
    )
    EXIT /b

:SUB
    ECHO Passed into subroutine: %1
    IF DEFINED NEWPATH IF "%NEWPATH%" == "" (
        SET NEWPATH=%1;
        ECHO IN SUB: %NEWPATH%
    )
    IF DEFINED NEWPATH IF "%NEWPATH:~0,1%, %1" == "" (
        SET NEWPATH=%NEWPATH%;%1
        ECHO Updated NEWPATH: %NEWPATH%
    )
    REM ECHO New path variable: %NEWPATH% 
    EXIT /b

:NEXT
    ECHO New path variable: %NEWPATH% 
    ECHO Removed Java entries from the path...
    ENDLOCAL

例程正在编译并运行。正确解析路径中的各个条目并将其传递到子例程中。但是,新路径未创建或正在更新。

Passed into subroutine: "C:\Windows\system32"
Passed into subroutine: "C:\Windows"
Passed into subroutine: "C:\Windows\System32\Wbem"
Passed into subroutine: "C:\Windows\System32\WindowsPowerShell\v1.0\"
New path variable:<nothing here>

任何评论都将不胜感激。

拉​​斯

1 个答案:

答案 0 :(得分:4)

您永远不会使用if defined NEWPATH输入分支,因为没有名为NEWPATH的变量。您在以下行中有一个多余的空间:

SET NEWPATH =""

因此变量名实际上是NEWPATH (包括末尾的空格)。