Date calculation using bat and txt file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
nancy
Posts: 3
Joined: 25 Dec 2009 03:47

Date calculation using bat and txt file

#1 Post by nancy » 25 Dec 2009 03:58

Hi All,

Am fairly new to dos. Let me tell you what I want to achieve.

1. I got a bat file I ll call check.bat, when I run this it should compare the system current date with dates from a text file(dates.txt) and give me the required output.

2. Explanation using example.
I will run my check.bat file daily. And I also have dates.txt in my system. The content of dates.txt file will be like :

Username1/Password1/Fri 12/25/2009
Username2/Password2/Thu 12/24/2009
Username3/Password3/Wed 12/23/2009 etc.....

When I run this check.bat it should compare the current system date for eg: 12/25/2009 with every entry from this dates.txt file specified above. And should return a date which is 48 hrs before. So If I run today (12/25/2009) I should get dates which are equal and less than 12/23/2009).

3. I don't know how to do this: Currently my check.bat looks like this :

@echo off
setlocal
set value=
for /f "tokens=*" %%a in ('type C:\Demo\dates.txt 2^>NUL') do set value=%%a
echo value=%value%
@echo %value%


Thanks.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 26 Dec 2009 20:51

You're asking a lot.

Can you change the format of the dates.txt file? Using / as the delimiter as well as part of the date makes this much more difficult.

Some pieces to help you:

month, day, and year can be extracted this way:

for /f "tokens=2* delims=/" %%a in (dates.txt) do (
for /f "tokens=2-4 delims= /" %%c in ("%%b") do (
set month=%%c
set day=%%d
set year=%%e
)
)

Once you have month, day, and year -- you should be able to use the date functions from dostips.com to help you do the rest of the math -- basically convert to Julian and do "normal" math on the result -- and convert back to "regular" date.

http://www.dostips.com/DtTipsDateTime.php

nancy
Posts: 3
Joined: 25 Dec 2009 03:47

#3 Post by nancy » 28 Dec 2009 05:02

Hi thanks for the reply. I will continue to work on it with your suggestion on mind.

Thanks much.

SenHu
Posts: 19
Joined: 19 Mar 2009 14:57

Parsing dates and calculating time differences

#4 Post by SenHu » 07 Jan 2010 16:30

Parsing a file, extracting contents, and calculating time differences - I had to take this challenge. Here is a script - I have added tons of comments so you can follow the logic.



Code: Select all

# Script Check.txt
var str content, line, time, currenttime, timediff day, month, year
# Get current time
set $currenttime = gettime()
# Read dates.txt file into a string variable.
cat "/path/to/dates.txt" > $content
# Go thru content, one line at a time.
while ($content <> "")
do
    # Get the next line.
    lex "1" $content > $line
    # Get the portion of this line after the last space.
    stex "^ ^l[" $line > $time
    # Extract month, day, year from $time.
    stex "]^/^" $time > $month ; stex "^/^]" $time > null
    stex "]^/^" $time > $day ; stex "^/^]" $time > null
    echo $time > $year
    echo -e "DEBUG Got month=" $month ", day=" $day ", year=" $year

    # Create the time in standard format - yyyymmddhhmmss.
    set $time = $year+$month+$day+"000000"

    # What is the time difference between $time and $currenttime ?
    set $timediff = difftime(time1($currenttime) time2($time))

    #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    # Do something here with $time, and $timediff.
    # I will just echo for debug.
    #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    echo -e "DEBUG Time difference is " $timediff
done



Script is in biterscripting ( http://www.biterscripting.com ). To try, copy and paste the script into file C:/Scripts/Check.txt, start biterscripting, enter the below command.


Code: Select all

script "C:/Scripts/Check.txt"




(You will need to change "/path/to/dates.txt", in the script, to the correct path of the dates.txt file.)

nancy
Posts: 3
Joined: 25 Dec 2009 03:47

#5 Post by nancy » 07 Jan 2010 20:45

Hi that was awesome thanks SenHu

SenHu
Posts: 19
Joined: 19 Mar 2009 14:57

#6 Post by SenHu » 17 Jan 2010 10:23

You are welcome.

Post Reply