0

I cannot seem to get the if %day% EQU lines correct. I am trying to get the autoShutDown variable to "T" when the script is run between Friday night 8:30 PM and Saturday at 5:15 PM. But it seems like sometimes the formatting is different and causes the variable assignment to not work correctly.

What's the best way to set the variable? In case it makes a difference, I have Windows 8.1.

@ECHO OFF
SET day=%date:~0,3%
SET time=%time:~0%
set "time=%time: =%"
set currentDate=%date:~4,10%
set autoShutDown=F

if %day% EQU Fri IF %time% GEQ 20:30:00.00 set autoShutDown=T
if %day% EQU Sat IF %time% LEQ 17:15:00.00 set autoShutDown=T 

if "%autoShutDown%"=="T" (
    GOTO AUTOSHUTDOWN
)

GOTO WEEKDAY

:AUTOSHUTDOWN
%WINDIR%\SYSTEM32\SHUTDOWN.EXE /s /t 180 /c "Shutdown in 180 seconds" /d p:4:1

:WEEKDAY

[ EDIT ] So based on what @harrymc wrote below, is the following what I should be doing?

@ECHO OFF
set autoShutDown=F

for /f %%C in ('wmic path Win32_LocalTime Get dayofweek^,Year^,Month^,Day^,Hour^,Minute^,Second /Format:List 2^>nul ^| find "="') do @set current%%C

REM - Friday 8 PM or later
if %currentDayOfWeek% EQU 5 IF %currentHour% GEQ 20 set autoShutDown=T

REM - ...or Saturday earlier than 6 PM
if %currentDayOfWeek% EQU 6 IF %currentHour% LEQ 17 set autoShutDown=T

if "%autoShutDown%"=="T" (
    GOTO AUTOSHUTDOWN
)

GOTO WEEKDAY

:AUTOSHUTDOWN
%WINDIR%\SYSTEM32\SHUTDOWN.EXE /s /t 180 /c "Shutdown in 180 seconds" /d p:4:1

:WEEKDAY
1

The %DATE% is not dependable, because its format is customizable and can change per each user.

This code, meant to run inside a .bat file, will extract all the date and time parameters using the wmic command:

@echo off

for /f %%C in ('wmic path Win32_LocalTime Get dayofweek^,Year^,Month^,Day^,Hour^,Minute^,Second /Format:List 2^>nul ^| find "="') do @set current%%C

:display the variables
set current

This is what it did on Sunday:

enter image description here

  • thank you very much! I added something onto my original post, since it was completely unreadable in a comment here. Is that what you meant? – Eliezer Dec 17 '18 at 2:14
  • Yes, that's the way it can be used. – harrymc Dec 17 '18 at 7:13
  • If this answer helped solve your problem, you might consider accepting it. – harrymc Dec 17 '18 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.