Jump to content

Recommended Posts

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

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

 

"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

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

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

"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

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

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

 

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
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.

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
Posted
  On 4/10/2017 at 9:00 AM, 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
Expand  
 

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

 

  • Moderators
Posted

trof,

  Quote

Autoit even adds a backslash to the folder name

Expand  

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:

  Reveal hidden contents

 

Posted
  On 4/10/2017 at 11:10 AM, 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

Expand  
 

Thanks for the explanation

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
  • Recently Browsing   0 members

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