Question for FOR /F.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Question for FOR /F.

#1 Post by einstein1969 » 06 Jul 2017 10:52

why this not work how expected?

Code: Select all

@echo off
FOR /F "usebackq delims=;" %%1 IN ('aa; bb; c c') DO ECHO %%1


result

Code: Select all

aa bb c c


Einstein1969

IcarusLives
Posts: 161
Joined: 17 Jan 2016 23:55

Re: Question for FOR /F.

#2 Post by IcarusLives » 06 Jul 2017 11:34

I'm not sure why, but these also do not work as expected

Code: Select all

@echo off
FOR /F "usebackq delims=;" %%1 IN (`echo aa; bb; c c`) DO ECHO %%1

Output:
aa bb cc


Code: Select all

@echo off
FOR /F "usebackq tokens=* delims=;" %%1 IN ('aa; bb; c c') DO ECHO %%1

Output:
aa bb cc


Code: Select all

@echo off
FOR /F "usebackq tokens=1 delims=;" %%1 IN ('aa; bb; c c') DO ECHO %%1

Output:
aa bb cc


Code: Select all

@echo off
FOR /F "usebackq tokens=2 delims=;" %%1 IN ('aa; bb; c c') DO ECHO %%1

Output:


dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Question for FOR /F.

#3 Post by dbenham » 06 Jul 2017 12:11

It is absolutely giving the expected behavior :wink:

Everything is explained by the fact that any string of consecutive unquoted and unescaped token delimiters (<space> <tab> , ; = <0xFF>) appearing within IN() clause is converted into a single space. And this conversion occurs before FOR /F tries to parse any tokens.

Code: Select all

@echo off
for /f "usebackq delims=" %%A in (',,,;;;;====     aa,,,;;;;====     bb,,,;;;;====     c,,,;;;;====     c,,,;;;;====     ') do echo [%%A]
--RESULTS--

Code: Select all

[ aa bb c c ]

If you really insist on using USEBACKQ, then you can escape the ;

Code: Select all

@echo off
FOR /F "usebackq delims=;" %%1 IN ('aa^; bb^; c c') DO ECHO %%1
--RESULTS--

Code: Select all

aa

Actually, you should escape all the token delimiters

Code: Select all

@echo off
FOR /F "usebackq delims=;" %%1 IN ('aa^;^ bb^;^ c^ c') DO ECHO %%1
Failure to do so could expose this nasty bug on XP

But why use USEBACKQ :?: :!:
You are simply making life difficult for yourself. All you need to do is drop USEBACK and use double quotes to enclose the string:

Code: Select all

@echo off
FOR /F "delims=;" %%1 IN ("aa; bb; c c") DO ECHO %%1

Prevalent use of USEBACKQ is one of my pet peeves. I've thought long and hard, and done a number of experiments, and the only time USEBACKQ is necessary is when your FOR /F should read a file whose path includes spaces or poison characters, and then you are safely using double quotes within the IN() clause.

There is no reason to ever use USEBACKQ in any other situation.

USEBACKQ - Just say NO! (unless you are reading file content)


Dave Benham

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Question for FOR /F.

#4 Post by einstein1969 » 06 Jul 2017 12:53

thanks Dave!

Post Reply