7

I have a txt file that contains the following lines

jfo3 93jfl
lvls 29fdj
nskd jfuwe
xlkw eklwe

I'm trying to read the file line by line, and do something with it. What delimiter should I use?

The delim I'm using here reads each word separately.

@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%x in (lines.txt) do (
    echo %%x
)
Suncatcher
  • 10,355
  • 10
  • 52
  • 90
sameold
  • 18,400
  • 21
  • 63
  • 87

3 Answers3

13

This reads line by line for me:

for /f "delims=" %x in (lines.txt) do echo %x
Moe Matar
  • 2,026
  • 13
  • 7
  • 4
    @sameold: In a batch file you need to use `%%` instead of `%`. The variant in this answer is for use on the command line directly. – Joey Jul 17 '11 at 09:02
  • @Joey is correct. The script above works on the command line. – Moe Matar Jul 17 '11 at 16:52
  • 2
    Why is `"delims="` used for newline delimeter? Answer: it sets the value of `delims` to empty, meaning the loop uses no character (except newline) as a delimiter – cowlinator Apr 09 '18 at 19:40
1

The problem is not related to delims, but to tokens:

for /f "tokens=*" %%x in (lines.txt) do echo %%x
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • 1
    Not really, as "tokens=*" and "delims=" produce nearly the same result. As with "tokens=*" the delims are still active, they remove leading spaces and TABs from each line – jeb Jul 22 '11 at 11:29
  • if you don't specify `"delims="`, then it will default to `"delims= "` (a space), and it will trim extra whitespace between the tokens which means you are not getting the real lines. `"delims="` by itself will have a default of `"tokens=1"`, but since it grabs the whole line, you only need the one token. – Dave Cousineau Mar 17 '16 at 21:41
0

If this is your input file:

abc,def

ghi,jkl

mno,pqr

then use

FOR /F "tokens=1,2,3 delims=," %%i in (test.txt) do (whatever u want) 
Andrea
  • 11,801
  • 17
  • 65
  • 72
Siddharth
  • 51
  • 1
  • 2