Jump to content
Sign in to follow this  

Sorting File List (shell.application)

Recommended Posts


The script should be self explanatory.

I'm wanting to sort a windows explorer (shell.application) file list (listview ctrl) items. The below script does sort the items, but a weird thing happens. Weird thing: the icons don't sort with items and when you click on what you the item says it is, it turns out to be what it originally was. Test it. Any ideas?

I've spent a lot of time on MSDN and they don't seem to have a sort function exposed, although I know they are doing it some how (duh!).

I did have an idea for sorting by clicking the particular column header, but i don't know how to do that yet.

#include <GuiConstants.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#Include <GuiHeader.au3>
#include <Array.au3>

Sleep(4000);  Sleep is there to wait for you to get to whatever window you want to test this on
;NOTE: you must have details view enabled in that window.  
;         I could have made the script do this all for you but it would make the script longer

Local $WindowHwnd = HWnd(WinGetHandle(''))
Local $FileListView_HWnd=ControlGetHandle($WindowHwnd,'','[Class:SysListView32;Instance:1]')
Local $HeaderControlHandle = ControlGetHandle($WindowHwnd ,'','SysHeader321');[class:SysHeader32;instance:1]
Local $colCount = _GUICtrlListView_GetColumnCount($FileListView_HWnd)

ConsoleWrite('  Col Count: '&$colCount &' for '&$HeaderControlHandle&@CRLF)

Local $columnToSort = 0
Local $columnSortType = 0
Local $columnSortDirection = 0 
For $_c = 0 To $colCount-1
    Local $_colflag = _GUICtrlHeader_GetItemFlags($HeaderControlHandle, $_c)
    If $_colflag <> 0 Then
        $columnToSort = $_c
        $columnFlag = $_colflag
        If $columnFlag = 8 Then;up arrow (default = up/descending)
            ConsoleWrite('   Col '& $_c &' - Item Flag: '&$_colflag &@CRLF)
            ConsoleWrite('  Col Sort Type: '&'descending' &@CRLF)
            $columnSortDirection = 1;Descending
        ElseIf $columnFlag = 4 Then;down arrow
            ConsoleWrite('   Col '& $_c &' - Item Flag: '&$_colflag &@CRLF)
            ConsoleWrite('  Col Sort Type: '& 'ascending' &@CRLF)
            $columnSortDirection = 0;Ascending

If $columnSortDirection = 0 Then
    $columnSortDirection = 1
    $columnFlag = 8
ElseIf $columnSortDirection = 1 Then
    $columnSortDirection = 0
    $columnFlag = 4
_GUICtrlHeader_SetItemFlags($HeaderControlHandle, $columnToSort, $columnFlag)

Global $B_DESCENDING[_GUICtrlListView_GetColumnCount ($FileListView_HWnd) ]
If $columnSortDirection = 0 Then
    $B_DESCENDING[$columnToSort] = 'False'
ElseIf $columnSortDirection = 1 Then
    $B_DESCENDING[$columnToSort] = 'True'
_GUICtrlListView_SimpleSort($FileListView_HWnd, $B_DESCENDING, $columnToSort)

For $_c = 0 to UBound($B_DESCENDING)-1
    ConsoleWrite("-->Col:" & @TAB & $B_DESCENDING[$_c] & @CRLF)
For $_c = 0 to $colCount-1
    If $columnToSort <> $_c Then
        _GUICtrlHeader_SetItemFormat ($HeaderControlHandle, $_c, BitOR($HDF_LEFT, $HDF_STRING));THANKS GARYFROST

A decision is a powerful thing

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this