batch - File last modification time with seconds

105

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.

Share:
105
Hatsune Miku
Author by

Hatsune Miku

Updated on September 18, 2022

Comments

  • Hatsune Miku
    Hatsune Miku over 1 year

    I am working on this command for a bot and I am not sure how I would get the text after an option, so like: /userinfo name: Hatsune Miku and prints out hatsune miku in the end.

    • Rik
      Rik over 10 years
      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
      Rik over 10 years
      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.
    • user3133076
      user3133076 over 10 years
      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
  • Hatsune Miku
    Hatsune Miku over 2 years
    I don't use cogs but someone else has helped me figure it out in Discord, although thank you for the answer. Really appreciated.