0

the batch file:

@echo.
@set curdrive=%~d0
@path | %curdrive%\utils\sed -e "s/PATH=//" | %curdrive%\utils\tr ; \n
@echo.

Sample output (one path element on each line):

C:\cheeso\bin
C:\Perl\bin
c:\utils
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
c:\Program Files\Microsoft SQL Server\90\Tools\binn\
c:\.net3.5
c:\.net2.0
c:\vs2008\common7\IDE
c:\netsdk2.0\bin

This batch file depends on the sed.exe and the tr.exe from UnxUtils. I'd like to do the same thing using only built-in commands and programs that are included with Windows. Can I do it? Hints?

Cheeso
  • 189,189
  • 101
  • 473
  • 713

5 Answers5

1

Warning, abuse of recursion ahead:

@echo off

call :one "%PATH%"
goto :eof

:one
for /f "tokens=1,* delims=;" %%i in (%1) do (
    echo %%i
    if not "%%j"=="" call :one "%%j"
)
Adam Mitz
  • 6,025
  • 1
  • 29
  • 28
  • 1
    User "Jay" has a great alternative here: http://stackoverflow.com/questions/817280/how-does-for-work-in-cmd-batch-file/1293667#1293667 – Adam Mitz Sep 15 '09 at 04:44
1

Here's a non-recursive version. Not really better, but I felt like looking for another solution:

SETLOCAL ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
:again
FOR /F "delims=;" %%I IN ("%PATH%") DO ECHO %%I & SET PATH=!PATH:%%I;=!
IF DEFINED PATH GOTO :again
ENDLOCAL

I believe that this will only work for Windows XP, 2003 Server, and newer.

D.Shawley
  • 58,213
  • 10
  • 98
  • 113
  • Did you try running this? It just prints the first value in an infinite loop. The "for" loop does not re-evaluate the expression in parens. – Adam Mitz Sep 15 '09 at 03:57
  • @Adam - yes, it works fine on Windows 2003. You are correct in that the `FOR` loop outputs the first item. Then it removes the item from `PATH`. The `IF DEFINED` is executed whenever the path is not empty. Be careful when typing the `SET` statement. – D.Shawley Sep 15 '09 at 15:00
  • Much better than the UnxUtils version, and I like it better than the recursive evrsion, too. But it didn't work for me, until I appended a final terminating ; to the path. – Cheeso Sep 15 '09 at 19:28
  • I copied your script exactly into a cmd file and ran it on my machine (Vista). It didn't work. Try an example where %PATH% doesn't end in ;. I was mistaken before, it's repeating the last element, not the first (happened to be the same on my system). – Adam Mitz Sep 16 '09 at 00:11
1
setlocal
SET _Path="%Path:;=";"%"
FOR %%a IN (%_Path%) DO ECHO     %%~a
endlocal
Anders
  • 97,548
  • 12
  • 110
  • 164
0

I started to use this:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
set tpath=%path%;
echo.
:again
FOR /F "delims=;" %%I IN ("%TPATH%") DO (
  echo    %%I 
  set TPATH=!TPATH:%%I;=!
)
IF DEFINED TPATH GOTO :again

ENDLOCAL

But then decided this was simpler:

setlocal
set _path="%PATH:;=" "%"
for %%p in (%_path%) do if not "%%~p"=="" echo     %%~p
endlocal
Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • Credit where credit is due. The simpler one is the example from Jay here: http://stackoverflow.com/questions/817280/how-does-for-work-in-cmd-batch-file/1293667#1293667 – Adam Mitz Sep 16 '09 at 00:13
0

I tried this on my Windows 2003 server and it worked. Here is the contents of my showpath.cmd:

@echo off
for %%p in (%PATH%) do echo %%p
Hai Vu
  • 37,849
  • 11
  • 66
  • 93
  • That doesn't work for my path, because my path includes directory names that contain spaces. So "C:\Program Files\Microsoft Team Foundation Server 2008 Power Tools\" gets displayed on 8 lines. – Cheeso Sep 15 '09 at 20:08