问题
I get the file date
from a file:
for %%x in (%file_test%) do set file_date_test=%%~tx
And then I get the system date
:
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%
And then do an if statement
with goto
:
IF %file_date_test% LSS %system_date_test% goto SOME
How can I compare both dates? I would like to check if the file date has more than 24H.
Which is the best way to do that? Can I use forfiles
to do it?
回答1:
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.
For an alternative
robocopy "c:\backup" "c:\backup" "test.bak" /l /nocopy /is /minage:1 > nul
if errorlevel 1 (
echo MATCH
) else (
echo NO_MATCH
)
At least in windows 7, the robocopy
command look at the timestamp of the file to determine its age.
Previous answer
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
)
Or
@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
)
来源:https://stackoverflow.com/questions/31242124/batch-compare-file-date-with-actual-date