batch-file - 如果时间是晚上 10 点至凌晨 4 点,则批处理文件运行 cmd1,否则运行 cmd2

标签 batch-file

我有一个批处理文件,在该批处理文件中,我需要根据服务器的时间运行两个命令之一。

如果时间在 22:00:00 到 03:30:00 之间 -- xcopy/Y a\1.txt c\1.txt

如果时间在此范围之前或之后 -- -- xcopy/Y b\1.txt c\1.txt

这将使用 xcopy 根据时间来回切换文件。

我知道这很容易,但我的大脑无法正常工作,哈哈

编辑:

选择 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 Do this between 10pm and 4am

goto continue 

:daycycle 

echo Do this before 10pm and after 4am

:continue

最佳答案

@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

关于batch-file - 如果时间是晚上 10 点至凌晨 4 点,则批处理文件运行 cmd1,否则运行 cmd2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21809027/

相关文章:

java - 如何引用我的批处理文件中的所有外部 jar 文件

Windows Batch 根据文件名创建文件夹

scheduled-tasks - 如何使用 Windows 任务调度程序执行 cscript?

python - 使用 .bat 更改目录并运行 Jupyter

batch-file - 映射网络驱动器而不在批处理文件中对驱动器号进行硬编码

windows - 批处理脚本在 Windows 中按计划运行

java - 在从批处理文件调用 Java 程序时需要帮助

batch-file - Windows 批处理文件中等效的 Switch 语句

Python:MySQL批量插入,跳过外键约束失败的条目

sql-server - bcp 命令输出汉字而不是表格