creating batch script

May 3, 2010 at 05:59:54
Specs: Windows XP
Hey all,

I'm trying to create a script that will copy data from one database to another on certain times during the day. To verify if it's necessary there should be a check done if the difference between the last time a copy was made(kept in a timestamp-file) and the current time. How can this be done in cmd?
So how can you compare 2 dates and have an action (here the copy) done if the condition is fulfilled? (for example: difference between the timestamps is greater then 2 hours)?


See More: creating batch script

Report •

May 3, 2010 at 07:36:47
How can this be done in cmd?
It really can't, and any attempt at date/'time math is messy at best, and buggy at worst. A better option would be to use VBScript, which is arguably "more native" than batch.

Report •

May 3, 2010 at 07:59:58
As per R2, date math in bat is a mess.

For the copy the way out is just use xcopy /d.

One of these days, I may get around to this. Post the output of:

echo %time%

Helping others achieve escape felicity


Report •

May 3, 2010 at 09:38:38
PM me with your email, and you'll get some tools around math-functions via Batch

Report •

Related Solutions

May 3, 2010 at 11:32:23
'here's a "general purpose" calcu-dator
'***** NEW! VERSION 2.01!! BETA RELEASE!!! *****
set xin=wscript.stdin
set cout=wscript.stdout
dim y,x
set y=wscript.arguments
if a<1 then
end if
if a<1 then
wscript.echo "add/subtract two times or dates, or add/sub units to a time or date"
wscript.echo "usage: da [units] time_or_date1 time_or_date2||number_of_units"
wscript.echo "(cap letter =default) units is one of : D w m h N s and if caps, default format output"
wscript.echo "d=days, w=weeks, m=months (applied to date), h=hours, m=minutes, s=secs (applied to time)"
end if
'use colon to interpret as time, else date
'sets defaults for date (adjust no. days) or time (adjust no. minutes)
if p>0 then
end if
'now allow overrides by token #1 if valid subset
if instr("dmwhns",lcase(y(0)))>0 then
'allow means for user to override formatting by using caps
if ucase(s)=s then ff=0
end if
if isnumeric(y(a)) then
end if
'---- (edit fixed a bug in above script!)
to use it, save it as (f/e) dd.vbs, then in batch:
set d1=date1
set d2=date2
for /f %%a in ('echo d1 d2 ^|cscript /b dd.vbs') do set diff=%%a

or alternatively:
for /f %%a in ('cscript /b dd.vbs %date1% %date2%') do set diff=%%a

(where date is either date or time, and date format is fairly robust, even works with yyyy-mm-dd!)
alternative differences available as optional token 1 passed:
difference in:
"d" days (default for dates)
"w" weeks
"m" months
"h" hours
"n" minutes (default for times)
"s" seconds
(or add your own using vbscript datediff parameters)

Report •

Ask Question