adrilash
adrilash

Reputation: 11

Searching dates inside a file with Batch

I want to find some dates inside a file, with Batch.

I have the following code:

SETLOCAL ENABLEDELAYEDEXPANSION
for /F "tokens=*" %%a in (%path%\%file%) do (
    echo %%a | findstr /I "Date:"
 )
ECHO.
PAUSE
PAUSE

With this code I'm able to get the first date that appears in the file, but then the script finishes, and I want to get all the dates presents in the content of the file, not only one.

Do I have to modify the for structure or use another command (instead of findstr)?

Upvotes: 0

Views: 170

Answers (1)

Ben Personick
Ben Personick

Reputation: 3264

If you don't need to use the dates in variables and just want to output them, then just do this at the cli

TYPE "%path%\%file%" | FIND /I "Date:"

If you need to find each date and then do something else with it using a temp variable (and assuming there is only one : in the line with the date, and nothing else follows the date, you could do this in a cmd script

@(SETLOCAL
  ECHO OFF
)

 CALL :Main

( ENDLOCAL
EXIT /b )

:Main
  For /F "Tokens=1* delims=:" %%A IN (`
    TYPE "%path%\%file%" | FIND /I "Date:"
  ') DO (
    REM if you need all the characters after "Date:" we can simply use this next line:
 
    SET "Date_Tmp_Full=%%~B"

    REM. If there may be whitespace around the value after "Date:" we can use the following method instead to trim it instead.

    For /F "tokens=*" %%_ IN ('echo %%~B') DO (
      SET "Date_Tmp=%%~_" )

     REM Your other code can go here, or you can call a function to do more instead.
     REM
     REM
  )

Upvotes: 1

Related Questions