I would like to code a function to which you can pass a file path, for example:
C:\FOLDER\SUBFOLDER\FILE.TXT
and it would open Windows Explorer with the folder containing the file and then select this file inside the folder. (Similar to the “Show In Folder” concept used in many programs.)
How can I do this?
Easiest way without using Win32 shell functions is to simply launch explorer.exe with the
/selectparameter. For example, launching the processexplorer.exe /select,"C:\Folder\subfolder\file.txt"will open a new explorer window to C:\Folder\subfolder with file.txt selected.
If you wish to do it programmatically without launching a new process, you’ll need to use the shell function
SHOpenFolderAndSelectItems, which is what the/selectcommand to explorer.exe will use internally. Note that this requires the use of PIDLs, and can be a real PITA if you are not familiar with how the shell APIs work.Here’s a complete, programmatic implementation of the
/selectapproach, with path cleanup thanks to suggestions from @Bhushan and @tehDorf:Reference: Explorer.exe Command-line switches