我有一个批处理文件,在该批处理文件中,我需要根据服务器的时间运行两个命令之一。
如果时间介于22:00:00和03:30:00之间 - xcopy / Y a \ 1.txt c \ 1.txt
如果时间在此范围之前或之后 - - xcopy / Y b \ 1.txt c \ 1.txt
这将使用xcopy根据时间来回切换文件。
我知道这很容易,但我的大脑不会起作用吗?lol
编辑:
22:00和4:00 ...这是我想出来的,但它似乎不是最好的方式......
set current_time =%time:~0.5%
if“%current_time%”lss“22:00”goto daycycle
if“%current_time%”gtr“4:00”goto daycycle
echo在晚上10点到凌晨4点之间进行此操作
转到继续
:daycycle
echo在晚上10点之前和凌晨4点之后做这个。
:继续
答案 0 :(得分:6)
@echo off
setlocal enableextensions disabledelayedexpansion
set "now=%time: =0%"
set "task=day"
if "%now%" lss "03:30:00,00" ( set "task=night" )
if "%now%" geq "22:00:00,00" ( set "task=night" )
call :task_%task%
endlocal
exit /b
:task_day
:: do daily task
goto :eof
:task_night
:: do nightly task
goto :eof
已编辑 - 以前的代码应该在原始问题的条件下工作。但是在不同的时间配置中会失败。这应该解决通常的问题
@echo off
setlocal enableextensions disabledelayedexpansion
call :getTime now
set "task=day"
if "%now%" lss "03:30:00,00" ( set "task=night" )
if "%now%" geq "22:00:00,00" ( set "task=night" )
call :task_%task%
echo %now%
endlocal
exit /b
:task_day
:: do daily task
goto :eof
:task_night
:: do nightly task
goto :eof
:: getTime
:: This routine returns the current (or passed as argument) time
:: in the form hh:mm:ss,cc in 24h format, with two digits in each
:: of the segments, 0 prefixed where needed.
:getTime returnVar [time]
setlocal enableextensions disabledelayedexpansion
:: Retrieve parameters if present. Else take current time
if "%~2"=="" ( set "t=%time%" ) else ( set "t=%~2" )
:: Test if time contains "correct" (usual) data. Else try something else
echo(%t%|findstr /i /r /x /c:"[0-9:,.apm -]*" >nul || (
set "t="
for /f "tokens=2" %%a in ('2^>nul robocopy "|" . /njh') do (
if not defined t set "t=%%a,00"
)
rem If we do not have a valid time string, leave
if not defined t exit /b
)
:: Check if 24h time adjust is needed
if not "%t:pm=%"=="%t%" (set "p=12" ) else (set "p=0")
:: Separate the elements of the time string
for /f "tokens=1-5 delims=:.,-PpAaMm " %%a in ("%t%") do (
set "h=%%a" & set "m=00%%b" & set "s=00%%c" & set "c=00%%d"
)
:: Adjust the hour part of the time string
set /a "h=100%h%+%p%"
:: Clean up and return the new time string
endlocal & if not "%~1"=="" set "%~1=%h:~-2%:%m:~-2%:%s:~-2%,%c:~-2%" & exit /b
答案 1 :(得分:0)
试试这个:
@echo off
set hour=%time:~0,2%
set min=%time:~3,2%
if %hour% GEQ 22 (
xcopy /Y a\1.txt c\1.txt
) ELSE (
if %hour% LEQ 3 (
if %hour% EQU 3 if %min% GTR 30 (
xcopy /Y b\1.txt c\1.txt
goto :END
)
xcopy /Y a\1.txt c\1.txt
) ELSE (
xcopy /Y b\1.txt c\1.txt
goto :END
)
)
:END
我相信我会做你想做的事。获胜的手动if
声明!请注意,如果它是22:00
到4:00
或3:00
,那将会容易得多。我不得不使用:30
分钟检查员。
但是,它可能不起作用,所以只需在将它放到服务器上之前检查它。
Monacraft。
答案 2 :(得分:-1)
应该做些什么:
@echo off
:loop
set hour=%time:~0,2%
set min=%time:~3,2%
cls
ECHO %hour%
ECHO %min%
ECHO This %I%
IF %hour% == 14 GOTO Test2
goto loop
:Test2
IF %min% == 58 GOTO YUP
IF %min% == 59 GOTO LATE
Goto loop
:YUP
SET I=0
GOTO loop
:LATE
SET I=NOPE
GOTO loop