Jump to content

Sort order: folders on top


Recommended Posts


I use this script (thanks to j0kky) to write to a file the files and the folders present in a directory.

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <Array.au3>
Opt("ExpandEnvStrings", 1)
#include <File.au3>
$aFileList = _FileListToArray(@WorkingDir, "*")
For $i = 1 To UBound($aFileList) - 1
    Local $sFilePath = $aFileList[$i]
    If Not FileWrite("elenco.txt", $sFilePath & @CRLF) Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
    EndIf
Next


The output is sorted alphabetically regardless the items are files or folders. I'd like instead to have folders on top

Link to comment
Share on other sites

If you want folders and files ordered with folders first The you need to list them separately then append the 3 lists 

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <Array.au3>
Opt("ExpandEnvStrings", 1)
#include <File.au3>
Global $aFolderList = _FileListToArray(@WorkingDir, "*",$FLTA_FOLDERS)
Global $aFileList = _FileListToArray(@WorkingDir, "*",$FLTA_FILES)

_ArraySort($aFolderList)
_ArraySort($aFileList)


For $i = 1 To UBound($aFolderList) - 1
    Local $sFilePath = $aFolderList[$i]
    If Not FileWrite("elenco.txt", $sFilePath & @CRLF) Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
    EndIf
Next
For $i = 1 To UBound($aFileList) - 1
    Local $sFilePath = $aFileList[$i]
    If Not FileWrite("elenco.txt", $sFilePath & @CRLF) Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
    EndIf
Next

 

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

Thanks, it works, but I have problems with 

_ArraySort

Sometimes it adds a random number, others it replaces the name of a file with a random number.

For instance, it replace "[VirtualDub] Convertire una file AVi in Div-x - VideoMakers.net (2013-01-25 21-13-11).htm" with 113

and "_FileListToArrayEx - AutoIt Example Scripts - AutoIt Forums.mhtml" with 72

Link to comment
Share on other sites

Hi sorry about that I've edited my script to add the extra parameters required to exclude the first row of the arrays which contains the number of Folders/Files found.

_ArraySort($aFolderList,0,1,0)
_ArraySort($aFileList,0,1,0)

 

Edited by Bowmore

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

Wow, thanks.

One last question.

I tried this 

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <Array.au3>
Opt("ExpandEnvStrings", 1)
#include <File.au3>
#include <_FileListToArrayEx.au3>
Global $aFolderList = _FileListToArrayEx(@WorkingDir, "*", 1 + 8 + 4)
Global $aFileList = _FileListToArrayEx(@WorkingDir, "*", 2 + 8 + 4)

_ArraySort($aFolderList,0,1,0)
_ArraySort($aFileList,0,1,0)


For $i = 1 To UBound($aFolderList) - 1
    Local $sFilePath = $aFolderList[$i]
    If Not FileWrite("elenco e sottocartelle.txt", $sFilePath & @CRLF) Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
    EndIf
Next
For $i = 1 To UBound($aFileList) - 1
    Local $sFilePath = $aFileList[$i]
    If Not FileWrite("elenco e sottocartelle.txt", $sFilePath & @CRLF) Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
    EndIf
Next

to write even files included in subfolders.

Again, I'd like to have folders with files that are inside them on top and then the files of the root folder. I hope it's clear

Link to comment
Share on other sites

how about this : 

#include <Array.au3>
#include <File.au3>

Global $aList, $collect

$aList = _FileListToArrayRec(@WorkingDir & "\", "*", 0, 1, 0, 2)

_sort()
_ArrayDisplay($aList)


Func _sort()
    For $i = UBound($aList) - 1 To 1 Step -1
        If StringInStr(FileGetAttrib($aList[$i]), "D") Then
            $collect = $aList[$i]
            $aList[$i] = ""
            Do
                $i -= 1
                If StringInStr(FileGetAttrib($aList[$i]), "D") Then
                    $collect &= "," & $aList[$i]
                    $aList[$i] = ""
                EndIf
            Until $i = 0
        EndIf
    Next
    For $k = UBound($aList) - 1 To 1 Step -1
        If $aList[$k] = "" Then
            Local $i = $k
            Do
                $k -= 1
                If $aList[$k] <> "" Then
                    $aList[$i] = $aList[$k]
                    $aList[$k] = ""
                    $i -= 1
                EndIf
            Until $k = 0
        EndIf
    Next
    If $collect <> "" Then
        Local $split = StringSplit($collect, ",")
        For $a = UBound($split) - 1 To 1 Step -1
            $aList[$a] = $split[UBound($split) - $a]
        Next
        $collect = ""
    EndIf
EndFunc   ;==>_sort

 

Link to comment
Share on other sites

Thanks, but it's not perfect.

This is the result on a test folder

xIGZZKI.jpg

 

I need
F:\Documenti\Download\TakeOwnAddPerm\Logs\
F:\Documenti\Download\TakeOwnAddPerm\Logs\Nuovo Script di comandi Windows.cmd
F:\Documenti\Download\TakeOwnAddPerm\Nuova cartella\
F:\Documenti\Download\TakeOwnAddPerm\Nuova cartella\Nuovo documento di testo.txt
F:\Documenti\Download\TakeOwnAddPerm\Nuovo AutoIt v3 Script.au3
F:\Documenti\Download\TakeOwnAddPerm\TakeOwnAddpermv1.ps1
F:\Documenti\Download\TakeOwnAddPerm\Takeownperm.bat

 

Also, I need output to a text file and not to a window

Edited by trof
Link to comment
Share on other sites

Isn't this just normal sort for example:

#include <Array.au3>
#include <File.au3>

Local $aFileList = _FileListToArrayRec(@WorkingDir & "\", "*", 0, 1, 1, 2)
_ArrayInsert($aFileList, 1, @WorkingDir & "\")
Local $hFileList = FileOpen(@ScriptDir & '\FileList.txt', 2)
_FileWriteFromArray($hFileList, $aFileList, 1)
FileClose($hFileList)
$aFileList[0] = UBound($aFileList) - 1
_ArrayDisplay($aFileList)

 

Link to comment
Share on other sites

Thanks, I  would have preferred folders on top instead they are at the bottom, but you can't always get what you want.

 

When I don't need to recurse subfolders I use this

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <Array.au3>
Opt("ExpandEnvStrings", 1)
#include <File.au3>
Global $aFolderList = _FileListToArray(@WorkingDir, "*",$FLTA_FOLDERS)
Global $aFileList = _FileListToArray(@WorkingDir, "*",$FLTA_FILES)

_ArraySort($aFolderList,0,1,0)
_ArraySort($aFileList,0,1,0)


For $i = 1 To UBound($aFolderList) - 1
    Local $sFilePath = $aFolderList[$i]
    If Not FileWrite("elenco.txt", $sFilePath & @CRLF) Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
    EndIf
Next
For $i = 1 To UBound($aFileList) - 1
    Local $sFilePath = $aFileList[$i]
    If Not FileWrite("elenco.txt", $sFilePath & @CRLF) Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
    EndIf
Next

 The only drawback is that it shows even protected files system, and I don't want this.

Link to comment
Share on other sites

My OP should display as follows, that's how it displays on my system:

F:\Documenti\Download\TakeOwnAddPerm\Logs\
F:\Documenti\Download\TakeOwnAddPerm\Logs\Nuovo Script di comandi Windows.cmd
F:\Documenti\Download\TakeOwnAddPerm\Nuova cartella\
F:\Documenti\Download\TakeOwnAddPerm\Nuova cartella\Nuovo documento di testo.txt
F:\Documenti\Download\TakeOwnAddPerm\Nuovo AutoIt v3 Script.au3
F:\Documenti\Download\TakeOwnAddPerm\TakeOwnAddpermv1.ps1
F:\Documenti\Download\TakeOwnAddPerm\Takeownperm.ba

In your post above you can use _FileListToArrayRec($sFilePath, "*", 12, 0, 1, 2) this will

  • Exclude Hidden and System Files and Folders - Parameter 3
  • List the Working Directory without recursive - Parameter 5
  • Return Full Path - Parameter 6
Link to comment
Share on other sites

1 hour ago, Subz said:

My OP should display as follows, that's how it displays on my system:

F:\Documenti\Download\TakeOwnAddPerm\Logs\
F:\Documenti\Download\TakeOwnAddPerm\Logs\Nuovo Script di comandi Windows.cmd
F:\Documenti\Download\TakeOwnAddPerm\Nuova cartella\
F:\Documenti\Download\TakeOwnAddPerm\Nuova cartella\Nuovo documento di testo.txt
F:\Documenti\Download\TakeOwnAddPerm\Nuovo AutoIt v3 Script.au3
F:\Documenti\Download\TakeOwnAddPerm\TakeOwnAddpermv1.ps1
F:\Documenti\Download\TakeOwnAddPerm\Takeownperm.ba

In your post above you can use _FileListToArrayRec($sFilePath, "*", 12, 0, 1, 2) this will

  • Exclude Hidden and System Files and Folders - Parameter 3
  • List the Working Directory without recursive - Parameter 5
  • Return Full Path - Parameter 6
 

This is what I have

F:\Documenti\Download\TakeOwnAddPerm\
F:\Documenti\Download\TakeOwnAddPerm\Nuovo AutoIt v3 Script.au3
F:\Documenti\Download\TakeOwnAddPerm\TakeOwnAddpermv1.ps1
F:\Documenti\Download\TakeOwnAddPerm\Takeownperm.bat
F:\Documenti\Download\TakeOwnAddPerm\Logs\
F:\Documenti\Download\TakeOwnAddPerm\Logs\Nuovo Script di comandi Windows.cmd
F:\Documenti\Download\TakeOwnAddPerm\Nuova cartella\
F:\Documenti\Download\TakeOwnAddPerm\Nuova cartella\Nuovo documento di testo.txt

 

 

When I don't recurse subfolders I don't like to have the full path, so I modded your line  _FileListToArrayRec($sFilePath, "*", 12, 0, 1, 2)

to  _FileListToArrayRec(@WorkingDir, "*", 12, 0, 1, 0)   but I don't know why Autoit always loses the sort order and when I apply the script in some specific folders Autoit even adds a backslash to the folder name (it seems Autoit remember a previously used script). Here an example

5.au3
agata nottwil.txt
Agnitum Ltd  Online Purchase.mht
Alice dns.txt
Alice\
Asus K8VMX\
auphonic-denoise-3-unprocessed.wav
Avid\

The script I used is

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <Array.au3>
Opt("ExpandEnvStrings", 1)
#include <File.au3>
Global $aFolderList = _FileListToArrayRec(@WorkingDir, "*", 12, 0, 1, 0)


For $i = 1 To UBound($aFolderList) - 1
    Local $sFilePath = $aFolderList[$i]
    If Not FileWrite("elenco.txt", $sFilePath & @CRLF) Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
    EndIf
Next

 

Link to comment
Share on other sites

  • Moderators

trof,

Quote

Autoit even adds a backslash to the folder name

That is by design as explained in the Help file:

$sFilePath    Initial path used to generate filelist.
              If path ends in \ then folders will be returned with an ending \

The idea is to differentiate between folders and files when getting both returned - otherwise how are you to know?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

19 minutes ago, Melba23 said:

trof,

That is by design as explained in the Help file:

$sFilePath    Initial path used to generate filelist.
              If path ends in \ then folders will be returned with an ending \

The idea is to differentiate between folders and files when getting both returned - otherwise how are you to know?

M23

 

Thanks for the explanation

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...