开发者

Parse PATH using FOR /F in BAT script

开发者 https://www.devze.com 2023-01-23 10:40 出处:网络
I need to parse the %PATH% list in a .BAT script, but I\'m not having much luck with paths that contain spaces.

I need to parse the %PATH% list in a .BAT script, but I'm not having much luck with paths that contain spaces.

for %%a in (%PATH%) do @echo %%a

The above parses on spaces (default), but I need to开发者_C百科 parse on semi-colons. I'm trying to use this but it's throwing me an error:

for /f "tokens=* delims=;" %%a in (%PATH%) do @echo %%a

The result is one line: "The system cannot find the file C:\Windows\system32."

I'm sure I'm missing something very basic but any help would be greatly appreciated. TY!


SET TempPath="%Path:;=";"%"
FOR %%a IN (%TempPath%) DO echo.%%~a


To do this correctly, you need something more complicated than a simple FOR. Try the following in a batch file:

@ECHO OFF
SET TEMPPATH=%PATH%
:PARSE_START
IF "%TEMPPATH%"=="" GOTO EXIT
FOR /F "tokens=1* delims=;" %%a in ("%TEMPPATH%") Do ECHO %%a
FOR /F "tokens=1* delims=;" %%a in ("%TEMPPATH%") Do SET TEMPPATH=%%b
GOTO PARSE_START
:EXIT
0

精彩评论

暂无评论...
验证码 换一张
取 消