Hai Vu
Hai Vu

Reputation: 40723

Batch file - for loop only prints out the first item

Here is my batch file, try.cmd:

for /f "delims=;" %%d in ("%PATH%") do echo %%d

Curiously, only the first directory in the path got printed, then the loop stopped. How do I get to loop over all directories in the path?

Upvotes: 1

Views: 2410

Answers (3)

Magoo
Magoo

Reputation: 80033

You mean

@ECHO OFF
SETLOCAL
ECHO %path:;=&ECHO(%

?

Your version doesn't work because for /f is required to invoke the delims= facility, but that means that there's only one input "line"; the for command will iterate through a sequence, but that means there's no delims available...

Upvotes: 3

Yasuo Ohno
Yasuo Ohno

Reputation: 103

@SETLOCAL
@ECHO OFF
SET "P=%PATH%"
:EXTRACT_LOOP
for /f "tokens=1* delims=;" %%p in ("%P%") do (echo %%p & SET P=%%q)
IF NOT "%P%" == "" GOTO :EXTRACT_LOOP

There might be another smart way.

Upvotes: 2

Monacraft
Monacraft

Reputation: 6630

Try this:

Printf.bat

@echo off
:loop
Echo %~1
shift /1
if "%~1" NEQ "" goto loop

Cmd

C:\>Printf.bat 1 2 3 4 5 6
1
2
3
4
5
6

C:\>set test=A;B;C;D
C:\>Printf.bat %test:;= %
A
B
C
D

C:\>Printf.bat %Path:;= %
...

C:\>

Upvotes: 0

Related Questions