Page 1 of 1

Batch to retrieve date string from text file

Posted: 12 Jan 2011 07:00
by steogen
Hello,

I have built a batch file that when opened adds current date and time to an external text file with the commands:

Code: Select all

@echo off
echo %date% %time% >> flexiLog.txt


Every time the batch runs a new line containg date and time is added to the text file as dd/mm/yyyy hh:mm:ss. It works fine. However, I would also like the same batch file to read the content of the last line in that text file before adding a new line. In particular if the date in the last line (i.e. just the "dd/mm/yyyy" part) is different than the current date, then a blank line should be added before the new entry.

this is a concrete example of what i would like to achieve:

1) case where the same date is verified:

12/01/2011 11.34
12/01/2011 11.50 (last line)
12/01/2011 12.34 (new entry)

2) case where the last date is different form the current:

11/01/2011 9.32
11/01/2011 10.52 (last line)

12/01/2011 12.34 (new entry)


Is it possible? how do I retrieve the date from the text file and compare it with the current date? I read how to extract characters from the beginning of a string once acquired (set str=%str:~0,10%) but i'm missing the rest...

Thank you.

S.

Re: Batch to retrieve date string from text file

Posted: 12 Jan 2011 07:36
by ChickenSoup
This could do it.

Code: Select all

for /f "tokens=1 delims= " %%a in (flexiLog.txt) do set lastdate=%%a
if "%date%"=="%lastdate%" goto EOF
echo %date% %time% >> flexiLog.txt
:EOF

Re: Batch to retrieve date string from text file

Posted: 12 Jan 2011 08:24
by steogen
Thank you!
It worked though I have set different events for the condition :)

S.