Page 1 of 1

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

Posted: 29 Jan 2013 08:05
by Aacini
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

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

Posted: 29 Jan 2013 08:08
by Squashman
Here is the general syntax for quote delimiting

Code: Select all

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

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

Posted: 29 Jan 2013 09:44
by Aacini
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

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

Posted: 27 Oct 2014 12:33
by Squashman
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"

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

Posted: 27 Oct 2014 12:49
by Aacini
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

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

Posted: 27 Oct 2014 13:00
by Squashman
You forgot the smiley. :mrgreen:

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

Posted: 27 Oct 2014 13:02
by Squashman
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"

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

Posted: 16 Nov 2014 16:23
by Ed Dyreen
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"

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

Posted: 17 Nov 2014 07:00
by penpen
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

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

Posted: 17 Nov 2014 10:00
by Aacini
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