trof

Sort order: folders on top

15 posts in this topic

#1 ·  Posted


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

Share this post


Link to post
Share on other sites



#2 ·  Posted

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

 

1 person likes this

"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

Share this post


Link to post
Share on other sites

#3 ·  Posted

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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
1 person likes this

"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

Share this post


Link to post
Share on other sites

#5 ·  Posted

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

Share this post


Link to post
Share on other sites

#6 ·  Posted

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

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#8 ·  Posted

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)

 

1 person likes this

Share this post


Link to post
Share on other sites

#9 ·  Posted

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.

Share this post


Link to post
Share on other sites

#10 ·  Posted

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

Share this post


Link to post
Share on other sites

#12 ·  Posted

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

 

Share this post


Link to post
Share on other sites

#13 ·  Posted

I don't want to use cmd because I can't use accented or Chinese characters

Share this post


Link to post
Share on other sites

#14 ·  Posted

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#15 ·  Posted

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

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