1

Consider that I have a textfile containing the following information:

TIMESTARTED=Thu Nov 17 09:43:47 GMT 2011
START-OF-DATA
TEXT TEXT TEXT

The TIMESTARTED line is on a specific line in the textfile and I need to create some sort of verification that the date here is correct before a specific task is being performed. I basically want it to look at the date on the computer and see if that corresponds to the date after TIMESTARTED=, if it does it should run a batscript and if it does not it should run a different bat script.

I do not know if this is possible or if it would require extensive programming? I would prefer if it's possible to do this just by scripting in Windows. Could anyone lead me to the right path? (Bear in mind that I am not a programmer, so as simple as possible would be preferred)

jeb
  • 78,592
  • 17
  • 171
  • 225
user829084
  • 441
  • 1
  • 5
  • 5
  • Is there only one line with `TIMESTARTED` in your textfile? Do you can change the format of the entry, especially the format of the date from `Thu Nov 17` to something like `17.11.2012`? – jeb Jun 27 '12 at 09:29

2 Answers2

1

This is jeb's solution slightly modified to use an array instead of a map string, just for comparison purposes (and to promote the use of arrays in Batch):

setlocal EnableDelayedExpansion

for /F "tokens=2,3 delims= " %%A in ('findstr /B "TIMESTARTED" html_2.txt') do (
  set "monthName=%%A"
  set "day=%%B"
)
set "year=%date:~-4%"
for %%a in ("Jan=01" "Feb=02" "Mar=03" "Apr=04" "May=05" "Jun=06" "Jul=07" "Aug=08" "Sep=09" "Oct=10" "Nov=11" "Dec=12") do set %%~a
set "month=!%monthName%!"

set "timestarted=%day%.%month%.%year%"
if "%timestarted%" == "%DATE%" (
  echo The date is correct
) ELSE (
  echo wrong date %timestarted%
)
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • 1
    @jeb: Thanks a lot jeb. Did you know that `call` command with a non-existent label does _not_ abort program execution? http://stackoverflow.com/questions/11235153/how-to-find-if-a-string-is-in-a-list-of-strings-in-a-dos-batch-file/11235782#comment14819276_11235782 – Aacini Jun 30 '12 at 11:51
0

You could first pick the line with findstr and then compare the date with the current date from %date%.

setlocal EnableDelayedExpansion

for /F "tokens=2,3 delims= " %%A in ('findstr /B "TIMESTARTED" html_2.txt') do (
  set "monthName=%%A"
  set "day=%%B"
)
set "year=%date:~-4%"
SET map=Jan-01;Feb-02;Mar-03;Apr-04;May-05;Jun-06;Jul-07;Aug-08;Sep-09;Oct-10;Nov-11;Dec-12
SET "v=!%map:*%monthName%-=!"
set "month=!v:~0,2!"

set "timestarted=%day%.%month%.%year%"
if "%timestarted%" == "%DATE%" (
  echo The date is correct
) ELSE (
  echo wrong date %timestarted%
)
jeb
  • 78,592
  • 17
  • 171
  • 225
  • Thank you very much! Made a slight modification to it and got it to work as I wanted... so basically tokens is like "delimiters" and you set those two space and then you set 2,3 to be the 2nd and 3rd value to be looked up? %%A means first value %%B means second... %%C would mean third? Am I correct or confused? – user829084 Jun 27 '12 at 12:10
  • Yes, you are almost right, but because `"tokens=` start in 2, %%A is the _second_ token and %%B the third one. If `"tokens=2-4` then %%C would be the fourth token. – Aacini Jun 28 '12 at 03:23
  • @jeb: Why you don't like the array notation? :-) – Aacini Jun 28 '12 at 03:25
  • @Aacini I like it, but in this case it's too long, much longer than the main code – jeb Jun 28 '12 at 04:19
  • @jeb: Take a look at my version of your solution; it have one line _less_. :-) – Aacini Jun 30 '12 at 01:27