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?
13 Answers
You can use Task Scheduler for this purpose. Open Task Scheduler > Create TaskYou can now define the properties, conditions for running the task, and what job/application to run, in the opened dialog.
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).
Download it from here (UnxUtils.zip).
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
exitYou will need to change two things in the batch file, to make sure it fits your needs.
- First, you will need to change
PATH_TO_FILEon line 7 to the path of the executable you want to launch. - Secondly, you may need to change
Sunon 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).