How set "delims=quote... in FOR command?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Aacini
Expert
Posts: 1927
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

How set "delims=quote... in FOR command?

#1 Post by Aacini » 29 Jan 2013 08:05

I am pretty sure this question has been answered before, but I can't find it!

I have a file with this line of text:

Code: Select all

sometext name1="first value" name2="second value" more text
I want to take the values enclosed in quotes. I try to escape the quote, but the next command issue a syntax error:

Code: Select all

for /F "tokens=2,4 delims=^"" %%a in (thefile.txt) do set v1=%%a& set v2=%%b
What is the right way to write this? Thanks!

Antonio

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How set "delims=quote... in FOR command?

#2 Post by Squashman » 29 Jan 2013 08:08

Here is the general syntax for quote delimiting

Code: Select all

for /f tokens^=2^ delims^=^" %%p in (file.txt) do echo "%%p"

Aacini
Expert
Posts: 1927
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How set "delims=quote... in FOR command?

#3 Post by Aacini » 29 Jan 2013 09:44

Squashman wrote:Here is the general syntax for quote delimiting

Code: Select all

for /f tokens^=2^ delims^=^" %%p in (file.txt) do echo "%%p"

You forgot to include the second token and escape its comma! :mrgreen:

Code: Select all

for /f tokens^=2^,4^ delims^=^" %%p in (file.txt) do echo "%%p %%q"
Thanks a lot!

Antonio

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How set "delims=quote... in FOR command?

#4 Post by Squashman » 27 Oct 2014 12:33

Hmm, I just realized I can't get this to work with the USEBACKQ option should your file name contain spaces.

This does not work

Code: Select all

for /f usebackq tokens^=2^ delims^=^" %%p in ("file 1.txt") do echo "%%p"

Aacini
Expert
Posts: 1927
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How set "delims=quote... in FOR command?

#5 Post by Aacini » 27 Oct 2014 12:49

Squashman wrote:Hmm, I just realized I can't get this to work with the USEBACKQ option should your file name contain spaces.

This does not work

Code: Select all

for /f usebackq tokens^=2^ delims^=^" %%p in ("file 1.txt") do echo "%%p"


You missed to escape the space!:

Code: Select all

for /f usebackq^ tokens^=2^ delims^=^" %%p in ("file 1.txt") do echo "%%p"
               |
               This one!


Antonio

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How set "delims=quote... in FOR command?

#6 Post by Squashman » 27 Oct 2014 13:00

You forgot the smiley. :mrgreen:

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How set "delims=quote... in FOR command?

#7 Post by Squashman » 27 Oct 2014 13:02

Just before you posted I had changed it to this.

Code: Select all

for /f tokens^=2^ delims^=^" %%p in ('type "file 1.txt"') do echo "%%p"

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How set "delims=quote... in FOR command?

#8 Post by Ed Dyreen » 16 Nov 2014 16:23

Aacini wrote:You missed to escape the space!:

Code: Select all

for /f usebackq^ tokens^=2^ delims^=^" %%p in ("file 1.txt") do echo "%%p"
               |
               This one!

or not use a space at all :mrgreen:

Code: Select all

for /f usebacktokens^=2delims^=^" %%p in ("file 1.txt") do echo "%%p"

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How set "delims=quote... in FOR command?

#9 Post by penpen » 17 Nov 2014 07:00

You have also found a 'feature': The above for-loop-type works without the q in "usebackq" (at least using win xp home 32 bit).
I wonder if there is a difference between "useback" and "usebackq".
It seems there is none, but maybe if a difference exists, then i haven't found it.

penpen

Aacini
Expert
Posts: 1927
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How set "delims=quote... in FOR command?

#10 Post by Aacini » 17 Nov 2014 10:00

penpen wrote:You have also found a 'feature': The above for-loop-type works without the q in "usebackq" (at least using win xp home 32 bit).
I wonder if there is a difference between "useback" and "usebackq".
It seems there is none, but maybe if a difference exists, then i haven't found it.

penpen


I discovered such "usebackq" synonym some time ago, when I was looking for additional internal commands inside cmd.exe file itself! :D Details at this post.

Antonio

Post Reply