I want Windows to get the file update time (same as in Explorer)

Asked 2 years ago, Updated 2 years ago, 327 views

st_mtime in os.stat seems to change to the time when it was copied.Is there any way to get the same update time as the explorer display?

Note:

I'd like to make an update comparison of my work, so I'd like to get the same update time as the navigator display.

As you said, it would be nice if I could copy it while keeping the update time information, but sometimes non-engineers organize files, so I can't do it all with Python commands.Even in those cases, I wanted to get this because the update time displayed in the explorer is kept at the update time at the time of work.

Personally, I looked it up and found that NTFS's File Modified Time (ATime) corresponds to this, but I can't find a way to get it.

python windows

2022-09-30 21:56

1 Answers

Answering Similar Questions on stackoverflow.com
https://stackoverflow.com/a/38524858/4366193

This answer appears to be calling Win32 API directly to get attribute information for the file. (This will be a Python code dependent on the Windows OS)

Verified in Windows 10 (64bit), Python 3.9.4.

 Python 3.9.4 (tags/v3.9.4:1f2e308, Apr 6 2021, 13:40:21) [MSC v.1928 64bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> from ctypes import windll, Structure, byref
>>> from ctypes.wintypes import LPWSTR, DWORD, FILETIME
>>
>>class WIN32_FILE_ATTRIBUTE_DATA(Structure):
...     _fields_=[("dwFileAttributes", DWORD),
...                 ("ftCreationTime", FILETIME),
...                 ("ftLastAccessTime", FILETIME),
...                 ("ftLastWriteTime", FILETIME),
...                 ("nFileSizeHigh", DWORD),
...                 ("nFileSizeLow", DWORD)]
...
>> filename = 'D:\\test.csv'
>>
>>>wfad=WIN32_FILE_ATTRIBUTE_DATA()
>>GetFileExInfoStandard=0
>>windll.kernel32.GetFileAttributesExW (LPWSTR(filename), GetFileExInfoStandard, byref(wfad))
1
>>>lowtime=wfad.ftLastWriteTime.dwLowDateTime
>>lighttime=wfad.ftLastWriteTime.dwHighDateTime
>>filetime=(hightime<<32)+lowtime
>> print (filetime)
132003113268921297

However, the data that can be retrieved will be FILETIME structure, so it will be difficult to use without converting it to Unix epoch time.

https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime
https://docs.microsoft.com/ja-jp/dotnet/api/system.runtime.interopservices.comtypes.filetime?view=net-5.0

FILETIME STRUCTURE

Represents the number of 100-nanosecond intervals since January 1, 1601. This structure has a 64-bit value.

For example, you can get Epoch seconds by converting to the following sites:
(By the way, it seems that similar processing is being carried out in anyzeMFT.)

https://www.frenk.com/2009/12/convert-filetime-to-unix-timestamp/

Between Jan 1,1601 and Jan 1,1970 there are 11644473600 seconds, so we will just subtract that value

>>diff=filetime-(11644473600000*10000)
>>epoch=diff/10000000
>> print (epoch)
1555837726.8921297

https://www.epochconverter.com/ confirms the following:

GMT: April 21, 2019 Sunday 09:08:46
Your time zone: Sunday, April 21, 2019 18:08:46 GMT+09:00

Screenshot in Explorer

Enter a description of the image here


2022-09-30 21:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.