trof Posted April 9, 2017 Share Posted April 9, 2017 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 More sharing options...
Bowmore Posted April 9, 2017 Share Posted April 9, 2017 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 trof 1 "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 More sharing options...
trof Posted April 9, 2017 Author Share Posted April 9, 2017 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 More sharing options...
Bowmore Posted April 9, 2017 Share Posted April 9, 2017 (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 April 9, 2017 by Bowmore trof 1 "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 More sharing options...
trof Posted April 9, 2017 Author Share Posted April 9, 2017 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 More sharing options...
Deye Posted April 9, 2017 Share Posted April 9, 2017 how about this : expandcollapse popup#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 More sharing options...
trof Posted April 9, 2017 Author Share Posted April 9, 2017 (edited) Thanks, but it's not perfect. This is the result on a test folder I needF:\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 April 9, 2017 by trof Link to comment Share on other sites More sharing options...
Subz Posted April 10, 2017 Share Posted April 10, 2017 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) trof 1 Link to comment Share on other sites More sharing options...
trof Posted April 10, 2017 Author Share Posted April 10, 2017 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 More sharing options...
Subz Posted April 10, 2017 Share Posted April 10, 2017 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 More sharing options...
Subz Posted April 10, 2017 Share Posted April 10, 2017 Also you could use CMD for example: #include <AutoItConstants.au3> Local $iFileList = Run(@ComSpec & " /c DIR " & @WorkingDir & "\* /A-S /B /OG", @SystemDir, @SW_HIDE, $STDOUT_CHILD) Local $sFileList = "" While 1 $sFileList &= StdoutRead($iFileList) If @error Then ExitLoop WEnd FileWrite("FileList(Recursive).txt", $sFileList) Link to comment Share on other sites More sharing options...
trof Posted April 10, 2017 Author Share Posted April 10, 2017 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.au3agata 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 More sharing options...
trof Posted April 10, 2017 Author Share Posted April 10, 2017 I don't want to use cmd because I can't use accented or Chinese characters Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 10, 2017 Moderators Share Posted April 10, 2017 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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
trof Posted April 10, 2017 Author Share Posted April 10, 2017 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now