Questions on delims in for loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
keny456789
Posts: 3
Joined: 03 Aug 2015 02:16

Questions on delims in for loop

#1 Post by keny456789 » 03 Aug 2015 02:38

The looping codes below deal with text file.

Code: Select all

for /f "tokens=1 delims=:" %%i in ('findstr /n "^END" %text_file1%') do set count=%%i

count line by line untill the line having "END"

Code: Select all

for /f "delims==" %%a in (%text_file1%) do set lastline=%%a

store the lastline string in %%a

Code: Select all

for /f "skip=1 delims=*" %%a in (test1.txt) do (
   echo %%a >> "test1_copy.txt"   
)

read in the test1.txt line by line until end of file and export them to the new text file test1_copy.txt

I get confused on

Code: Select all

"delims=:"  "delims=="   "delims=*"  and "delims= "

Are all of them used for looping on every whole new line?
Any differences/explanations on putting : = * space in delims? :oops:
Thanks in advance.

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

Re: Questions on delims in for loop

#2 Post by jeb » 04 Aug 2015 05:08

Hi keny456789,

the main difference is that they split a line at different characters.

If you only want to count, then you don't need to use the delim option at all.

It's important when you have to look at each line like

Code: Select all

for /f "delims=:" %%a in (test1.txt) do (
   echo %%a
)


And in your file is
Hello:world
line2*another:text


Then it will print
Hello
line2*another

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Questions on delims in for loop

#3 Post by foxidrive » 04 Aug 2015 06:23

You may have posted the wrong code, jeb.

It prints

line2

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

Re: Questions on delims in for loop

#4 Post by jeb » 04 Aug 2015 06:42

foxidrive wrote:You may have posted the wrong code, jeb.

You are right, of course :oops:

I fixed it in my prior post.

keny456789
Posts: 3
Joined: 03 Aug 2015 02:16

Re: Questions on delims in for loop

#5 Post by keny456789 » 19 Aug 2015 19:24

jeb wrote:Hi keny456789,

the main difference is that they split a line at different characters.

If you only want to count, then you don't need to use the delim option at all.

It's important when you have to look at each line like

Code: Select all

for /f "delims=:" %%a in (test1.txt) do (
   echo %%a
)


And in your file is
Hello:world
line2*another:text


Then it will print
Hello
line2*another


Hi Jeb, Thanks for your explanation. I am clearer about the delims usage.

keny456789
Posts: 3
Joined: 03 Aug 2015 02:16

Re: Questions on delims in for loop

#6 Post by keny456789 » 20 Aug 2015 19:09

Thank jeb and foxidrive, you gave clear answers to me. :D

Post Reply