Remove unwanted path name from %path% variable via batch

前端 未结 4 1584
青春惊慌失措
青春惊慌失措 2020-12-01 05:15

Scope: Windows XP or newer Tools: Batch script

I need to be able to remove an unneeded path name from the system %PATH% variable. I know how to add a new path name

相关标签:
4条回答
  • 2020-12-01 05:49

    After trying SachaDee's answers I got errors with paths like

    C:\Program Files (x86)
    

    with brackets: Program Files (x86)\Directory gave me

    Directorywas unexpected at this time. (no matter what time I tried it)

    I added

    set $line=%$line:)=^^)%
    

    before the for-loop and

    set $newpath=!$newpath:^^=!
    

    after the loop (not sure if it is necessary)

    @echo off
    setlocal EnableDelayedExpansion
    set path
    set $line=%path%
    set $line=%$line: =#%
    set $line=%$line:;= %
    set $line=%$line:)=^^)%
    
    for %%a in (%$line%) do echo %%a | find /i "oracle" || set $newpath=!$newpath!;%%a
    set $newpath=!$newpath:#= !
    set $newpath=!$newpath:^^=!
    set path=!$newpath:~1!
    

    And it is now working.

    0 讨论(0)
  • 2020-12-01 05:59

    You can try something like this :

    @echo off&cls
    setlocal EnableDelayedExpansion
    set $line=%path%
    set $line=%$line: =#%
    set $line=%$line:;= %
    
    for %%a in (%$line%) do echo %%a | find /i "oracle" || set $newpath=!$newpath!;%%a
    set $newpath=!$newpath:#= !
    echo set path=!$newpath:~1!
    

    I putted an echo to the last line. Check the result and If it's OK for you, remove it.

    0 讨论(0)
  • 2020-12-01 05:59

    This removes the substring C:\Program Files (x86)\Git\bin; from the PATH string and re-assigns:

    set PATH=%PATH:C:\Program Files (x86)\Git\bin;=%
    

    You might use this to see the change:

    echo %PATH:C:\Program Files (x86)\Git\bin;=% | tr ; \n
    

    Note: be exact on the substring. It's case-sensitive and slash-sensitive.

    If you need to make it a persistent change use setx instead of set and open another console for changes to take effect.

    setx /M PATH "%PATH:C:\Program Files (x86)\Git\bin;=%"
    
    0 讨论(0)
  • 2020-12-01 06:07

    I found the other solutions to this problem a bit awkward, I don't really want to rely on exact paths, complex 'delayed expansion' syntax, removing spaces for the 'for /f' loop and then adding them back in...

    I think this is more elegant, and I commented the hell out of it so even someone new to the horrors of Batch can follow along.

    ::Turn off command display and allows environmental variables to be overridden for the current session
    @echo off & setlocal
    
    ::Creates a unique file to use for the 'for loop'
    set "TMPFILE="%temp%\tmp%RANDOM%%RANDOM%.txt""
    
    ::Duplicate PATH into OLDPATH
    set "OLDPATH=%PATH%"
    
    ::Declare label for the 'goto' command
    :Loop
    
    ::Extract the first text token with the default delimiter of semicolon
    for /f "tokens=1 delims=;" %%G in ("%OLDPATH%") do (
    
    REM Copy text token to TMPFILE unless what we want to remove is found
    <NUL set /p="%%G" | find /i "StRiNgThAtMaTcHeSwHaTtOrEmOvE" >NUL 2>&1 || <NUL set /p="%%G;" >>%TMPFILE%
    
    REM Remove text token from OLDPATH
    set "OLDPATH=%OLDPATH:*;=%"
    )
    
    ::Repeat loop until OLDPATH no longer has any delimiters, and then add any remaining value to TMPFILE
    echo %OLDPATH% | findstr /C:";" >NUL && (goto :Loop) || <NUL set /p="%OLDPATH%" >>%TMPFILE%
    
    ::Set the path to TMPFILE
    for /f "usebackq delims=" %%G in (%TMPFILE%) do (set "PATH=%%G")
    
    ::Clean-up
    del %TMPFILE% >NUL 2>&1
    
    ::An echo and pause just for debug purposes
    echo %PATH%
    pause
    
    0 讨论(0)
提交回复
热议问题