how do you set for delimiters to a word?

April 19, 2011 at 05:50:26
Specs: Windows vista
Is it possible to have more than one character as the delimiter set for the for command? not as in a whole bunch of single characters, but as in if I wanted to set the delimiters to a specific word
say if I wanted to set the delimiters to the word Fish:

for /f "tokens=2 delims=Fish" %%a in ("if you eat fish then go") do set two=%%a

sets the delimiters to f, i, s, and h separately.
If I try to use grouping like this:

for /f "tokens=2 delims=(Fish)" %%a in ("if you eat fish then go") do set two=%%a
or
for /f "tokens=2 delims="Fish"" %%a in ("if you eat fish then go") do set two=%%a

then the grouping symbols become delimiters and the same problem occurs
is there any way to set the delimiters to word without this problem?


See More: how do you set for delimiters to a word?

Report •


#1
April 19, 2011 at 06:47:26
Definitively NO, since a delimiter is defined as a single character or symbol.

Report •

#2
April 19, 2011 at 17:32:52
oh, ok. thanks anyway.

Report •

#3
April 19, 2011 at 20:53:36
You could split the text using substrings, but it's not pretty:

@echo off
SetLocal EnableDelayedExpansion
rem spliton can't contain ! " or =
set "spliton=fish"
set "line=fishif you fish eat fishfish then go fish okfishfish"


set tokcnt=0
:loop
if not "!line:%spliton%=!"=="!line!" (
    for /f "delims=" %%a in ("!line:*%spliton%=!") do (
        set /a tokcnt+=1
        set "text!tokcnt!=!line:%spliton%%%a=!"
        set line=%%a
        if not defined text!tokcnt! set /a tokcnt-=1
        goto loop
    )
)
if not "!line!"=="!spliton!" (
    set /a tokcnt+=1
    set text!tokcnt!=!line!
)
echo %tokcnt% tokens:
for /l %%a in (1 1 %tokcnt%) do echo %%a: "!text%%a!"
pause


Report •

Related Solutions

#4
April 20, 2011 at 21:46:18
thanks heaps! that helps a lot, even though it's tones of code, at least you can actually do it, so again, thanks.

Report •

Ask Question