# Batch file that run on a special day

How do I create a batch file that once clicked on, starts an .exe file if it is a Sunday?

I do not want it to run the .exe file if it is on another day. If for example it is Friday I just want it to close it self and do nothing.

Also it would be nice if it was possible to run it in the background without showing any kind of command prompt?

-
What have you tried so far? –  Dave M Jan 26 at 19:50
Please don't vandalize your own posts. If you feel strongly about having a question disassociated with your account, contact the team: superuser.com/contact –  slhck Jan 29 at 20:19

You can use Task Scheduler for this purpose. Open Task Scheduler > Create Task You can now define the properties, conditions for running the task, and what job/application to run, in the opened dialog.

-
@Martin - why not??? - It can start an executable, and it can be scheduled to run weekly on a particular day of the week at a particular time. So where is the problem? –  dbenham Jan 26 at 21:45
@Martin That's what your scheduler can do!, Just schedule your executable directly and you can see the option of executing when the user is logged in above image^ Trust me, Windows Task scheduler is the perfect solution for your task :) –  Saurabh Sharma Jan 27 at 18:03
@Martin You can schedule a task whenever a user logs in on a particular day. –  Saurabh Sharma Jan 28 at 4:52

Tha fact is that you want to use an advanced feature of date command (Day Of Week) that Windows is unable to manage in DOS Batch.

For this, i would use a third party binary file date.exe, that comes from Unix world and that works on Windows : UnxUtils (that is a collection of common GNU Unix utilities that runs on Windows).

Then extract it, and delete all files except date.exe

Then, write a batch file like this (assuming date.exe is located on c:\) :

@ECHO OFF
REM Sunday is 0
for /f "delims=" %%a in ('c:\date.exe +%%w') do set DayOfWeek=%%a
if %DayOfWeek% == 0 (
cmd /c c:\path\to\exe\file.exe
) else (
echo Do Nothing
)


You can put it in the Windows startup folder and will work on all Windows version, regardless where you are in the world.

Also (much better), in those days, DOS Batch are a bit obsolete. As you are talking about Windows 8 i would consider Powershell (can be called by a batch file if needed) :

$a = Get-Date if($a.DayOfWeek -eq "Sunday") {
cmd /c c:\path\to\exe\file.exe
}


Here, no need of third party binary file : Powershell is Powerfull ;)

-

According to HERE, it is impossible to execute a batch file with the built in Command Prompt, and not show a window.

So, eventually, I landed upon THIS page, and used the information to make a batch file that will do what you want it to. What the batch script will do, is check the time. If it is not the correct day, the Command Prompt will flash red, and exit. If it is the correct day, it will flash green, and launch your program, while exiting.

All of the stuff in the above paragraph happens in less than a second (depending on your computers speed), and therefore won't be very noticeable.

@echo off

mode con: cols=16 lines=6

:: The file you want to launch.
set FILE=PATH_TO_FILE

:: The day you want to launch the file on.
set DAY=Sun

for /F "tokens=1 delims= " %%A in ('Date /t') do ( Set TODAY=%%A )
if %TODAY% == %DAY% goto START

:STOP
color 47
goto END

:START
color 27

start "" "%FILE%"
goto END

:END
exit


You will need to change two things in the batch file, to make sure it fits your needs.

1. First, you will need to change PATH_TO_FILE on line 7 to the path of the executable you want to launch.
2. Secondly, you may need to change Sun on line 10. This may need to be changed to the first 3 letters of the day you want to launch the executable on (if you don't want to launch it on Sunday).
-
@Martin The file path you wrote will work. Also, I tested the batch file again, and it works fine for me. Are you sure that the day in the batch file, and your computer's date are the same? –  joejoe31b Jan 27 at 20:35
@joejoe31b This is system dependant and will not work on 90% computers on the planet. Mine gives me date under format dd/mm/YYYY, so no trace of Sun or anything like this. –  user2196728 Jan 27 at 22:15
@user2196728 I have fixed the batch script. It should work how it's supposed to now. –  joejoe31b Jan 28 at 3:18