Run Batch File at Specific Time

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Run Batch File at Specific Time

#1 Post by Samir » 16 Jul 2013 14:39

I needed something to do this that was pure command line DOS, so I made this. I'm looking for feedback for any improvements. 8)

Code: Select all

:START
PING -n 60 127.0.0.1 > NUL
SET CURTIME=%TIME:~0,-6%
IF NOT "%CURTIME%"=="10:00" GOTO START

CODE TO EXECUTE

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Run Batch File at Specific Time

#2 Post by penpen » 16 Jul 2013 16:30

If you count all executables in %SystemRoot%\system32 to pure command line dos i would prefer cscript.exe:

Code: Select all

@if (true == false) then /*
   @echo off
   %SystemRoot%\system32\cscript.exe //E:JScript "%~f0" "10:00"
::   code to execute
   goto :eof
*/
@end
if (WScript.Arguments.Unnamed.Length > 0) {
   var wait = WScript.Arguments.Unnamed.Item (0);
   var date = new Date ();
   var until = new Date (date.getYear (), date.getMonth (), date.getDate () , parseInt (wait.substring (0,2)), parseInt (wait.substring (3,5)), 0,0);
   var diff = 0;

   // EDIT: added this line to do the same as in the above batch:
   // EDIT 2: corrected the line...
   if ((until.getTime () - date.getTime ()) < 0) until.setDate (until.getDate () + 1);

   do {   // maybe this program may be waked up by some security manager, so loop
      date = new Date ();
      diff = until.getTime () - date.getTime ();
      WScript.sleep (diff);
   } while (diff > 0)
}

If not, you should call ping using %SystemRoot%\system32\ping.exe to make its call safer.

penpen
Last edited by penpen on 17 Jul 2013 02:37, edited 2 times in total.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Run Batch File at Specific Time

#3 Post by Samir » 16 Jul 2013 21:29

Neat! I didn't even know about cscript. :) But I did want to stay at a 'pure' dos level if possible.

To make ping safer, you mean to explicitly specify where the exe is vs letting the path determine the location? :?:

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Run Batch File at Specific Time

#4 Post by penpen » 17 Jul 2013 01:19

Definitely that.
If you don't exactly specify which executable to run, someone may execute his exe file instead of yours, with your rights as you call it.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Run Batch File at Specific Time

#5 Post by foxidrive » 17 Jul 2013 01:58

That's true - but think of all the other tools that you would also have to prepend the path.

find, findstr, xcopy, robocopy, and scads more external commands.

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Run Batch File at Specific Time

#6 Post by penpen » 17 Jul 2013 04:54

Normally i using the executables without the path prior to it, too,
but i then add the following at the beginning of a batch file and run it:

Code: Select all

@echo off
setlocal
for /f "tokens=2* delims=:" %%a in ('%SystemRoot%\system32\find /C    "#" "%~f0"') do set /a "lines=%%b"
for /f "tokens=2* delims=:" %%a in ('%SystemRoot%\system32\find /C /V "#" "%~f0"') do set /a "lines+=%%b"
for /f "tokens=1,* delims=:" %%a in ('findstr /N /R /C:"^:: delete up to this line.*" "%~f0"') do (
   set /a "from=%%a"
   goto :next
)


:next
setlocal enableDelayedExpansion

(
   for /l %%a in (1,1,%lines%) do (
      set "INPUT="
      set /p "INPUT="

      if %%a GTR %from% (
         if defined INPUT (
            set "INPUT=!INPUT:findstr=%%SystemRoot%%\system32\findstr!"
            set "INPUT=!INPUT:xcopy=%%SystemRoot%%\system32\xcopy!"
            echo(!INPUT!
         ) else (
            echo(
         )
      )
   )
)< "%~f0" > "%~f0.bat"
endlocal
goto:eof
(
   del "%~f0"
   move "%~f0.bat" "%~f0"
   exit /b 0
)


:: delete up to this line
::   findstr !
::   xcopy   !
The last 2 lines are just to show this in a single batch.

penpen

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Run Batch File at Specific Time

#7 Post by Squashman » 17 Jul 2013 06:31

AT is still supported. Why not use that?

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Run Batch File at Specific Time

#8 Post by Samir » 17 Jul 2013 10:37

penpen wrote:Definitely that.
If you don't exactly specify which executable to run, someone may execute his exe file instead of yours, with your rights as you call it.
Gotcha. Makes sense. 8)
foxidrive wrote:That's true - but think of all the other tools that you would also have to prepend the path.

find, findstr, xcopy, robocopy, and scads more external commands.
This was my thinking as well. It really depends on how secure the execution is going to be. This is on a locked server sitting in a closet by itself. No one can even execute commands from the command line. :)
Squashman wrote:AT is still supported. Why not use that?
I actually just found out about that, but it's still a windows service. I wanted something a lot more command-line based.

Post Reply