Finding day of week in batch file? (Windows Server 2008)

I'm a little late to this party, but this will give you the day of week:

wmic path win32_localtime get dayofweek

Use a batch for loop to obtain the output, and you have a testable condition for the day of the week.

Rob


Here's the correct answer for a BAT script on a modern system:

@echo off
for /f %%i in ('powershell ^(get-date^).DayOfWeek') do set dow=%%i
echo %dow%

Outputs:

Saturday

Usage:

@echo off
for /f %%i in ('powershell ^(get-date^).DayOfWeek') do set dow=%%i
if %dow% == Saturday goto shutdown
if %dow% == Sunday goto shutdown
exit

:shutdown
shutdown /s /f /t 90

I used this for PC's which auto-boot daily from BIOS, which don't have an option to exclude weekends.

Aside: ACPI is a more reliable way of doing this, and allows for a dummy BAT script such as exit to be used in a Windows Scheduled Task with specific days selected, using the Wake the computer to run this task option.


Here's a batch file that will get all the date and time info into variables but it does depend on you having an appropriate date format set on your machine. Also, the order might need adjusting depending on the local date format, I'm not sure.

@echo off
for /F "tokens=1-4 delims=/ " %%i in ('date /t') do (
set WD=%%i
set D=%%j
set M=%%k
set Y=%%l
) 

for /F "tokens=1-4 delims=: " %%i in ('time /t') do (
set H=%%i
set Mn=%%j
set AP=%%k
)

if "%AP%"=="PM" set /A H=%H%+12

echo %WD% %D%/%M%/%Y% - %H%:%Mn%

The bit you want is in %WD%.


Batch can't do this easily. It requires a lot of command-line fu with the date command. There are a few ways to achieve what you're after, but most of all I'm wondering why the built-in Task Scheduler isn't an option? It allows you specify the days you want it to run and what time:

alt text

You could also use VBScript:

wscript.stdout.writeline weekdayname(weekday(date))

Then run it:

C:\Documents and Settings\Administrator>cscript /nologo dayofweek.vbs
Wednesday

You can easily use if/else logic in VBScript then have your VBScript code execute batch files depending on the day, and vice-versa (have batch call a VBScript).