我似乎无法得到如果 %day% EQU行正确。我正在尝试自动关机当脚本在星期五晚上 8:30 到星期六下午 5:15 之间运行时,变量被赋值为“T”。但有时格式似乎不同,导致变量赋值无法正常工作。
设置变量的最佳方法是什么?以防万一,我有 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
[编辑] 那么根据@harrymc 下面所写的内容,我应该做什么吗?
@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
这%DATE%
是不可靠的,因为它的格式是可定制的,并且可以按照每个用户而改变。
此代码用于在.bat
文件中运行,将使用以下方法提取所有日期和时间参数:
wmic 命令:
@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
以下是它在周日所做的事情: