need to skip a part of batch file

Microsoft Windows xp professional w/serv...
May 10, 2010 at 02:17:33
Specs: Windows XP
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


See More: need to skip a part of batch file

Report •


#1
May 10, 2010 at 02:28:54
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.txt

for /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
)
setlocal

endlocal & if %currdate% equ %lastdeleted% goto endofFile


:next
rem **************
rem say his block of code needs to be executed onece a day
rem ****************
:endofFile


Report •

#2
May 10, 2010 at 05:19:09
Post the output of:

echo %date%


=====================================
Helping others achieve escape felicity

M2


Report •

#3
May 10, 2010 at 05:57:02
code: localdate.bat

@echo off


echo this part will be printed every time

date /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_2010

C:\>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?


Report •

Related Solutions

#4
May 10, 2010 at 08:55:09
You'll probably want to look into SETX, which will allow your environment variables to persist across the multiple runs.

Report •

#5
May 11, 2010 at 06:41:21
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.


Report •

#6
May 11, 2010 at 06:43:37
> 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.


Report •

#7
May 11, 2010 at 07:01:23
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.

Report •

#8
May 11, 2010 at 20:18:47
That's why I asked for info in #3. I guess we're not gonna find out.


=====================================
Helping others achieve escape felicity

M2


Report •

#9
May 11, 2010 at 20:45:02
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.

Report •

#10
May 12, 2010 at 01:33:05
got it guys. used file instead of var. working fine.

thanks to all of you.


Report •

#11
May 13, 2010 at 06:36:08
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


Report •

Ask Question