Batch Script - Using String as a delimter.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mendax_r
Posts: 5
Joined: 15 Aug 2017 14:25

Batch Script - Using String as a delimter.

#1 Post by mendax_r » 15 Aug 2017 14:50

Hi,

Can we use string as a delimiter ? ( Please see below. "one" is string delimiter )

for /F "tokens=1,2 delims=~" %%a in ("CheckoneThat") do
(
echo %%a>>Out.txt
echo %%b>>Out.txt
)

Expected Results:

Check
That


-Thanks

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch Script - Using String as a delimter.

#2 Post by aGerman » 15 Aug 2017 15:42

You only can work around it e.g. using string manipulation. Choose a character that most likely won't occur in your strings. Replace "one" with this character and use it as delimiter.

Code: Select all

set "str=CheckoneThat"
for /f "tokens=1,2 delims=~" %%a in ("%str:one=~%") do (
  >>"Out.txt" echo %%a
  >>"Out.txt" echo %%b
)

Steffen

mendax_r
Posts: 5
Joined: 15 Aug 2017 14:25

Re: Batch Script - Using String as a delimter.

#3 Post by mendax_r » 15 Aug 2017 15:56

Very cool. That works. Thank you !

aGerman wrote:You only can work around it e.g. using string manipulation. Choose a character that most likely won't occur in your strings. Replace "one" with this character and use it as delimiter.

Code: Select all

set "str=CheckoneThat"
for /f "tokens=1,2 delims=~" %%a in ("%str:one=~%") do (
  >>"Out.txt" echo %%a
  >>"Out.txt" echo %%b
)

Steffen

mendax_r
Posts: 5
Joined: 15 Aug 2017 14:25

Re: Batch Script - Using String as a delimter.

#4 Post by mendax_r » 16 Aug 2017 15:07

Hi There,

Let us say a input file with the following contents.

File Name : Input.txt
Input.txt file content: asd:asdfa:sdfwe:Firstonesecondonethirdonefourth

Expected results:

Out.txt:
sdfwe

Out2.txt:
First
Third

somehow, below is not working. am i missing something ?

@echo off
setlocal
for /f "tokens=3* delims=:" %%a in (Input.txt) do (
setlocal EnableDelayedExpansion
set string1=%%a
set string2=%%b
echo !string1!>>Out.txt
for /f "tokens=1,3 delims=~" %%c in ("%string2:one\=~%") do (
setlocal EnableDelayedExpansion
echo %%c>>Out2.txt
echo %%d>>Out2.txt
)
)

Thanks !

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch Script - Using String as a delimter.

#5 Post by aGerman » 16 Aug 2017 15:19

There are two main reasons.
1) You didn't use exclamation marks for variable string2.
2) You try to replace one\ but there is no backslash in your string.

Code: Select all

@echo off
setlocal
for /f "usebackq tokens=3* delims=:" %%a in ("Input.txt") do (
  set string1=%%a
  set string2=%%b
  setlocal EnableDelayedExpansion
  >>"Out.txt" echo !string1!
  for /f "tokens=1,3 delims=~" %%c in ("!string2:one=~!") do (
    endlocal
    >>"Out2.txt" echo %%c
    >>"Out2.txt" echo %%d
  )
)


Steffen

mendax_r
Posts: 5
Joined: 15 Aug 2017 14:25

Re: Batch Script - Using String as a delimter.

#6 Post by mendax_r » 16 Aug 2017 15:40

Nice.. Thank You. It did work without adding the backslash.
Will there be any input file size limit when using for loop?
It would be great if you can share any previous posts are available on this topic.

Thanks !!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch Script - Using String as a delimter.

#7 Post by aGerman » 16 Aug 2017 16:07

mendax_r wrote:Will there be any input file size limit when using for loop?

No the file size is not limited. Although a FOR /F loop buffers the data. That means it will most likely take quite a while before it even begins to iterate.
The only real limit in this context is the string lenth that is, 8191 characters.

mendax_r wrote:It would be great if you can share any previous posts are available on this topic.

Not sure what you mean. There are more than 6500 Threads at DosTips. I don't remember them all. If you want to search for a specific DosTips topic then use
your search term site:dostips.com
at Google. At least I wouldn't do anything else to find related topics for you.

Steffen

mendax_r
Posts: 5
Joined: 15 Aug 2017 14:25

Re: Batch Script - Using String as a delimter.

#8 Post by mendax_r » 17 Aug 2017 13:58

Cool. Thank You.. :-)

Post Reply