How do I specify the " character as a delimiter in FOR

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
melliott
Posts: 1
Joined: 06 Feb 2008 13:05

How do I specify the " character as a delimiter in FOR

#1 Post by melliott » 06 Feb 2008 13:13

I want to parse a string with FOR /F where the delimiter character is the double-quote character (i.e. ")

for example, the contentes of C:\temp.out is:

name = "Joe"


I want to use something like

FOR /F "tokens=1,2 delims=^"" %%I in (temp.out) do @echo =%%J

and I'd like it to print

Joe


But I can't get FOR to treat " as a delimiter. I tried to escape it with ^", but no luck.

Any ideas?

Thanks.

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#2 Post by jeb » 06 Feb 2008 15:29

Hi melliott,

I can see your problem, but can't solve it a "nice" way, but I can solve it in two steps.

First reading a complete line,
then replacing all " to another "unsued" character (I use #),
then splitting it with another FOR /F.

Code: Select all

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SETLOCAL ENABLEEXTENSIONS

FOR /F "tokens=*" %%I in (temp.out) do (
  set par=%%I
  set por=!par:"=#!
  for /F "tokens=1,2 delims=#" %%J in ("!por!") DO echo %%K
)


bye
Jan Erik

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#3 Post by DosItHelp » 06 Feb 2008 21:41

If the parsing allows '=' and ' ' as delimiters then you could use the tilde to remove quotes from the resulting string in %%J. I.e.:

Code: Select all

FOR /F "tokens=1,2 delims== " %%I in (temp.out) do @echo.%%~J

Post Reply