0

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.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Premanshu
  • 616
  • 3
  • 14
  • 24
  • Possible duplicate of [Setting a windows batch file variable to the day of the week](https://stackoverflow.com/questions/11364147/setting-a-windows-batch-file-variable-to-the-day-of-the-week) – phuclv Jun 05 '18 at 01:52

2 Answers2

2

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!
Endoro
  • 37,015
  • 8
  • 50
  • 63
1

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.

LS_ᴅᴇᴠ
  • 10,823
  • 1
  • 23
  • 46
  • saturday = 6, while Sunday = 7 on my system....Is it anything to do with local dependency or are these codes universal in nature!! – Premanshu Aug 07 '13 at 12:25
  • Hum, my machine also tells me that today, Wed, is 3... However, MSDN states that "Current day of the current week that matches the query (1–7). By convention, the value 1 (one) is always Sunday, regardless of the culture or the locale set on the machine." in http://msdn.microsoft.com/en-us/library/windows/desktop/aa393244(v=vs.85).aspx ... – LS_ᴅᴇᴠ Aug 07 '13 at 12:50