We help IT Professionals succeed at work.

Bat File - If date = this, then run that, else quit

BILLBACKUS
BILLBACKUS asked
on
I want to run a bat file every morning, have it look at the date, and compare it against a list of dates.  If today is on the list, I want to execute a command.  If today is not on the list, simply end.

So for example:
01/30/08
02/28/08
03/30/08

If today is one of these dates, run program.exe

can someone whip up a bat file that will do this?

Thanks,
Greg
Comment
Watch Question

@echo off
For /f "tokens=1-7 delims=:/-, " %%i in ('echo exit^|cmd /q /k"prompt $D $T"') do (
      For /f "tokens=2-4 delims=/-,() skip=1" %%a in ('echo.^|date') do (
            set dow=%%i
            set %%a=%%j
            set %%b=%%k
            set %%c=%%l
            set hh=%%m
            set min=%%n
            set ss=%%o
      )
)

set today=%mm%/%dd%/%yy%

if "%today%"=="01/30/2008" GOTO ENDOFJAN
if "%today%"=="02/28/2008" GOTO ENDOFFEB
if "%today%"=="03/20/2008" GOTO ENDOFMAR
GOTO END

:ENDOFJAN
REM STUFF GOES HERE
GOTO END

:ENDOFFEB
REM STUFF GOES HERE
GOTO END

:ENDOFMAR
REM STUFF GOES HERE
GOTO END


:END
@echo off
For /f "tokens=1-7 delims=:/-, " %%i in ('echo exit^|cmd /q /k"prompt $D $T"') do (
      For /f "tokens=2-4 delims=/-,() skip=1" %%a in ('echo.^|date') do (
            set dow=%%i
            set %%a=%%j
            set %%b=%%k
            set %%c=%%l
            set hh=%%m
            set min=%%n
            set ss=%%o
      )
)

set today=%mm%/%dd%/%yy%

if "%today%"=="01/30/2008" GOTO RUNPROG
if "%today%"=="02/28/2008" GOTO RUNPROG
if "%today%"=="02/28/2008" GOTO RUNPROG
GOTO END

:RUNPROG
program.exe

:END
Sorry, last 02/28/2008 should be 03/30/2008

Commented:
If you are keeping the Dates in a separate file, you can use the following two lines. Note: the date format should be MM/DD/YYYY .

For /f "tokens=1,2" %%A in ('date /T') DO SET SysDate=%%B
For /f %%G in (dates.txt) Do If %SysDate%==%%G ECHO Yes


Your date file should look like this (dates.txt)
01/30/2008
02/28/2008
03/30/2008
01/28/2008

In the second line above, just delete "ECHO YES" and put in your command or batch to launch.

Author

Commented:
perfect thanks!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.