Page 1 of 3
Could use some help with how to do batch code?
Posted: 09 Apr 2013 01:56
by batchfile
Hi, happy to be here
3 Parts:
1.
Task A - Runs at start up and triggers
Check.bat to run and check if it has been
30 days since
Task B has been created
2.
Task B - Runs once every
30 days3.
Check.bat - Has code that detects if
Task B is
30 days past the day it was created otherwise Check.bat exits
Any help would be appreciated or if other way to keep track of
Task B from the date it was created?
Re: Could use some help with how to do batch code?
Posted: 09 Apr 2013 02:56
by foxidrive
Are you running task B as a scheduled task every month? What do you need to do? Please clarify.
Re: Could use some help with how to do batch code?
Posted: 09 Apr 2013 03:14
by batchfile
foxidrive wrote:Are you running task B as a scheduled task every month? What do you need to do? Please clarify.
Task B runs every 30 days not monthly since months are uneven in Gregorian calendar.
Task B runs a program I need to run exactly every 30 days
Problem is if computer is off when Task B should run it doesn't run, so Task A at startup is needed run Check.bat to detect if past the 30 days that Task B missed cause computer was off
Basically, Task A is a backup plan to run a program if Task B missed it due to computer being off
So I want Check.bat to time or detect if past Task B's schedule to run and run a program that normally Task B would if 30 day date hadn't been missed
Hope that clarifies things
Thank you for your help
Re: Could use some help with how to do batch code?
Posted: 09 Apr 2013 03:22
by foxidrive
Thanks for the description. How do you run your program every 30 days? By task scheduler?
Re: Could use some help with how to do batch code?
Posted: 09 Apr 2013 03:55
by batchfile
foxidrive wrote:Thanks for the description. How do you run your program every 30 days? By task scheduler?
I have the code to create both tasks
One runs at startup
Code: Select all
schtasks /create /tn "Task A" /tr "%SystemDrive%\Check.bat" /sc onstart /ru "" >NUL
The other every 30 days
Code: Select all
schtasks /create /tn "Task B" /tr "%SystemDrive%\Program.exe" /sc daily /mo 30 /ru "" /f >NUL
My request is the code in Check.bat that has to know when Task B was created and if at day 31 since Task B was created Check.bat would run program.exe
So I'm trying to cover the bases so to speak so the program.exe runs as soon as computer starts up if Task B is past scheduled time/date to run
I'm open to different ways to do this:
Like when Task B is created could other file be created to act as time/date marker and could that file be checked for being 31 days old...
Also every time program.exe actually runs, Task B is re-created as to sync program.exe being run every 30 days with task B creation
Sorry if that's confusing but I'm just trying to have control over program.exe being either run every 30 days or a.s.a.p when computer is starting via Task A triggering Check.bat which determines if past Task B scheduled run time
So technically speaking Task A is backup plan to run program.exe and Check.bat needs code to somehow determine the duration since date that Task B gets created and Check.bat will just exit if it's say Task B days 1-30
Re: Could use some help with how to do batch code?
Posted: 09 Apr 2013 06:12
by foxidrive
So you are using task scheduler to schedule it every 30 days.
But if I understand you, you want the task to run if it goes over 30 days, When the PC is eventually started. Task scheduler does have a setting for that.
I'm not sure if it will then rearm the schedule for 30 days from that (new) date.
Re: Could use some help with how to do batch code?
Posted: 09 Apr 2013 07:41
by Squashman
foxidrive wrote:So you are using task scheduler to schedule it every 30 days.
But if I understand you, you want the task to run if it goes over 30 days, When the PC is eventually started. Task scheduler does have a setting for that.
I'm not sure if it will then rearm the schedule for 30 days from that (new) date.
Yep
Run task as soon as possible after scheduled start is missed
Re: Could use some help with how to do batch code?
Posted: 09 Apr 2013 08:36
by batchfile
I'm not needing help with task settings or task
Those things are fine
My posts are about the Check.bat I want to write
Let me reiterate
I'm using the startup task only as a measure if computer was off when 30 day task was suppose to run
So the start up task runs Check.bat and it somehow through date/time does query of exactly how many days since 30 day task was created?
If Check.bat at startup determines, "through some way", that it is past 30 days since Task B was created then Check.bat will run program.exe
So it's not about the program that gets run or the tasks, there static
So lets say when Task B gets created, some way, at that moment perhaps a way of counting the days that will pass is initiated? This is what check.bat will be doing every start up but it need a way to do it precisely
So can a text file be exported with a time stamp that check.bat can use to compare dates and either take action in running program.exe or not
I know some of the bat files here use set and DD/MM/YYYY to do calculations to gather date to make decisions correct? Also want this to work on non-english OS's that might have different date/time formats
I really wanted to query the registry to determine Task B created date or create custom entry to act as marker for 30 day cycle of Task B but don't know the registry very well...lol
Thanks for your time
Re: Could use some help with how to do batch code?
Posted: 09 Apr 2013 08:56
by Squashman
I guess I am not understanding. Here is how I understand what you are doing.
TASK B is scheduled to run program.exe every 30 days.
TASK A is scheduled to run every day and checks if program.exe ran within the last 30 days and if not runs program.exe.
That makes no sense. That is the purpose of the option "Run task as soon as possible after scheduled start is missed".
There is no need to check if program.exe ran if you just tell the scheduled task to run the task as soon as it can on the next boot up.
Re: Could use some help with how to do batch code?
Posted: 09 Apr 2013 10:26
by Endoro
I want to see, if my date function works for you, so: please post the output from this code:
Code: Select all
echo off&setlocal
for /f "tokens=2*" %%a in ('reg query "HKCU\Control Panel\International" /v sShortDate^|find "REG_SZ"') do set "ssShortDate=%%b"
reg add "HKCU\Control Panel\International" /f /v sShortDate /d "dd MM yyyy" >nul
set "cdate=%date%"
reg add "HKCU\Control Panel\International" /f /v sShortDate /d "%ssShortDate%" >nul
for /f "tokens=1-3" %%i in ("%cdate%") do set "day=0%%i"&set "month=0%%j"&set "year=%%k"
set "day=%day:~-2%"
set "month=%month:~-2%"
echo(%day%.%month%.%year%
Re: Could use some help with how to do batch code?
Posted: 09 Apr 2013 11:03
by batchfile
Endoro wrote:I want to see, if my date function works for you, so: please post the output from this code:
Code: Select all
echo off&setlocal
for /f "tokens=2*" %%a in ('reg query "HKCU\Control Panel\International" /v sShortDate^|find "REG_SZ"') do set "ssShortDate=%%b"
reg add "HKCU\Control Panel\International" /f /v sShortDate /d "dd MM yyyy" >nul
set "cdate=%date%"
reg add "HKCU\Control Panel\International" /f /v sShortDate /d "%ssShortDate%" >nul
for /f "tokens=1-3" %%i in ("%cdate%") do set "day=0%%i"&set "month=0%%j"&set "year=%%k"
set "day=%day:~-2%"
set "month=%month:~-2%"
echo(%day%.%month%.%year%
Thank you for your code help, here is output with pause. Also I meant that I want it to work on English and non-english regional timedate format so it's universal on other language OS.
So now we have current date and have to make check if 30 days past that date meaning that current date is day 1
This is close in theory and I appreaciate your help greatly
@squashman
I want this to be automatic and not have to go into task scheduler and set the "run the task as soon as it can on the next boot up" setting plus that setting is a little buggy if I remember correctly in testing but basically that is not direction I want to go
Just want the Task A/Check.bat to take care of date comparison. The 30 day Task B will just be left alone and only when program.exe has actually ran will I create over the current Task B with new
To do the recreate Task B all that is need is just to rerun the Task B
schtasks /create /tn "Task B" /tr "%SystemDrive%\Program.exe" /sc daily /mo 30 /ru "" /f >NUL
This is essential in Check.bat working in a current manner as a new starting point to measure/check has to be done to keep program.exe running in sync with new Task B and to have new Task B starting date
Thank you for your time, efforts and any code help
BF
Re: Could use some help with how to do batch code?
Posted: 09 Apr 2013 11:06
by Squashman
Well as long as someone is willing to placate you that is fine. I tend to do things as efficiently and with as little effort as possible.
Have you determined how we are going to know if your program.exe ran? Does it create a log file for us to check the date?
Re: Could use some help with how to do batch code?
Posted: 09 Apr 2013 11:41
by batchfile
Squashman wrote:Well as long as someone is willing to placate you that is fine. I tend to do things as efficiently and with as little effort as possible.
Have you determined how we are going to know if your program.exe ran? Does it create a log file for us to check the date?
Since this an automated deal efficient to me means, not having to open task scheduler and like I said that setting is a little buggy if I remember correctly but nonetheless it is not what I want as it's manual
I wish to not monitor when program.exe last ran as it runs when it runs which if computer is off could be intermittent and it just better to redo Task B when program runs and set new initial date?
My ideas are to let Check.bat do start up checking then if past day 30 since
Endoro's code will detect, hopefully in a international region format way, the starting date at the exact time Task B is created hence the first check and Task B creation are always in sync
So every time program.exe does run Task B is recreated and new start date is set. All we need is code to compare at start up in case past Task B schedule to run was missed or we're at past day 30
All I'm doing, as difficult as it is to explain or write code for, is avoiding user having to run program.exe manually if past day 30
Sorry if my explanation is not trued yet as I'm not so perfect at the code but know the idea and try to relay as best as possible
Thank you for you time
Re: Could use some help with how to do batch code?
Posted: 09 Apr 2013 12:08
by Endoro
I'm not really sure about your needs- but you can try this.
It does:
- get the current Julian Day
- get the last stored Julian Day from a .sav file, build one if it not exists
- check the difference between today and the stored Julian Day
- if the difference is greater than 29 it might start your task if you put the code in
and the current Julian Day is stored
- the Julian Day function is from
here.
Code: Select all
@echo off&setlocal
set "fsave=%~dpn0.sav"
call:JDNtoday cJDN
call:getsaveJDN sJDN
if not defined sJDN goto:saveJDN
set /a days=%cJDN%-%sJDN%
echo(%days% day(s^) have passed.
if %days% gtr 29 (
rem start task B here
echo(starting task B now ...
rem store the new date in %~dpn0.sav
call:saveJDN
)
goto:eof
:saveJDN
>"%fsave%" echo(%cJDN%&& (echo(Date saved & goto:eof) || echo ERROR date NOT in "%fsave%" saved & goto:eof
goto:eof
:getsaveJDN
setlocal
if exist "%fsave%" for /f "usebackq" %%i in ("%fsave%") do set "JDN=%%i"
endlocal& set "%1=%JDN%"
exit /b
:JDNtoday
setlocal
for /f "tokens=2*" %%a in ('reg query "HKCU\Control Panel\International" /v sShortDate^|find "REG_SZ"') do set "ssShortDate=%%b"
reg add "HKCU\Control Panel\International" /f /v sShortDate /d "dd MM yyyy" >nul
set "cdate=%date%"
reg add "HKCU\Control Panel\International" /f /v sShortDate /d "%ssShortDate%" >nul
for /f "tokens=1-3" %%i in ("%cdate%") do set "day=0%%i"&set "month=0%%j"&set "year=%%k"
set "day=%day:~-2%"
set "month=%month:~-2%"
call:DateToJDN %day%.%month%.%year% JDN
endlocal& set "%1=%JDN%"
exit /b
:DateToJDN dd.mm.yyyy jdn=
setlocal
set date=%1
set /A yy=%date:~-4%, mm=1%date:~-7,2% %% 100, dd=1%date:~-10,2% %% 100
set /A a=mm-14, jdn=(1461*(yy+4800+a/12))/4+(367*(mm-2-12*(a/12)))/12-(3*((yy+4900+a/12)/100))/4+dd-32075
endlocal & set %2=%jdn%
exit /B
endlocal
Re: Could use some help with how to do batch code?
Posted: 09 Apr 2013 12:37
by Squashman
batchfile wrote:
Since this an automated deal efficient to me means, not having to open task scheduler and like I said that setting is a little buggy if I remember correctly but nonetheless it is not what I want as it's manual
Set it once and forget. You could probably script it as well. From your previous posts that looks like how you were setting the scheduled tasks in the first place. If it were buggy or didn't work half the crap that Google and Microsoft have programmed into Task Scheduler would never work then.
If you are going to go this route then your TASK B is somehow going to have to write a log file so we know the last time it ran. The other option may be to query the Scheduled Tasks and look at the NEXT RUN TIME field and compare that to the current date. If it is before the current date then you know it didn't run and you could run it.