Search code examples
batch-filedate-format

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.


Solution

  • 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!