How can I compare today's date with the last modified date of a file without being affected by regional settings?
You can get the current date on Windows and the last modified date of the file, for example thise:
- The current date:
date /T
- Last Modified Date: ±
echo %~tI%
(where%I
is the file in the loopFOR
)
They both depend on regional settings, so they need to be replaced with something else:
- The current date:
wmic os get localdatetime
- Date of last modification:
Does anyone know how to fill in question marks?
Background information, the idea is to get the result like this:
- Current date: 20170323115047.782000 + 060
- Date of last modification: 20170323120513.0123
Cut off the first eight characters (to get the day)
- Current date: 20170323
- Last Modified Date: 20170323
This allows you to see that the file was actually changed today.
I want to avoid regional settings to be sure of the number of characters I need to chop off.
source to share
The next batch output is the lastmodified datetime via wmic.
@Echo off&SetLocal
set File=%~f0
:: need to escape \ with another one \\
set File=%File:\=\\%
for /f %%a in (
'wmic DataFile where "Name='%File%'" get lastmodified ^| findstr "^[0-9]" '
) do Echo File:%File% lastmodified:%%a
Output example
> 12-16.cmd
File:Q:\\Test\\2017-03\\23\\12-16.cmd lastmodified:20170323123900.908030+060
source to share
The LotPings' script gives you an idea of how to get started, but there's a little more in there when fully checked out. Try the following:
@echo off
setlocal
::change to the needed file or to the command line argument
set fileLoc=%~f0
set fileLoc=%fileLoc:\=\\%
for /f "tokens=* delims=" %%# in ('wmic os get localdatetime /format:value') do (
for /f "tokens=* delims=" %%a in ("%%#") do set "%%a"
)
::echo %LocalDateTime%
set today=%LocalDateTime:~0,8%
echo today: %today%
for /f "tokens=* delims=" %%# in ('wmic DataFile where "Name='%fileLoc%'" get lastmodified /format:Value ') do (
for /f "tokens=* delims=" %%a in ("%%#") do set "%%a"
)
echo %lastmodified%
set lmodified=%lastmodified:~0,8%
if "%lmodified%" equ "%lmodified%" (
echo modified today
) else (
echo hasnt been modified today
)
endlocal
The substring is explained here: https://ss64.com/nt/syntax-substring.html
source to share
For timestamp mappings, you can use forfiles
to find files changed today. I think this solution is more graceful than chopping up substrings from the results of a WMI query.
@echo off & setlocal
set "file=notes.txt"
forfiles /d +0 /m "%file%" >NUL 2>NUL && (
echo %file% was modified today.
) || (
for %%I in ("%file%") do echo %%~I was last modified %%~tI
)
forfiles /?
in the cmd console for more information. The command forfiles
returns 0 by match, 1 does not match, allowing conditional execution . Also, see this related question .
source to share
Rojo is correct that FORFILES
you can use it to display files that have changed today. But this is SL.O ... W .....
ROBOCOPY
runs much faster :-)
Listed below are the recently changed files in the current directory.
robocopy . . /l /is /maxage:1 /njh /njs /nc /ns /ndl /fp
You can specify a specific path if you like
robocopy "c:\your\path" "c:\your\path" /l /is /maxage:1 /njh /njs /nc /ns /ndl /fp
You can add /S
to recursively search all child folders
robocopy "c:\your\path" "c:\your\path" /l /is /maxage:1 /njh /njs /nc /ns /ndl /fp /s
source to share
After some work, I found a way to get everything and run (based on Lotping's answer):
set Filename=F:\\DIR\\Filename.exe
for /f %%a in ('wmic datafile where "Name='!Filename!'" get "lastmodified"^|findstr [0-9]') do set Filename_DATE=%%a
echo Filename_DATE=!Filename_DATE!
set FilenameFinalDATE=!Filename_DATE:~0,8!
echo !FilenameFinalDATE!
(Beware, everything is important here (double slashes, double percentages, single and double quotes, ...))
source to share
Although date /T
/ %DATE%
and %%~tI
return locale-dependent date formats, they can still be used for (non) equality comparisons, since the formats are the same, given that you separated the time portion %%~tI
on the first SPACEand that SPACEit is not used as a date separator (which is very unlikely and unusual ):
for %%I in ("C:\TEMP\test.txt") do set "AGE=%%~tI" set "AGE=%AGE: =" & rem "%" if "%AGE%"=="%DATE%" echo File has been modified today.
source to share