Premanshu
Premanshu

Reputation: 626

run a batch script only on weekdays/selected days

I have a batch file to take DB backup, scheduled to run at a selected time everyday. But I don't want to run the same on weekends or say on selected days. On my server, %DATE% returns 07/08/2013. So doing %DATE:~0,3% returns 07/ instead of day.

Is there a way to return day and then using an if-else, I can guide the program to flow as per the requirement.

My server runs Windows server 2003.

Upvotes: 0

Views: 3001

Answers (2)

LS_ᴅᴇᴠ
LS_ᴅᴇᴠ

Reputation: 11151

I'm learning about WMI, and following should run fine:

FOR /F "TOKENS=1,* DELIMS==" %%v IN ('WMIC Path Win32_LocalTime Get /FORMAT:VALUE') DO IF "%%v" == "DayOfWeek" SET DayOfWeek=%%w
IF %DayOfWeek% == 0 REM Do whatever you want for Sunday
...
IF %DayOfWeek% == 6 REM Do whatever you want for Saturday

EDIT: Changed, according comment on Win32_LocalTime class stating documentation is wrong.

Upvotes: 1

Endoro
Endoro

Reputation: 37569

Example for weekend:

@ECHO OFF &SETLOCAL
for /f "tokens=2*" %%a in ('reg query "HKCU\Control Panel\International" /v sShortDate^|find "REG_SZ"') do set "ssShortDate=%%b"
reg add "HKCU\Control Panel\International" /f /v sShortDate /d "d" >nul
FOR /f %%a IN ("%date%") DO SET "dow=%%a"
reg add "HKCU\Control Panel\International" /f /v sShortDate /d "%ssShortDate%" >NUL
IF /i "%dow:~0,1%"=="s" ECHO weekend!

Upvotes: 2

Related Questions