How to use a double quote as delimiter in a FOR loop

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

How to use a double quote as delimiter in a FOR loop

Post by MigrationUser »

30 Jul 2009 02:48
aariel_us

Any idea how to use a double quote as a delim character?

It appears that the only way to do it is with:

Code: Select all

delims=*"
But that uses all the delimiter characters and has unintended consequences.

I tried these two, but they don't work:

Code: Select all

delims=""
delims=^""
Apparently the command parser is reading the " delim character as the close quote
on "token... delim=..."

Code: Select all

FOR /F "tokens=2,6,8 delims="" %%G IN (.\foobar.txt) DO ECHO "%%G" "%%H" "%%I"
Here is one line of the data:

Code: Select all

foo("foobar","Foo Bar"){"FOOBAR"="cn=Foo Bar,ou=Foo,dc=foo,dc=bar"}
I realize trying to use double quotes as a delimiter is a bad idea, but it does work for my data and I really don't want to have to "clean" my data first if there is someway to get double quotes to be a delimiter. :?

----------------------------

#2 30 Jul 2009 10:52
bluesxman

I'm going to go out on a limb here and say I don't think it's possible.

For that line of data I'd probably do something like this (untested):

Code: Select all

for /f "usebackq tokens=1,2,3 delims=(){}" %%a in ('foo("foobar","Foo Bar"){"FOOBAR"="cn=Foo Bar,ou=Foo,dc=foo,dc=bar"}') do (
   set "token1=%%~a"
   for /f "usebackq tokens=1,2 delims=," %%A in ('%%b') do (
      set "token2=%%~A"
      set "token3=%%~B"
   )
   for /f "usebackq tokens=1* delims==" %%A in ('%%c') do (
      set "token4=%%~A"
      set "token5=%%~B"
   )
)
Alternatively, you could "FOR" through your data extracting the whole line, swap the quotes for some other suitably obscure character (I like to use "¡" and "¿" as I can remember the Alt+ codes and generally don't have to deal with Spanish text) and define that as the delimiter in a nested "FOR" that will do what you're wanting on the "new" string.

Last edited by bluesxman (30 Jul 2009 11:13)

cmd | *sh | ruby | chef

----------------------------

#3 30 Jul 2009 11:30
jumper

Code: Select all

@echo off

FOR /F "delims=" %%x IN (data.txt) DO (
  set V=%%x
  call set V=%%V:"=µ%%
  FOR /F "tokens=2,6,8 delims=µ" %%G IN (
   'call echo.%%V%%'
  ) DO ECHO "%%G" "%%H" "%%I")
)

pause
?

----------------------------

#4 30 Jul 2009 14:02
aariel_us

Thanks jumper!

----------------------------

#5 13 Mar 2014 17:24
nodelord


This is how to do it using the brakout chr ^

Code: Select all

for /f tokens^=2^ delims^=^" %a .....
just in case anyone asks :)
Post Reply