3

It is possible to get the current date on a Windows system and the last modification date of a file, like thise:

  • Current date : date /T
  • Last modification date : ±echo %~tI% (where %I is the file within a FOR loop)

Both however depend on regional settings, so they need to be replaced by something else:

  • Current date : wmic os get localdatetime
  • Last modification date : ???

Does somebody know how to fill in the question marks?

Background information, the idea is to get a result like the following:

  • Current date : 20170323115047.782000+060
  • Last modification date : 20170323120513.0123

Chop off the first eight characters (in order to get the day)

  • Current date : 20170323
  • Last modification date : 20170323

This makes it possible to see that the file has indeed been modified today.
I want to avoid regional settings in order to be sure of the amount of characters I need to chop off.

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • Although `date /T` and `%%~tI` return the locale-dependent date formats, they can still be used for (non-)equality comparisons as the format are the same, given that you split off the time portion of `%%~tI` at the first _space_, and that the _space_ is not used as date separator (which is very unlikely)... – aschipfl Mar 24 '17 at 10:38

6 Answers6

5

For timestamp comparisons, you could use forfiles to find files modified today. This solution is more graceful than chopping substrings off of WMI query results I think.

@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 a cmd console for more info. The forfiles command exits 0 on match, 1 on no match, allowing the use of conditional execution. Also, see this related question.

Community
  • 1
  • 1
rojo
  • 24,000
  • 5
  • 55
  • 101
4

The following batch output it's own 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

Sample output

> 12-16.cmd
File:Q:\\Test\\2017-03\\23\\12-16.cmd lastmodified:20170323123900.908030+060
4

LotPings' script gives you the idea how to start but there's a little bit more on complete check. Try this:

@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

Here's substring is explained : https://ss64.com/nt/syntax-substring.html

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • 1
    ++npocmaka, but where's your `setlocal`? Its conspicuous exclusion is bothering my OCD. – rojo Mar 23 '17 at 14:52
4

Rojo is correct that FORFILES can be used to list files that have been modified today. But it is S.L..O...W.....

ROBOCOPY is much faster :-)

The following will list recently modified files in the current directory.

robocopy . . /l /is /maxage:1 /njh /njs /nc /ns /ndl /fp

You can specify a specific path if you want

robocopy "c:\your\path" "c:\your\path" /l /is /maxage:1 /njh /njs /nc /ns /ndl /fp

You can add the /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
dbenham
  • 127,446
  • 28
  • 251
  • 390
2

Although date /T/%DATE% and %%~tI return the locale-dependent date formats, they can still be used for (non-)equality comparisons as the formats are the same, given that you split off the time portion of %%~tI at the first SPACE, and that the SPACE is not used as date separator (which is very unlikely and uncommon):

for %%I in ("C:\TEMP\test.txt") do set "AGE=%%~tI"
set "AGE=%AGE: =" & rem "%"
if "%AGE%"=="%DATE%" echo File has been modified today.
aschipfl
  • 33,626
  • 12
  • 54
  • 99
0

After quite some work, I've found a way to get everything up and running (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, ...))

Dominique
  • 16,450
  • 15
  • 56
  • 112