Question

I want to know when a file has been modified for the last time.

I can get these infos using the following batch script:

FOR %%i IN (myfile) DO SET modif_time=%%~ti

The problem is that I need the second of the last modification and the command %~t returns the date and the time with only hours and minutes.

I can only check the seconds by manually viewing the "property window" file by file.

How can I get the time with seconds in batch?

Was it helpful?

Solution

wmic datafile where name="FileName" get LastModified

FileName must contain the full path with double backspaces.


Sorting example (recent first):

for /f "delims=" %a in ('wmic datafile where "drive='c:' and path='\\windows\\'" get LastModified^,Name /format:table^|find ":"^|sort /r') do @echo %a

OTHER TIPS

In Windows 7 and forward (or via Resource Kit for XP) you can use forfiles for this. Something like:

forfiles /m *.* /c "cmd /c ECHO The last modified date of: @file is: @ftime"

In a directory with the following files:

myTest.txt
myTest2.txt

I get:

The last modified date of: "myTest.txt" is: 13:21:07
The last modified date of: "myTest2.txt" is: 13:21:20
wmic datafile where "drive='c:' and path='\\windows\\'" get "Last Modified",Name

You can embed a small JScript in the batch file to get the last modified epoch:

@if (@a==@b) @end /*
@echo off
SetLocal EnableDelayedExpansion

set EPOCH=0
FOR /F "delims=" %%D in ('cscript /nologo /e:jscript "%~f0" "%1"') do (
    set EPOCH=%%D
)
echo Last modified (epoch-seconds): !EPOCH!

goto :eof
*/

var fs = new ActiveXObject("Scripting.FileSystemObject");
var filename = WSH.Arguments(0)
var millis = -1
if (fs.FileExists(filename))
{
    var file = fs.GetFile(filename);
    millis = Date.parse(file.DateLastModified) / 1000;
}
WSH.Echo(millis);

The /* */ will comment out the batch script while running as a JScript, and the @if (@a==@b) @end and goto :eof will skip the JScript while running as a batch script

>epoch.bat epoch.bat
Last modified (epoch-seconds): 1533282229

>epoch.bat "epoch.bat"
Last modified (epoch-seconds): 1533282229

>epoch.bat notareal.file
Last modified (epoch-seconds): -1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top