2

I have some batch files that currently run every day that basically open system files as soon as the system data is updated in the morning (which can be at different times depending on the day).

The current batch files (created from CMD) all run a simple start:... command to open the files.

I am looking for a way to only run some of the batch files on Mondays and Thursdays, but not open the programs on any other day. Basically if the batch file runs every day, it would do nothing unless it was Monday or Thursday, and then it would open the system file.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user3456594
  • 29
  • 1
  • 3
  • looks like you need an "if" statement and a way to determine the current day of week ... here is a question for the day of week issue (it isn't as simple as you might like) http://stackoverflow.com/questions/11364147/setting-a-windows-batch-file-variable-to-the-day-of-the-week – TCC Jul 28 '14 at 15:48

2 Answers2

3

You could find the day of the week using

wmic path win32_localtime get dayofweek

which will give you a number refering to the day of the week (this changes based on your local settings, but usually Sunday is 0).

You could use this and an if statement to decide whether the code should run.

TheDarkTurtle
  • 413
  • 1
  • 3
  • 12
  • Helpful, thanks. To get it into a variable: `for /F "skip=1 tokens=*" %%a in ('wmic path win32_localtime get dayofweek') do if not defined dayofweek set dayofweek=%%a`, See https://stackoverflow.com/a/45421395/550712. – Mark Berry Jul 31 '17 at 16:52
0

Here's some code from many many moons ago:

@echo off
::
:: uses Windows Scripting Host
:: to set a variable to the current day number
:: for Win9x/ME/NT/W2K/XP
set amp=&
if not "%amp%"=="&" set amp=^^^&
set OF="%temp%.\tmp.vbs"
 >%OF% echo n=weekday(now)
>>%OF% echo WScript.Echo "set day=" %amp% n
cscript //nologo "%temp%.\tmp.vbs" > "%temp%.\tmp.bat"
call "%temp%.\tmp.bat"
del  "%temp%.\tmp.bat"
del  %OF%

if "%day%"=="1" echo Sunday 
if "%day%"=="2" echo Monday 
if "%day%"=="3" echo Tuesday 
if "%day%"=="4" echo Wednesday 
if "%day%"=="5" echo Thursday 
if "%day%"=="6" echo Friday 
if "%day%"=="7" echo Saturday 
foxidrive
  • 40,353
  • 10
  • 53
  • 68