This page is edited from the 1cmdfaq.txt faq-file contained in
my tscmd.zip
command line interface (CLI) collection. That zipped file has much
additional material, including a number of detached .cmd script
files. It is recommended that you also get the zipped version as a
companion.
Please see "
The Description and
the Index page" for the conditions of usage and other such
information.
115} How can I decompose the path to be one folder per line?
With G(nu)AWK
@echo off & setlocal enableextensions
echo %path%;^
|gawk -F; "{for (i=1;i<=NF-1;++i) printf \"%%s\n\",$i}"
endlocal & goto :EOF
The output might be e.g.
C:\_D\TEST>cmdfaq
c:\_f\xtools
c:\_f\tools
c:\_f\ftools
c:\_e\arczip
C:\WINDOWS\system32
C:\WINDOWS
C:\WINDOWS\System32\Wbem
C:\Program Files\TSEPro
C:\Program Files\010Editor
C:\Program Files\QuickTime\QTSystem\
C:\Program Files\SSH Communications Security\SSH Secure Shell
With
SED
@echo off & setlocal enableextensions
path | sed -e s/.*=// -e s/;/\r\n/g
endlocal & goto :EOF
Or with straight script
@echo off & setlocal enableextensions
for /f "tokens=1-15 delims=;" %%a in ("%path%") do (
if not [%%a]==[] echo %%a
if not [%%b]==[] echo %%b
if not [%%c]==[] echo %%c
if not [%%d]==[] echo %%d
if not [%%e]==[] echo %%e
if not [%%f]==[] echo %%f
if not [%%g]==[] echo %%g
if not [%%h]==[] echo %%h
if not [%%i]==[] echo %%i
if not [%%j]==[] echo %%j
if not [%%k]==[] echo %%k
if not [%%l]==[] echo %%l
if not [%%m]==[] echo %%m
if not [%%n]==[] echo %%n
if not [%%o]==[] echo %%o
)
endlocal & goto :EOF
Exceptionally for this FAQ a pure Visual Basic Scripting (i.e a
non-batch aided solution) is of particular
relevance because the environment in a CLI might differ (you may have
added some folders or other customized the path, as I have done). The
VBS solution gives your regular path.
' SHOWPATH.VBS by Prof. Timo Salmi
' Last modified Wed 11-Jun-2008
'
Sub GetAndShowPath()
dim s, n, m, myPath, rest
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("SYSTEM")
myPath=WshSysEnv("PATH")
m = 1
n = 1
rest = myPath & ";"
Do While n > 0
n = Instr(1,rest,";")
if n = 0 Then Exit Do
s = s & Mid(rest,1,n-1) & vbCrLf
m = n + 1
rest = Mid(rest,m)
Loop
WScript.Echo s & vbCrLf & myPath
End Sub 'GetAndShowPath
'
Sub MainProgram()
GetAndShowPath
End Sub 'MainProgram
'
MainProgram
However, as pointed out by Ralph Brown to me, the actual task can be
done in just one line of pure script:
echo %path:;=&echo.%