FOR /F loop text files: why blank lines are skipped?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

FOR /F loop text files: why blank lines are skipped?

#1 Post by tinfanide » 13 May 2012 05:40

Code: Select all

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

< 6.txt (
   FOR /F "delims=" %%A IN (5.txt) DO (
      SET str=
      SET /P str=

      
      ECHO %%A - !str!
   )
)

PAUSE


5.txt
a

b
c


6.txt
a

d
e


a - a
b -
c - d
Press any key to continue . . .

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

Re: FOR /F loop text files: why blank lines are skipped?

#2 Post by aGerman » 13 May 2012 05:47

Because that's the way FOR /F works :wink:
Try:

Code: Select all

...
FOR /F "tokens=1* delims=:" %%A IN ('FINDSTR /N "^" "5.txt"') DO (
...
      ECHO %%B - !str!
...

Regards
aGerman

EDIT:
To avoid faults with leading colons:

Code: Select all

...
FOR /F "delims=" %%A IN ('FINDSTR /N "^" "5.txt"') DO (
...
      ECHO %%A - !str:*:=!
...

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: FOR /F loop text files: why blank lines are skipped?

#3 Post by miskox » 13 May 2012 09:51

Code: Select all

help for


says:

Code: Select all

Blank lines are skipped.


Saso

tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Re: FOR /F loop text files: why blank lines are skipped?

#4 Post by tinfanide » 14 May 2012 06:15

aGerman wrote:Because that's the way FOR /F works :wink:
Try:

Code: Select all

...
FOR /F "tokens=1* delims=:" %%A IN ('FINDSTR /N "^" "5.txt"') DO (
...
      ECHO %%B - !str!
...

Regards
aGerman

EDIT:
To avoid faults with leading colons:

Code: Select all

...
FOR /F "delims=" %%A IN ('FINDSTR /N "^" "5.txt"') DO (
...
      ECHO %%A - !str:*:=!
...


Code: Select all

< 6.txt (
   FOR /F "tokens=1* delims=:" %%A IN ('FINDSTR /N "^" "5.txt"') DO (
        SET str=
        SET /P str=
        ECHO %%B - !str!
   )
)


Code: Select all

< 6.txt (
   FOR /F "delims=" %%A IN ('FINDSTR /N "^" "5.txt"') DO (
        SET str=
        SET /P str=
        ECHO %%A - !str:*:=!
   )
)


The first version works better than the second one because
the second one produces

first:
a - a
-
b - d
c - e


second
1:a - a
2: - *:=
3:b - d
4:c - e


It seems that

Code: Select all

!str:*:=!

does not substitute the string "*:" for "".

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

Re: FOR /F loop text files: why blank lines are skipped?

#5 Post by aGerman » 14 May 2012 09:49

My fault.

Code: Select all

@ECHO OFF
< 6.txt (
FOR /F "delims=" %%A IN ('FINDSTR /N "^" "5.txt"') DO (
      SET str=
      SET /P str=
      SET "str2=%%A"
      SETLOCAL ENABLEDELAYEDEXPANSION
        ECHO !str2:*:=! - !str!
      ENDLOCAL
   )
)
PAUSE

Regards
aGerman

tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Re: FOR /F loop text files: why blank lines are skipped?

#6 Post by tinfanide » 16 May 2012 08:55

Yes, it works.

Post Reply