Remove unwanted path name from %path% variable via batch

前端 未结 4 1639
青春惊慌失措
青春惊慌失措 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.

提交回复
热议问题