批处理 - 比较文件的日期与实际日期 [英] Batch - Compare file date with actual date

查看:402
本文介绍了批处理 - 比较文件的日期与实际日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的文件日期从文件:

for %%x in (%file_test%) do set file_date_test=%%~tx

然后我得到的系统日期

set year=%date:~-4%
set month=%date:~3,2%
if "%month:~0,1%" == " " set month=0%month:~1,1%
set day=%date:~0,2%
if "%day:~0,1%" == " " set day=0%day:~1,1%

set hour=%time:~0,2%
if "%hour:~0,1%" == " " set hour=0%hour:~1,1%
set min=%time:~3,2%
if "%min:~0,1%" == " " set min=0%min:~1,1%

然后做一个 if语句转到

IF %file_date_test% LSS %system_date_test% goto SOME

我如何比较两个日期?我想检查文件的日期有 24h以上

这是做的最好的方法?我可以用 FORFILES 办呢?

Which is the best way to do that? Can I use forfiles to do it?

推荐答案

编辑 FORFILES 命令甚至不会看文件的时间如果文件中有不同的日期,但不超过24h所以previous答案(在有人的情况下底部发现它很有用)将无法工作。

Edited forfiles command will not even look at the hour of the file, so the previous answer (at the bottom in case someone find it useful) will not work if the file has different date but less than 24h.

有关替代

robocopy "c:\backup" "c:\backup" "test.bak" /l /nocopy /is /minage:1 > nul 
if errorlevel 1 (
    echo MATCH
) else (
    echo NO_MATCH
)

至少在Windows 7中,的Robocopy 命令看一下文件的时间戳来确定它的年龄。

At least in windows 7, the robocopy command look at the timestamp of the file to determine its age.

previous答案

您可以使用 FORFILES 检查操作的错误级别

You can use forfiles checking the errorlevel of the operation

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "file_test=c:\backups\test.bak"

    for %%a in ("%file_test%") do (
        forfiles /p "%%~dpa." /m "%%~nxa" /d -1 >nul 2>nul && echo MATCH || echo NO MATCH
    )

或者

@echo off
    setlocal enableextensions disabledelayedexpansion

    forfiles /p "c:\backups" /m "test.bak" /d -1 >nul 2>nul
    if errorlevel 1 (
        echo NO_MATCH
    ) else (
        echo MATCH
    )

这篇关于批处理 - 比较文件的日期与实际日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆