Page 1 of 1

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

Posted: 13 May 2012 05:40
by tinfanide

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 . . .

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

Posted: 13 May 2012 05:47
by aGerman
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:*:=!
...

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

Posted: 13 May 2012 09:51
by miskox

Code: Select all

help for


says:

Code: Select all

Blank lines are skipped.


Saso

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

Posted: 14 May 2012 06:15
by tinfanide
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 "".

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

Posted: 14 May 2012 09:49
by aGerman
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

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

Posted: 16 May 2012 08:55
by tinfanide
Yes, it works.