Batch with time and date controls

April 13, 2010 at 12:01:25
Specs: Windows XP
Sorry for my bad english, I'm italian programmer without experience on advanced use of batch commands. I have a CSV file where, for each row, there are day date, sunset and sunrise time. I need to run one dos command (via batch file) with these rules: for each day, if pc time is greather/equal of sunrise time and less of sunset time run "DAY" command, if time is greather/equal of sunset time and time is less of sunrise time run "NIGHT" command. DAY and NIGHT are simple batch comand for run an exe procedure. This is structyre of CSV file:
Date;Sun Rise;Sun Set;Azimuth Sun Rise;Azimuth Sun Set
11/04/2010;06.54;20.27;75°54';284°27'
12/04/2010;06.52;20.29;75°19';285°02'
etcetera.
Batch to run in the background costantly. Any assistance will be greatly appreciated. Thanks.

See More: Batch with time and date controls

Report •


#1
April 13, 2010 at 13:17:11
the dates in your sample jump from 11/4 to 12/4. Are the dates consecutive in the actual csv file? and are weekends included?
and, what is your pc's date format (output of: echo %date%).

Report •

#2
April 13, 2010 at 15:01:36
If you're based in Italy I assume your system's date format is dd/mm/yyyy which is the same as in your csv file. Assuming that your csv file contains a line for every single day, then I would try the following:

for /f "tokens=1-3 delims=;" %%a in ('find "%date%" ^<sunrise.csv') do (
set sunrise=%%b
set sunset=%%c
)
echo sunrise: %sunrise%
echo sunset: %sunset%
rem the csv file uses 0-prefixed times so make current time same format
set now=%time: =0%
set dayornight=night
if "%now%" gtr "%sunrise%" if "%now%" lss "%sunset%" set dayornight=day
echo it is currently %dayornight%
calling batch file %dayornight%...
call %dayornight%


Report •

#3
April 14, 2010 at 11:28:12
Thanks. I tested and it works. Fine. Thanks.
A curiosity: if is night the batch file displays the comments and time, if is day display nothing, but this is marginal.
I need to run this script continuously in background so it is always running and stop just with the ESC key.
In other words, script should check PC time continuously; when pc clock reaches the time of the sunrise, script starts DAY command, when pc clock reaches sunset time, script starts NIGHT program. Can you do?
Many, many thanks for your patience !

Report •

Related Solutions

#4
April 14, 2010 at 14:37:31
"A curiosity: if is night the batch file displays the comments and time, if is day display nothing"

That is very curious indeed. I have been looking at my code for some time and I can't see why it should be behaving like that. Can anyone else see anything wrong?

"I need to run this script continuously in background so it is always running and stop just with the ESC key."

I am not sure what you mean by this. This batch file runs through and completes in less than a second. How can it run continuously? If you mean you want to run it in a loop, how often do you want to run it? If it doesn't have a built-in delay, it will run maybe a hundred times a second. Do you really want to call day.bat or night.bat 100 times a second all day long? It may be better to have it running, say, once an hour. It depends on what day.bat or night.bat does.


Report •

#5
April 14, 2010 at 15:26:33
I use a special camera can be programmed with the parameters of day and night for photos. The camera makes a picture every 5 minutes during the 24 hours. Actually I use Windows Scheduled Tasks to schedule DAY.EXE and NIGHT.EXE. This is very annoying because I have to update the times of sunrise and sunset to set the starting time of the two programs. If I use the timetable of sunrise and sunset (CSV file) I can start each day the two files at the right time. If you have a suggestion I will be happy to accept it.

Report •

#6
April 14, 2010 at 23:23:25
Here's an outline for a batch file that runs continuously (every 5 minutes). You can use the CHOICE command from Vista, but as you are running XP, you can google it and download it as a separate command. CHOICE allows you to prompt if you want to exit, but also has a timeout so it carries on after a specified period (say 5 minutes.)

When it first runs, search for sunrise and sunset for today's date (as shown in my previous code).

If it's before sunrise, call night.bat and go to :waitforsunrise.

Otherwise, if it's before sunset, call day.bat and go to :waitforsunset.

Otherwise it's night again so call night.bat and go to :waitformidnight

In each of these three labels, use the CHOICE command to prompt for user exit but also use a timeout. After the timeout, check the current time and if it's passed the required time do the appropriate thing and go to the appropriate label. (e.g. in waitforsunrise, call day.bat and go to waitforsunset)

And here's another way to do it. Have a batch file scheduled to run at midnight (00:00) every day, which reads the sunrise and sunset times for that particular day and schedules the batch files day.bat and night.bat to run once for that day at the appropriate times.


Report •

#7
April 15, 2010 at 00:03:41
@Klint: I started to recommend the AT command, but your batch seems to fill the bill. my idea was, each time it "wakes up", set the next time for wake-up using the daytimes file then reschedule the AT for the bat (%0.bat).
it was kind of complicated so i wussed out since things seemed going smooth! also trying to avoid the "date problem".
But, no way to check for ESC since the AT bat is asleep.
Use AT /DEL job# is only way to break the schedule.

Report •

#8
April 15, 2010 at 14:10:39
Ok, thanks, I wait your code.

Report •

#9
April 16, 2010 at 02:56:15
I have some scripts that may help you, doing comparisons and deltas with dates. PM me your email address, and I'll send them. They are under construction, but you'll get the idea (and the power).

Report •

#10
April 16, 2010 at 09:58:17
Thank you all !
I don't know batch language, only few and sample command lines.
In any case I am not able to write a batch like what I'm looking for.
If you can send a full batch, I will appreciate very much your help.

Report •

#11
May 8, 2010 at 05:42:34
@ Klint:

I use your batch (thanks):


for /f "tokens=1-3 delims=;" %%a in ('find "%date%" ^<sunrise.csv') do (
set sunrise=%%b
set sunset=%%c
)
echo sunrise: %sunrise%
echo sunset: %sunset%
rem the csv file uses 0-prefixed times so make current time same format
set now=%time: =0%
set dayornight=night
if "%now%" gtr "%sunrise%" if "%now%" lss "%sunset%" set dayornight=day
echo it is currently %dayornight%
calling batch file %dayornight%...
call %dayornight%

but I have this problem: script set only night time.
I use this sunrise.txt database:

Suncycle 1.1.0.0 Coordinate: 40n43 13e55 Fuso orario 1 0
Copyright Straetemans jean-marie Paquaylaan 39 3700 Tongeren Belgium
--------------------------------------------------------------------
Date;Sun Rise;Sun Set;Azimuth Sun Rise;Azimuth Sun Set
08/05/2010;05.57;20.08;66°35';293°39'
09/05/2010;05.56;20.09;66°13';294°01'
10/05/2010;05.55;20.10;65°51';294°22'
11/05/2010;05.54;20.11;65°29';294°43'
12/05/2010;05.53;20.12;65°08';295°04'
13/05/2010;05.52;20.13;64°48';295°25'
14/05/2010;05.51;20.14;64°28';295°45'
15/05/2010;05.50;20.15;64°08';296°04'
16/05/2010;05.49;20.16;63°48';296°23'
17/05/2010;05.48;20.17;63°29';296°42'
18/05/2010;05.47;20.18;63°11';297°00'
19/05/2010;05.46;20.19;62°53';297°18'
20/05/2010;05.45;20.20;62°35';297°36'
21/05/2010;05.45;20.21;62°18';297°53'
22/05/2010;05.44;20.22;62°01';298°09'
23/05/2010;05.43;20.22;61°45';298°25'
24/05/2010;05.42;20.23;61°29';298°41'
25/05/2010;05.42;20.24;61°13';298°56'
26/05/2010;05.41;20.25;60°59';299°11'
27/05/2010;05.40;20.26;60°44';299°25'
28/05/2010;05.40;20.27;60°30';299°38'
29/05/2010;05.39;20.27;60°17';299°51'
30/05/2010;05.39;20.28;60°04';300°04'
31/05/2010;05.38;20.29;59°52';300°16'

Can you help me ? Can you run the script with these parameters ?
Many thanks for your patience.
Ombell


Report •

#12
May 8, 2010 at 11:13:43
I suspect the time format is different. What is the output when you type:

echo %time%

?

On my machine it's 19:13, and your file uses a dot instead of a colon (i.e. 19.13)

Try
set now=%time: =0%
set now=%now::=.%


Report •

#13
May 9, 2010 at 01:42:50
Many thanks for your quick reply.

On one pc echo %time% is OK
On another pc (with the same data and time configuration) echo %time% is blank !

On pc with error test of this morning (local time is 10:30) %time% is blank and dayornight is night.

YOUR SCRIPT:

for /f "tokens=1-3 delims=;" %%a in ('find "%date%" ^<sunrise.txt') do (
set sunrise=%%b
set sunset=%%c
)
echo sunrise: %sunrise%
echo sunset: %sunset%
rem the csv file uses 0-prefixed times so make current time same format
set now=%time: =0%
set dayornight=night
if "%now%" gtr "%sunrise%" if "%now%" lss "%sunset%" set dayornight=day
echo it is currently %dayornight% calling batch file %dayornight%...
call %dayornight%


MY DATABASE:

Suncycle 1.1.0.0 Coordinate: 40n43 13e55 Fuso orario 1 0
Copyright Straetemans jean-marie Paquaylaan 39 3700 Tongeren Belgium
--------------------------------------------------------------------
Date;Sun Rise;Sun Set;Azimuth Sun Rise;Azimuth Sun Set
08/05/2010;05.57;20.08;66°35';293°39'
09/05/2010;05.56;20.09;66°13';294°01'
10/05/2010;05.55;20.10;65°51';294°22'
11/05/2010;05.54;20.11;65°29';294°43'
12/05/2010;05.53;20.12;65°08';295°04'
13/05/2010;05.52;20.13;64°48';295°25'
14/05/2010;05.51;20.14;64°28';295°45'
15/05/2010;05.50;20.15;64°08';296°04'
16/05/2010;05.49;20.16;63°48';296°23'
17/05/2010;05.48;20.17;63°29';296°42'
18/05/2010;05.47;20.18;63°11';297°00'
19/05/2010;05.46;20.19;62°53';297°18'
20/05/2010;05.45;20.20;62°35';297°36'
21/05/2010;05.45;20.21;62°18';297°53'
22/05/2010;05.44;20.22;62°01';298°09'
23/05/2010;05.43;20.22;61°45';298°25'
24/05/2010;05.42;20.23;61°29';298°41'
25/05/2010;05.42;20.24;61°13';298°56'
26/05/2010;05.41;20.25;60°59';299°11'
27/05/2010;05.40;20.26;60°44';299°25'
28/05/2010;05.40;20.27;60°30';299°38'
29/05/2010;05.39;20.27;60°17';299°51'
30/05/2010;05.39;20.28;60°04';300°04'
31/05/2010;05.38;20.29;59°52';300°16'
01/06/2010;05.38;20.30;59°40';300°28'
02/06/2010;05.37;20.30;59°28';300°39'
03/06/2010;05.37;20.31;59°18';300°49'
04/06/2010;05.37;20.32;59°07';300°59'
05/06/2010;05.36;20.32;58°58';301°08'
06/06/2010;05.36;20.33;58°49';301°17'


Report •

#14
May 9, 2010 at 07:17:28
On the machine where %time% is blank, either Command Processor Extensions are turned off, or it is running a old version of Windows that doesn't support it. This batch script needs %time% to be supported in order to work. Otherwise, you will need to find a different solutions, possibly using VBScript or another language.

Report •

#15
May 9, 2010 at 09:06:09
If you need a solution - via batchcode and compiled C program - that is more independant from Windows version, and mostly : idenpendant from chosen date format ... let me know.

Report •

#16
May 9, 2010 at 12:42:58
@ Klint. Thanks ! In any case your script is helpful.

@ Tvc: Thank you, a compiled script in C would be helpful.


Report •

#17
May 9, 2010 at 12:49:51
but in that case, how did the script run at all?
(for /f should have thrown an error right at the beginning).

Report •

Ask Question