Sunday, August 4, 2019

batch - File last modification time with seconds


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?


Answer



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



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.


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...