batch - File last modification time with seconds

5

1

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?

user3133076

Posted 2014-01-10T11:57:53.580

Reputation: 145

Are you really sure that with dir /od the files are sorted "randomly" if made in the same minute? For me the cmd does the sorting correctly (despite the fact it doesn't show the seconds). You can follow these instruction to get the seconds in the property-dialog in explorer to make sure.

– Rik – 2014-01-10T12:57:50.090

1

And if you really want the seconds in your script you might want to consider executing a powerscript-command to get it. See here for a solution.

– Rik – 2014-01-10T13:02:01.667

I controlled again and the files are NOT sorted randomly but in the correct way even if the minute is the same. Maybe I made something wrong the first time I checked. My error. Thank you. @Rik – user3133076 – 2014-01-19T09:05:59.840

Answers

6

Windows Vista / 7 and later

Windows Server 2003 and later

With a little effort you can use forfiles to get the last modified time of a specific file, seconds included:

REM "delims=" is required to avoid stripping AM/PM
for /f "delims=" %%i in ('"forfiles /m filename /c "cmd /c echo @ftime" "') do set modif_time=%%i
echo %modif_time%

Example output

7:33:54 AM

The value displayed is based on the local time of the computer and matches the time shown in the file properties dialog.

Usage help

http://technet.microsoft.com/en-us/library/cc753551.aspx


Windows XP

forfiles.exe is not available out of the box, however you can manually get the required executable. It's an old version which is part of the Windows 2000 Resource Kit. The syntax is case-sensitive and slightly different, and so is the output:

for /f %%i in ('"forfiles.exe -mfilename -c"cmd /c echo @FTIME" "') do set modif_time=%%i
echo %modif_time%

Example output

153354

Here the time value is displayed in the UTC format and is not affected by changes in time zone or daylight saving time. In this example the file was last modified at 15:33:54 (UTC).

Note You can obtain the newer forfiles.exe version by grabbing a copy of the file from any Windows 2003 Server installation or setup media.

and31415

Posted 2014-01-10T11:57:53.580

Reputation: 13 382

0

Use a command interpreter that is capable of it.

Here's how to do it with JP Software's TCC/LE, with some variations on the theme thrown in for good measure:

[C:\Users\JdeBP]touch /c myfile
10/01/2014 18:31:32.710  C:\Users\JdeBP\myfile

[C:\Users\JdeBP]echo myfile was created at %@filetime[myfile,c,s]
myfile was created at 18:31:32

[C:\Users\JdeBP]echo myfile was last accessed at %@filetime[myfile,a,s]
myfile was last accessed at 18:31:32

[C:\Users\JdeBP]for i in (myfile) do set modif_time=%@filetime[%i,w,s]

[C:\Users\JdeBP]echo myfile was last modified at %modif_time%
myfile was last modified at 18:31:32

Further reading

  • JP Software. @FILETIME. Take Command / TCC Help.
  • JP Software. TOUCH. Take Command / TCC Help.

JdeBP

Posted 2014-01-10T11:57:53.580

Reputation: 23 855