JSI Tip 9055. What command will return the folders in my PATH, one per line?


Making use of Environment Variable string substitution syntax, and the FOR %variable IN (set) DO command command, you can use the command-line, or a batch, to display the folders in your %PATH%, one per line:

Command-line

for %f in ("%PATH:;=" "%") do @echo %f

Batch

for %%f in ("%PATH:;=" "%") do @echo %%f

If you wish to also strip the encapsulating quote marks ("), you can add parameter parsing:

Command-line

for %f in ("%PATH:;=" "%") do @echo %~f

Batch

for %%f in ("%PATH:;=" "%") do @echo %%~f

Examples:

If your %PATH% contains:
C:\Program Files\Windows Resource Kits\Tools\;C:\WIN_ONE;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\UTIL

Typing for %f in ("%PATH:;=" "%") do @echo %f would return:
"C:\Program Files\Windows Resource Kits\Tools\"
"C:\WIN_ONE"
"C:\WINDOWS\system32"
"C:\WINDOWS"
"C:\WINDOWS\System32\Wbem"
"C:\UTIL"
while typing for %f in ("%PATH:;=" "%") do @echo %~f would return:
C:\Program Files\Windows Resource Kits\ToolsC:\WIN_ONE
C:\WINDOWS\system32
C:\WINDOWS
C:\WINDOWS\System32\Wbem
C:\UTIL

NOTE: What is happening with ("%PATH:;=" "%"):

1. The leading " inserts a quote mark at the beginning of the set.

2. The %PATH:;=" "% replaces all ;s with " ", defining the end of each folder path and the beginning of the next.

3. The trailing " inserts a quote mark at the end of the set.



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish