How can I use a .bat file to remove specific tokens from the PATH environment variable?

前端 未结 2 1538
北海茫月
北海茫月 2020-11-29 13:40

I am writing an uninstallation script, so I would like to \'undo\' modifications the installation made to the system. In achieving this goal, I would like to parse the

2条回答
  •  一个人的身影
    2020-11-29 14:35

    This command only outputs the first token, and no subsequent tokens are echoed.

    FOR /F "delims=;" %%A IN (%TEMP_PATH%) DO ECHO %%A 
    

    How can I loop through an unknown number of tokens and work with each one?

    Use the following batch file.

    SplitPath.cmd:

    @echo off
    setlocal
    for %%a in ("%path:;=";"%") do (
      echo %%~a
      )
    endlocal
    

    Example Output:

    F:\test>path
    PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\apps\WSCC\Sysinternals Suite;C:\apps\WSCC\NirSoft Utilities
    
    F:\test>splitpath
    C:\Windows\system32
    C:\Windows
    C:\Windows\System32\Wbem
    C:\Windows\System32\WindowsPowerShell\v1.0\
    C:\apps\WSCC\Sysinternals Suite
    C:\apps\WSCC\NirSoft Utilities
    

    Notes:

    • Modify the for loop as appropriate to implement the rest of your pseudocode.

    Further Reading

    • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
    • for - Conditionally perform a command several times.
    • variable edit/replace - Edit and replace the characters assigned to a string variable.

提交回复
热议问题