Python Tool Like ExifTool: Show Details in Windows Explorer Headers using Python
Have you ever wished you could view detailed information about files, similar to the functionality provided by ExifTool, but directly in Windows Explorer? This article covers how to create a Python tool that adds custom columns to Windows Explorer, displaying detailed information about files, such as creation date, last modified date, and file size. We will also discuss the limitations of this approach and potential solutions for those limitations.
Introduction
ExifTool is a powerful command-line tool for reading and writing metadata of various file formats. However, it can sometimes be inconvenient to use the command line for obtaining simple details about files. This article will show you how to create a Python script that adds custom columns to Windows Explorer, displaying a wealth of information about files without the need for external tools.
Designing the Script
The script will make use of the pywin32 library so make sure to install it using pip install pywin32.
Adding Custom Columns
To add custom columns to Windows Explorer headers, you will need to make use of the SHColumnOrderFuncEx callback function to populate the header data. To start, create a new Python file:
import pythoncom, pywintypes
from win32com.shell import shell, shellcon
Now you can define the SHColumnOrderFuncEx callback function:
def ColumnHandler(hwnd, lpcustomColumn, lpdwNumSortColumns, psort):
# Populate custom columns here
Registering the Callback Function
The next step is to register the callback function. This will enable the custom columns created by the callback function to be shown in Windows Explorer:
def RegisterWindowEvent(window_class, func_pointer):
clsf = pythoncom.CoClass("SHELLFOLDER", pythoncom.IID_IShellFolderViewCB)
viewer = clsf.CreateInstance()
viewer.SetColumnsProc(func_pointer)
viewer.SetColumnsToFetch([lpcustomColumn.fmtid, lpcustomColumn.pid])
pidl = shell.SHParseDisplayName(window_class)
view = shell.SHGetView(pidl)
view.AddRawUICommand(viewer)
In the RegisterWindowEvent function, the window_class argument represents the type of window that you want to register the custom columns for. You can use any of the following window types:
' directory'- Registers custom columns for directories'desktop'- Registers custom columns for the desktop'Drive'- Registers custom columns for drives in My Computer
Now you can call RegisterWindowEvent function in the script and test if custom columns are displayed:
if __name__ == '__main__':
RegisterWindowEvent('directory', ColumnHandler)
Custom Column Implementation
You can now implement the custom columns by populating information inside the ColumnHandler callback function:
import os
def ColumnHandler(hwnd, lpcustomColumn, lpdwNumSortColumns, psort):
column_data = bytearray(256)
for i in range(lpdwNumSortColumns):
column_id = lpcustomColumn[i].pid
if column_id == 0x10: # File creation time
file_creation_time = os.path.getctime(lpcustomColumn[i].pszPath)
filetime_to_localtime = pythoncom.CoCreateInstance(pythoncom.PyIID_FILETIME,
```