OK I have another small issue. I name a lot of my "important" files and folders by prepending the name with an underscore (eg: _MyFolder, _myfile.ahk) so they "sort" (in usual Explorer-style) to the top. My script (with no defined sort command) puts underscored items at the bottom. I thought this a bit strange given that the default Sort, VarName examples in the help file sort with underscores at the top eg:[The following _example sorts the contents of a file] would sort correctly with _example at the top). So I'm thinking I need to add some kind of sort command to my script and force it to sort the way I want but I have no idea where to place it. This is my script:
Code: Select all
;Opens a file explorer containing files in a_scriptdir
;Using just me's code:
;http://ahkscript.org/boards/viewtopic.php?f=5&t=2777
#SingleInstance force
; Set the root folder for the TreeView. Loading may take a long time if an entire drive such as C:\ is specified:
TreeRoot = %A_ScriptDir%
TreeViewWidth := 280
ListViewWidth := A_ScreenWidth - TreeViewWidth - 450
Gui +Resize
; Create an ImageList and put some standard system icons into it:
ImageListID := IL_Create(5)
Loop 5
IL_Add(ImageListID, "shell32.dll", A_Index)
; Create a TreeView and a ListView side-by-side :
Gui, Add, TreeView, vMyTreeView r20 w%TreeViewWidth% gMyTreeView ImageList%ImageListID%
Gui, Add, ListView, vMyListView r20 w%ListViewWidth% x+10 gMyListView AltSubmit, Name|Modified
; Set the ListView's column widths:
Col2Width = 70 ; Narrow to reveal only the YYYYMMDD part.
LV_ModifyCol(1, ListViewWidth - Col2Width - 30) ; Allows room for vertical scrollbar.
LV_ModifyCol(2, Col2Width)
; Create a Status Bar:
Gui, Add, StatusBar
SB_SetParts(60, 85) ; Create three parts in the bar (the third part fills the remaining width).
; Add folders and their subfolders to the tree. Display the status in case loading takes a long time:
SplashTextOn, 200, 25, Please wait, Loading the tree...
TreeRoot := RTrim(TreeRoot, "\") ; Remove trailing backslashes, if any.
SplitPath, TreeRoot, TreeRootName, TreeRootDir ; Split TreeRoot into TreeRootName and TreeRootDir.
If (TreeRootName) ; If TreeRootName is not empty
TreeRootID := TV_Add(TreeRootName, 0, "Expand Icon4") ; add it to the TreeView
Else ; Else
TreeRootID := 0 ; set TreeRootId to zero.
AddSubFoldersToTree(TreeRoot, TreeRootID) ; Call AddSubFolders() passing TreeRoot and TreeRootID
TreeRoot := TreeRootDir ; Set TreeRoot to TreeRootDir.
SplashTextOff
Gui, Show, h550, %TreeRoot%
return
; This function adds to the TreeView all subfolders in the specified folder.
AddSubFoldersToTree(Folder, ParentItemID = 0)
{
; It also calls itself recursively to gather nested folders to any depth.
Loop %Folder%\*.*, 2 ; Retrieve all of Folder's sub-folders.
AddSubFoldersToTree(A_LoopFileFullPath, TV_Add(A_LoopFileName, ParentItemID, "Icon4"))
}
;--------------------------------------------------------------------------------------------------
; What to do when a folder is clicked in treeview
MyTreeView:
if A_GuiEvent <> S ; if event other than "select new tree item".
return ; Do nothing.
; Otherwise, populate the ListView with the contents of the selected folder.
; First determine the full path of the selected folder:
TV_GetText(SelectedItemText, A_EventInfo)
ParentID := A_EventInfo
Loop ; Build the full path to the selected folder.
{
ParentID := TV_GetParent(ParentID)
if not ParentID ; No more ancestors.
break
TV_GetText(ParentText, ParentID)
SelectedItemText = %ParentText%\%SelectedItemText%
}
SelectedFullPath = %TreeRoot%\%SelectedItemText%
;--------------------------------------------------------------------------------------------------
; Put the files into the ListView:
LV_Delete() ; Clear all rows.
GuiControl, -Redraw, MyListView ; Improve performance by disabling redrawing during load.
FileCount = 0
TotalSize = 0
Loop %SelectedFullPath%\*.* ; For simplicity, this omits folders so that only files are shown in the ListView.
{
;LV_Add("", A_LoopFileName, A_LoopFileTimeModified)
LV_Add("", A_LoopFileName, A_LoopFileSize . " KB")
FileCount += 1
TotalSize += A_LoopFileSize
}
GuiControl, +Redraw, MyListView
; Populate status bar to show info about the currently selected folder:
SB_SetText(FileCount . " files", 1)
SB_SetText(Round(TotalSize / 1024, 1) . " KB", 2)
SB_SetText(SelectedFullPath, 3)
return
;--------------------------------------------------------------------------------------------------
; What to do when a file is double/right-clicked in listview
MyListView:
if (A_GuiEvent = "DoubleClick")
{
LV_GetText(RowText, A_EventInfo) ; Get the text from the row's first field and store it in the var RowText.
run %SelectedFullPath%\%RowText%
;exitapp ;exit the script after selection is made
}
else if (A_GuiEvent = "RightClick")
{
LV_GetText(RowText, A_EventInfo) ; Get the text from the row's first field and store it in the var RowText.
run C:\PortableApps\Notepad++\notepad++.exe "%SelectedFullPath%\%RowText%"
;exitapp ;exit the script after selection is made
}
return
;--------------------------------------------------------------------------------------------------
GuiSize: ; Expand/shrink the ListView and TreeView in response to user's resizing of window.
if A_EventInfo = 1 ; The window has been minimized. No action needed.
return
; Otherwise, the window has been resized or maximized. Resize the controls to match.
GuiControl, Move, MyTreeView, % "H" . (A_GuiHeight - 30) ; -30 for StatusBar and margins.
GuiControl, Move, MyListView, % "H" . (A_GuiHeight - 30) . " W" . (A_GuiWidth - TreeViewWidth - 30)
return
GuiClose: ; Exit the script when the user closes the TreeView's GUI window.
ExitApp
Cheers
EDIT: Just discovered the Sort, VarName still doesn't sort underscores to the top if there is a string that starts with a number. The numbers are sorted first then the underscore then alphabetical characters after that.