batch - File last modification time with seconds
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.
Hatsune Miku
Updated on September 18, 2022Comments
-
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 outhatsune miku
in the end.-
Rik over 10 yearsAre you really sure that with
dir /od
the files aresorted "randomly"
if made in the same minute? For me thecmd
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 over 10 yearsAnd 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 over 10 yearsI 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 over 2 yearsI don't use cogs but someone else has helped me figure it out in Discord, although thank you for the answer. Really appreciated.