Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically sort folders on Windows 8

Windows 8 does not seem to persist sort orders on individual folders. I'm guessing when explorer.exe terminates (like when rebooting) these preferences get discarded? Beats me, all I know is after a time (like a few days) those preferences get totally lost.

Is there a way to duplicate programmatically sorting folders in Windows Explorer (opting one of the 'Sort by' options in the context menu)?

Such that, say I have a folder opened in the Windows UI. Say the folder is currently set to sort by 'Name' and after running the program which will sort it by size I check the sort order in Windows Explorer again and it now says 'Size'?

I have a certain big folder structure that I want all of it and its subfolders sorted by size permanently. So I would like to run this program before viewing them and not have to individually manually re-sort them all first. Thanks!

like image 334
Wes Avatar asked Jan 08 '23 00:01

Wes


2 Answers

The sort order belongs to the view, not folder. You can have two Windows Explorer windows open on the same folder, each has a different sort order.

To force a sort order when Windows Explorer navigates to a specific folder, first hook the DShellWindowsEvents::WindowRegistered event on the shellwindows object, then each time when the WindowRegistered event triggers, enumerate through the shell window list and compare with your existing enumeration to find the new windows explorer instance. Once you get hand on the new instance, hook up the DWebBrowserEvents2::DocumentComplete event for that instance to listen to its navigations.

When a navigation is complete, the DocumentComplete event will give you the target URL which you can use to detect if the target is inside your big folder structure. If so, query the IShellBrowser service from the windows explorer instance, then call QueryActiveShellView to get the shell view. Once you get the shell view, QI for IFolderView2 and then call SetSortColumns.

The Automate the Active Windows Explorer or Internet Explorer Window sample on codeproject has most of the code, except that it calls IShellView::SelectItem to simulate SHOpenFolderAndSelectItems at the end.

like image 171
Sheng Jiang 蒋晟 Avatar answered Jan 19 '23 03:01

Sheng Jiang 蒋晟


You can save Explorer sort settings by holding CTRL while clicking on that X on top right.

http://blog.chron.com/helpline/2009/01/saving-explorer-sort-order/

like image 43
Matt Avatar answered Jan 19 '23 02:01

Matt