Hi everyone, i hv created a batch files which will be scheduled to run every one hour from 8am to 11pm. but certain part of it will be executed only once per day.
say the script is like this
block which will be run every one hour
--
--
block which will run once a day
--
--
end of script.my requirement is like that i cant separate the code into a different file and schedule, it has to be in the same bat file.
my logic is to create a variable and assign the date to it. and then to check the currnent date if they are different then execute the block and assign the current date to it, and if same then skip the part (using goto statement). but i am unable to convert this logic to code. can n e one help me in this issue? its urgent.
thanks in advance
Roy
what i hv done is set a global variable to a date like this from the command prompt before the batch file has run for the first time c:\> set lastdeleted=04_09_2010
and this the code from the batch file
@echo off
rem *********
rem say this block contains code to be executed per hour
rem **********
date /t > c:\date1.txtfor /f "tokens=1,2,3 delims=/" %%i in (c:\date1.txt) do (
setlocal & set currdate=%%i_%%j_%%k
)
endlocal & if not %currdate% equ %lastdeleted% (
set lastdeleted=%currdate%
goto next
)
setlocalendlocal & if %currdate% equ %lastdeleted% goto endofFile
:next
rem **************
rem say his block of code needs to be executed onece a day
rem ****************
:endofFile
Post the output of: echo %date%
=====================================
Helping others achieve escape felicityM2
code: localdate.bat @echo off
echo this part will be printed every timedate /t > c:\date1.txt
for /f "tokens=1,2,3 delims=/" %%i in (c:\date1.txt) do (
setlocal & set currdate=%%i_%%j_%%k
)echo %currdate%
endlocal & if not %currdate% equ %lastdeleted% (
echo %lastdeleted%
set lastdeleted=%currdate%
goto :exeOne
)setlocal
endlocal & if %currdate% equ %lastdeleted% (
echo hi
echo %lastdeleted%
set lastdeleted=%currdate%
goto :endoffile
)
:exeOne
echo this part will be printed first time
:endoffile
output:
C:\>set lastdeleted=05_09_2010C:\>localdate
this part will be printed every time
05_10_2010
05_09_2010
this part will be printed first time
C:\>localdate
this part will be printed every time
05_10_2010
05_10_2010 was unexpected at this time.C:\>
its giving error when run for the second time.
can u help me to figure out the error?
You'll probably want to look into SETX, which will allow your environment variables to persist across the multiple runs.
I'm working on code with date manipulation. One of the functions is (will be) that you can check if a given file is older (or newer) than X amounts of seconds, hours, days, ... So, if you want to run every 1 day, this is what I suggest:
- Each time you run, you either END (or START) with creating an indicator file.
- But even before that, you check how old the indicator file actually is, and if less than 24 hours, exit script there.PM me if you want code.
> You'll probably want to look into SETX, which will
> allow your environment variables to persist across
> the multiple runs.
>Or, just store them in files ... it's very simple. Batch is 100% suited for that.
For the date manipulation, I'm thinking that's more effort than required. Just save %DATE%. Then you can compare %DATE% to the saved %DATE%. If they're equal, it's obviously the same day.
That's why I asked for info in #3. I guess we're not gonna find out.
=====================================
Helping others achieve escape felicityM2
well, actuelly, the date format doesn't matter too much if you
are just comparing for a difference. R2 was right (this time,
this time... oh all right, he's ALWAYS right. Lol!)
forget the environment var.s and just store the date in a squib
file and compare it once per hour with %date% (since the
script is supposed to do something once per hour)
the hourly thing should be handled by chron/AT subsys. to
avoid having to loop 1000 times per second checking time.
I guess chron/AT loops 1000 times per second anyhow if it's
initialized.
got it guys. used file instead of var. working fine. thanks to all of you.
You can indeed let the scheduler do the hour checking, but know that you are limited to the features offered by that scheduler itself. If you core code can only be run 1 time/hour, it's better to have that check included in the code itself, rather then in the "caller". The caller can also be manual, and then there is no check at all. But, if you are happy, we are as well. Also behold the difference between "one time a day" and "last occurance must be more than 1 day". I see the question in this thread is relating to case nr. 1
But if case nr. 2 is the requirement (which sometimes only is acknowledged later), than you cannot compare dates and come to conclusion, unless you include hour, minute and possible seconds comparison.OK
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |