• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 607
  • Last Modified:

Is it possible to set a 7 hour delay in a batch file to have the same effect as scheduling a task in Task Scheduler to run at midnight?

I have a batch file which runs a macro in an Access 2007 database. It takes hours for the Access code to run, so I want it to run overnight:

Echo Off
"C:\Program Files\Microsoft Office\Office12\msaccess.exe" "c:\users\me\Documents\databasename.accdb" /x macroname
Exit

My goal is to schedule a task in the task scheduler so that this macro will run at midnight. I am just looking for a plan B in case I don't have permissions to set up a task and there are issues with IT doing this for me. Is it possible to put a 7 hour delay in the batch file? In this way, we could run the batch file at 5pm and it wouldn't run the Access macro until midnight.

I see information on the internet concerning setting a delay for up to 99 seconds. Is it possible to set a delay for 7 hours?

Thanks.
0
newbie46
Asked:
newbie46
8 Solutions
 
Brian GeeCommented:
Here's something I do to remotely kickoff certain files I have in batch files:

If you can keep your Outlook client open on the machine in question, then you can setup a rule to run the batch file when an email is received with a specific keyword(s) in the subject line, e.g. runbatchforaccess.

If you need this to be a bit more automated (as per your scenario), you can set another Outlook client (perhaps the same one, although I would recommend a different one) to send the email with the keyword at 12am (Delay Delivery in Outlook 2010) so that it can trigger the rule when it is received in your Inbox. This would work as a secondary option provided you don't need to run this process at exactly 12am... it might kickoff a minute or two later depending on how fast your email is received, etc.

I know this method is not the most ideal, but it is definitely viable as an option to accomplish the objective. The system needs to be on and the Outlook client launched (but you can lock the system for security purposes).
0
 
Brian GeeCommented:
^ In the first sentence, certain files was meant to say certain processes.
0
 
TimBusiness Systems AnalystCommented:
7 hours = 25200 seconds
You can add a loop to your batch file to pause 99 seconds 254 times
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
SStoryCommented:
Since things are synchronously ran in a batch file, you need only call an EXE that sleeps for 7 hours and then returns execution to the batch file for the line after that program has run.

It turns out there is a sleep command in this resource kit:
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&DisplayLang=en

See this article
http://malektips.com/xp_dos_0002.html

0
 
subhashchyCommented:
try this batch file....

@echo off
echo wscript.sleep 25200000 >%temp%\sleep.vbs 
cscript /nologo %temp%\sleep.vbs 
"C:\Program Files\Microsoft Office\Office12\msaccess.exe" "c:\users\me\Documents\databasename.accdb" /x macroname
Exit

Open in new window

0
 
ReneGeCommented:
Here it is

While it runs, look at the title bar.

Cheers,
Rene

 
@ECHO OFF

ECHO STARTED AT: %date% %time%
SET CountDownHours=7

:HOME

FOR /L %%A IN (%CountDownHours%,-1,1) DO (
	FOR /L %%B IN (60,-1,1) DO (
		FOR /L %%C IN (60,-1,1) DO (
			TITLE COUNT DOWN: HOUR:%%A MIN:%%B SEC:%%C
			CHOICE /D Y /T 1 >NUL

		)
	)
)

ECHO COMPLETED AT: %date% %time%
ECHO.
PAUSE

Open in new window

0
 
ReneGeCommented:
Oups, I forgot to add your command line:
 
@ECHO OFF

ECHO STARTED AT: %date% %time%
SET CountDownHours=7

:HOME

FOR /L %%A IN (%CountDownHours%,-1,1) DO (
	FOR /L %%B IN (60,-1,1) DO (
		FOR /L %%C IN (60,-1,1) DO (
			TITLE COUNTDOWN: HOUR:%%A MIN:%%B SEC:%%C
			CHOICE /D Y /T 1 >NUL

		)
	)
)

ECHO COUNTDOWN COMPLETED AT: %date% %time%
ECHO RUNNING COMMAND LINE
"C:\Program Files\Microsoft Office\Office12\msaccess.exe" "c:\users\me\Documents\databasename.accdb" /x macroname
ECHO.
ECHO TASK COMPLETED AT: %date% %time%
ECHO.
PAUSE

Open in new window

0
 
Bill PrewCommented:
I think I would approach this differently.  By always delaying a certain amount in the script before running the Access program you will need to start the script at pretty close to the same time every day so it runs at the right time.  Rather than doing that, I would keep checking the system time, and let the script realize when midnight comes, and then run the Access program.

Here's a script that does that.  You will need the Windows Resource Kit sleep command, but that can be downloaded from:

http://www.dynawell.com/download/reskit/microsoft/win2000/sleep.zip

Let me know if you have questions.

@echo off

REM Loop until the hour hits midnight
:WaitUntilMidnight
  for /F "tokens=1-2 delims= :." %%A in ("%TIME%") do set Hour=%%A
  if %Hour% NEQ 0 (
    REM Not midnight yet, wait 10 minutes and check again
    sleep 600
    goto :WaitUntilMidnight
  )

REM Midnight has come, do processing
"C:\Program Files\Microsoft Office\Office12\msaccess.exe" "c:\users\me\Documents\databasename.accdb" /x macroname
exit /b

Open in new window

~bp
0
 
ReneGeCommented:
Inspired by bp's script, I did this one for fun

 
@ECHO OFF

SETLOCAL enabledelayedexpansion

REM THIS IS ASSUMING THAT THE COUNT DOWN WILL FINNISH THE SAME DAY
REM W=WAIT T=TARGET EX.: WHour THour

SET WHour=0
SET WMin=1
SET WSec=10

REM SETTING UP TARGET TIME
FOR /F "tokens=1-3 delims=:, " %%A IN ("%TIME%") DO (
	SET /a THour+=%%A + %WHour%
	SET /a TMin+=%%B + %WMin%
	SET /a TSec+=%%C + %WSec%

	IF !TSec! GTR 60 (
		SET /a TMin+=1
		SET /A TSec-=60
	)
	
	IF !TMin! GTR 60 (
		SET /a THour+=1
		SET /A TMin-=60
	)
	
	IF !THour! GTR 24 (
		ECHO ERROR
		PAUSE
		EXIT
	)
)

REM WAITING FOR THE TARGET TIME TO COME
:HOME
CHOICE /D Y /T 1 >NUL
ECHO %time% | Findstr -i "!THour!:!TMin!:!TSec!"
IF !errorlevel! NEQ 1 GOTO Home

Echo Done

PAUSE

Open in new window

0
 
ReneGeCommented:
By the way, I know it's not working. Just felt like sharing my idea.
0
 
ReneGeCommented:
I'd like to develop this idea so I'll create a new post
0
 
ReneGeCommented:
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Sleep can use the 25200 seconds delay, or, if not available, we can use a ping to a non-existent target:
   ping -w 25200000 1.2.3.4 >nul

Of course there is no progress indicator. And that is just to show that it can be easy even without using Scheduled Tasks (which is definitely the better way to do it).
0
 
Steve KnightIT ConsultancyCommented:
Well I made these comments yesterday in which is apparently a duplicate and about to be deleted... http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_26928553.html

Well you can delay for as long as you want.  If you do have to do it this way then probably best is to use something that will pause for 1 minute or similar and call that so many times or in your case it might be easier to run a loop checking periodically if the hour has passed until it is 0:xx, e.g.

@echo off
:loop
set t=%time:~0,2%
set hour=%t: =%
echo It is currently %time% which is %hour%
if %hour%==0 goto next
  REM Wait a minute by pinging for 60 secs
  ping localhost -n 60 >NUL
goto loop
:next
echo This happens now...

Other way would be to add a check of the time or run a delay at the start of your macro and start it at 5pm?

Steve

Depending upon what your macro is might be as simple as:

do
 
loop until hour(time) =0

Trouble is I know you can do application.wait timeserial(0,10,0) say to wait for 10 minutes BUT which you could put in the loop but that goes to 100% cpu for me at least last time I tried it so have always used stuff outside of Excel etc.  Should be some other delay function that could be used, no doubt with a quick google!

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now