Why it only retrieve last line of the text file?

I have a few text files that have multiple lines. I am trying to read through all the lines in the text files using below code, but it only returns the last sentence of each text file.

1st text file :

Successful Download
FTP DOWNLOADING
ERROR-20140605CW19

2nd text file :

Succesful Download
DisConnected

The below code is used to to find specific keywords so it will perform some action in the if else:

@echo off
setlocal EnableDelayedExpansion
REM (
cd C:\Users\310152922\Desktop\AutomatedScript\Scriptlogs\3535-2D\
for /F "tokens=2 delims=:" %%e in ('findstr /B "DisCo ERROR null DOWNLOADING" log_DP_*.txt') do (

   echo %%e    <------- this always return the last line of the text files.
   SET x=%%e
   SET s=!x:~0,5!
   SET ddate=!x:~6,8!
   SET cf=!x:~25,4!
   SET week=!x:~14,5!
   SET ddate2=!x:~17,8!
   SET folder=!x:~13,4!
   SET NS=!x:~4,11!

   REM (
   REM echo(s=!s!
   REM echo(ddate=!ddate!
   REM echo(cf=!cf!
   REM echo(week!week!
   REM echo(ddate2=!ddate2!
   REM )>&2
   IF "!s!" == "DisCo" IF NOT "!cf!" == "null" (
      echo(Downloaded:!ddate2!:3535-2D-!folder!
   )
   IF "!s!" == "DisCo" IF "!cf!" == "null" (
      echo(EmptyFiles:!ddate2!:3535-2D-!folder!
   )
   IF "!s!" == "ERROR" IF NOT "!NS!" == "DOWNLOADING" (
      echo(NoneFolder:!ddate!:3535-2D-!week!
   )
   IF "!s!" == "ERROR" IF "!NS! == "DOWNLOADING" (
          echo(FailedDownload
       )
    )

I need to get the 2nd to last sentence FTP DOWNLOADING so I able to perform my if else statement.

Why does %%e always return the last sentence of text file only?

Upvotes: 0

Views: 47

Answers (2)

mordecai
mordecai

Reputation: 71

Use "tokens=2,*"

So that all 2nd tokens in every possible line (*) are passed to the for body with

tokens 2 as %%e tokens * as %%f

Hope that helps ...

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 881263

Because the findstr /b option only matches strings at the beginning of the line. See here for details.

DOWNLOADING is not at the beginning of the line. ERROR is, which is why you're finding that particular line.

You'll either have to adjust what you're looking for (such as FTP instead of DOWNLOADING) or get rid of the /b so it looks in the entire line - that, however, may have other consequences you should watch out for.

Upvotes: 3

Related Questions