for /f syntax with delims?

July 14, 2011 at 04:14:29
Specs: windows xp
Hi all,

I cannot figure out what I do to get this output.

I process this string: a|b|c

I got this output:

token 1: a
token 2: b
token 3: c

ok good enough


2nd string: a||c

I've got this output
token 1: a
token 2: c

I want this output

token 1: a
token 2:
token 3: c

How can I get this output?

see my script below.


:: Main

@echo off
set i=1

for /f "tokens=* delims= " %%f in ("a||c") do (
set line=%%f
call :processToken
goto :eof

:: Func processToken


for /f "tokens=1* delims=|" %%a in ("%line%") do (
echo token %i%: %%a
set line=%%b
Set /A i+=1
if not "%line%" == "" goto :processToken
goto :eof


See More: for /f syntax with delims?

Report •

July 15, 2011 at 02:44:56
A For loop will only process data character(s) which appears between delims, as you have no data character(s) between delims in your 2nd string the field is ignored. Delims themselves are not processed.

A workaround is to place a space between the delims, not an easy task if you have a large file with many instances of adjacent delims.

Please come back & tell us if your problem is resolved.

Report •

July 16, 2011 at 07:39:00
Hi degelis
Are the strings always in alphabetical order.

Report •

July 17, 2011 at 05:49:30
No, the strings aren't always in alphabetical order.

However, is there way to do this in dos shell like we do korn shell
echo "a||c"|sed 's/||/| |/', the result will be a| |c ?


Report •

Related Solutions

July 17, 2011 at 08:52:42
HI degelis
This help

@echo off
SetLocal EnableDelayedExpansion
set Str="a||c"
set Str=%Str:||= @ %
set Str=%Str:|= %
call :Process %Str%
exit /b

:Process %1
set Str=%~1
set Num=0
for %%a in (%Str%) do (
set /a Num+=1
if "%%a" EQU "@" (echo Level !Num!:
) else (echo Level !Num!:%%a)
exit /b

Report •

August 15, 2011 at 09:18:03
Hi everyone,

As mention by Wahine said For loop will only process data character, based on that information and dtech10's example I've figured out how to work around the bug.

I've to search for string "||" and replace by "| |" and these commands do exactly what does sed. This way the for loop statement works well.

set str="a||c"
set Str=%Str:||=| |%
echo %Str%
"a| |c"

thank you very much guys

Report •

Ask Question