In a batch file, is it possible to replace this use of SED and TR with a for loop?

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 sed.exe and tr.exe on UnxUtils . I would like to do the same using only the built-in commands and programs that are included in Windows. Can I do it? Tips?

0
source share
5 answers
setlocal
SET _Path="%Path:;=";"%"
FOR %%a IN (%_Path%) DO ECHO     %%~a
endlocal
+1
source

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"
)
+1
source

- . , :

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

, Windows XP, 2003 Server .

+1

:

@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 they decided that it was easier:

setlocal
set _path="%PATH:;=" "%"
for %%p in (%_path%) do if not "%%~p"=="" echo     %%~p
endlocal
0
source

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
0
source

All Articles