Jump to content

Recommended Posts

Posted (edited)

Hi All, Sorry for my English

#include-once

; #UDF# =======================================================================================================================
; Title .........: File List To Array
; AutoIt Version : 3.3.8.1
; Language ......: English
; Description ...: Lists files and\or folders in a specified path (Similar to using Dir with the /B Switch)
; Author(s) .....: DXRW4E
; Notes .........:
; ===============================================================================================================================

; #CURRENT# =====================================================================================================================
;~ _FileListToArrayEx
; ===============================================================================================================================

; #FUNCTION# =======================================================================================================================================================
; Name...........: _FileListToArrayEx
; Description ...: Lists files and\or folders in a specified path (Similar to using Dir with the /B Switch)
; Syntax.........: _FileListToArrayEx($sPath[, $sFilter = "*"[, $iFlag = 0]])
; Parameters ....: $sPath   - Path to generate filelist for.
;                 $sFilter - Optional the filter to use, default is *. (Multiple filter groups such as "All "*.png|*.jpg|*.bmp") Search the Autoit3 helpfile for the word "WildCards" For details.
;                 $iFlag   - Optional: specifies whether to return files folders or both Or Full Path (add the flags together for multiple operations):
;                 |$iFlag = 0 (Default) Return both files and folders
;                 |$iFlag = 1 Return files only
;                 |$iFlag = 2 Return Folders only
;                 |$iFlag = 4 Search SubDirectory
;                 |$iFlag = 8 Return Full Path
;                 |$iFlag = 16 $sFilter do Case-Sensitive matching (By Default $sFilter do Case-Insensitive matching)
;                 |$iFlag = 32 Disable the return the count in the first element - effectively makes the array 0-based (must use UBound() to get the size in this case).
;                   By Default the first element ($array[0]) contains the number of file found, the remaining elements ($array[1], $array[2], etc.)
;                 |$iFlag = 64 $sFilter is REGEXP Mod, See Pattern Parameters in StringRegExp (Can not be combined with flag 16)
;                 |$iFlag = 128 Return Backslash at the beginning of the file name, example Return "\Filename1.xxx" (Can not be combined with flag 8)
; Return values .: Failure - @Error
;                 |1 = Path not found or invalid
;                 |2 = Invalid $sFilter
;                 |3 = No File(s) Found
; Author ........: DXRW4E
; Modified.......:
; Remarks .......: The array returned is one-dimensional and is made up as follows:
;                               $array[0] = Number of Files\Folders returned
;                               $array[1] = 1st File\Folder
;                               $array[2] = 2nd File\Folder
;                               $array[3] = 3rd File\Folder
;                               $array[n] = nth File\Folder
; Related .......:
; Link ..........:
; Example .......: Yes
; Note ..........: Special Thanks to SolidSnake & Tlem
; ==================================================================================================================================================================
Func _FileListToArrayEx($sPath, $sFilter = "*", $iFlag = 0)
    $sPath = StringRegExpReplace($sPath & "\", "(?!\A)[\\/]+\h*", "\\")
    If Not FileExists($sPath) Then Return SetError(1, 1, "")
    If StringRegExp($sFilter, StringReplace('^\s*$|\v|[\\/:><"]|^\||\|\||\|$', "[" & Chr(BitAND($iFlag, 64) + 28) & '\/:><"]|^\||\|\||\|$', "\\\\")) Then Return SetError(2, 2, "")
    Local $hSearch, $sFile, $sFileList, $sSubDir = BitAND($iFlag, 4), $sDelim = "|", $sDirFilter = StringReplace($sFilter, "*", "")
    $hSearch = FileFindFirstFile($sPath & "*")
    If @Error Then Return SetError(3, 3, "")
    Local $hWSearch = $hSearch, $hWSTMP, $SearchWD, $Extended, $iFlags = StringReplace(BitAND($iFlag, 1) + BitAND($iFlag, 2), "3", "0")
    If BitAND($iFlag, 8) Then $sDelim &= $sPath
    If BitAND($iFlag, 128) Then $sDelim = "|\"
    If Not BitAND($iFlag, 64) Then $sFilter = StringRegExpReplace(BitAND($iFlag, 16) & "(?i)(", "16\(\?\i\)|\d+", "") & StringRegExpReplace(StringRegExpReplace(StringRegExpReplace(StringRegExpReplace($sFilter, "[^*?|]+", "\\Q$0\\E"), "\\E(?=\||$)", "$0\$"), "(?<=^|\|)\\Q", "^$0"), "\*+", ".*") & ")"
    While 1
        $sFile = FileFindNextFile($hWSearch)
        If @Error Then
            If $hWSearch = $hSearch Then ExitLoop
            FileClose($hWSearch)
            $hWSearch -= 1
            $SearchWD = StringLeft($SearchWD, StringInStr($SearchWD, "\", 1, -2))
        ElseIf $sSubDir Then
            $Extended = @Extended
            If ($iFlags + $Extended <> 2) Then
                If $sDirFilter Then
                    If StringRegExp($sFile, $sFilter) Then $sFileList &= $sDelim & $SearchWD & $sFile
                Else
                    $sFileList &= $sDelim & $SearchWD & $sFile
                EndIf
            EndIf
            If Not $Extended Then ContinueLoop
            $hWSTMP = FileFindFirstFile($sPath & $SearchWD & $sFile & "\*")
            If $hWSTMP = -1 Then ContinueLoop
            $hWSearch = $hWSTMP
            $SearchWD &= $sFile & "\"
        Else
            If ($iFlags + @Extended = 2) Or StringRegExp($sFile, $sFilter) = 0 Then ContinueLoop
            $sFileList &= $sDelim & $sFile
        EndIf
    WEnd
    FileClose($hSearch)
    If Not $sFileList Then Return SetError(3, 3, "")
    Return StringSplit(StringTrimLeft($sFileList, 1), "|", StringReplace(BitAND($iFlag, 32), "32", 2))
EndFunc    ;==>_FileListToArrayEx

Registry Key List To Array - Read Registry KeySybKey

_FileListToArrayEx.au3

Edited by DXRW4E

apps-odrive.pngdrive_app_badge.png box-logo.png new_logo.png MEGA_Logo.png

  • 3 months later...
Posted

Wow, very nice! Thanks for sharing!

Ian

My projects:

  • IP Scanner - Multi-threaded ping tool to scan your available networks for used and available IP addresses, shows ping times, resolves IPs in to host names, and allows individual IPs to be pinged.
  • INFSniff - Great technicians tool - a tool which scans DriverPacks archives for INF files and parses out the HWIDs to a database file, and rapidly scans the local machine's HWIDs, searches the database for matches, and installs them.
  • PPK3 (Persistent Process Killer V3) - Another for the techs - suppress running processes that you need to keep away, helpful when fighting spyware/viruses.
  • Sync Tool - Folder sync tool with lots of real time information and several checking methods.
  • USMT Front End - Front End for Microsoft's User State Migration Tool, including all files needed for USMT 3.01 and 4.01, 32 bit and 64 bit versions.
  • Audit Tool - Computer audit tool to gather vital hardware, Windows, and Office information for IT managers and field techs. Capabilities include creating a customized site agent.
  • CSV Viewer - Displays CSV files with automatic column sizing and font selection. Lines can also be copied to the clipboard for data extraction.
  • MyDirStat - Lists number and size of files on a drive or specified path, allows for deletion within the app.
  • 2048 Game - My version of 2048, fun tile game.
  • Juice Lab - Ecigarette liquid making calculator.
  • Data Protector - Secure notes to save sensitive information.
  • VHD Footer - Add a footer to a forensic hard drive image to allow it to be mounted or used as a virtual machine hard drive.
  • Find in File - Searches files containing a specified phrase.
  • 1 month later...
  • 2 weeks later...
  • 2 weeks later...
Posted (edited)

Perhaps a Developers, Moderator, MVP or other, perhaps need to review this (see if need anything else to repair improve something or other), and perhaps use this to defualt (and the same code, the one defined by default, only it was updated), as it seems that the code more clean (39 line) and I think works faster, so maybe it also closes the history of _FileListToArray + Search subdirectory

sorry for my English

Ciao a tutti.

Edited by DXRW4E

apps-odrive.pngdrive_app_badge.png box-logo.png new_logo.png MEGA_Logo.png

Posted (edited)

@JohnOne

The SmOke_N FileListToArrayEx function presents two problems with regard to that of the DXRW4E :

1st : She's more slowly (we talk about some ms, so it's not really important)

2nd : The return are not true, because if I search for this _FileListToArrayEx2(@WindowsDir, "*.dll", 1), it return me files like this C:WINDOWSInstaller$PatchCache$Managed5C1093C35543A0E32A41B090A305076A4.0.30319system.net.dll_x86

Edited by Tlem

Best Regards.Thierry

  • Moderators
Posted

@JohnOne

The SmOke_N FileListToArrayEx function presents two problems with regard to that of the DXRW4E :

1st : She's more slowly (we talk about some ms, so it's not really important)

2nd : The return are not true, because if I search for this _FileListToArrayEx2(@WindowsDir, "*.dll", 1), it return me files like this C:WINDOWSInstaller$PatchCache$Managed5C1093C35543A0E32A41B090A305076A4.0.30319system.net.dll_x86

Hmm, that's an expected return. That is a mask search, not an extension search.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Posted

as it seems that the code more clean (39 line) and I think works faster, so maybe it also closes the history of _FileListToArray + Search subdirectory

Not trying to pick on you.. but you cant call it 39 lines, or call it clean when you have a line like this in your code:

Local $hWSearch = $hSearch, $hWSTMP = $hSearch, $SearchWD, $FPath = StringRegExpReplace(StringRegExpReplace($sSDir, '[^2]+', ""), "2+", StringRegExpReplace($sPath, '\\', "\\\\")), $sSDirF[3] = [0, StringReplace($sSDirFTMP, "*", ""), "(?i)(" & StringRegExpReplace(StringRegExpReplace(StringRegExpReplace(StringRegExpReplace(StringRegExpReplace(StringRegExpReplace("|" & $sSDirFTMP & "|", '\|\h*\|[\|\h]*', "\|"), '[\^\$\(\)\+\[\]\{\}\,\.\=]', "\\$0"), "\|([^\*])", "\|^$1"), "([^\*])\|", "$1\$\|"), '\*', ".*"), '^\||\|$', "") & ")"]
Posted

Hi Beege, that's not a problem is called only once, and it is a necessary thing, the_FileListToArrayEx uses only 1 While, more clean so I do not think that there is

and sorry for my English

Ciao.

apps-odrive.pngdrive_app_badge.png box-logo.png new_logo.png MEGA_Logo.png

Posted (edited)

Hmm, that's an expected return. That is a mask search, not an extension search.

Sorry, but I can't be agree with you. Make a try with the Windows search. If you search for this mask *.dll the result never return files like system.net.dll_x86.

If I want to search for these files, I use this mask : *.dll*

So for me, it can't be an expected return.

Edited by Tlem

Best Regards.Thierry

  • 3 weeks later...
Posted (edited)

There's a problem in this function. Your $sDir parameter has incorrect information in the header, it states:

; $sSDir - Optional: specifies whether to return files folders or both

; |$sSDir=1 Search subdirectory

; |$sSDir=2 Search subdirectory & Return Full Path

But inside the function itself, it doesn't look at whether $sSDir is 1 or 2, it returns the same results regardless of what you set $sSDir to, unless you set it to 0.

ElseIf $sSDir Then

That line only checks if it's True (not 0) or False (0). This follows the default parameter of the function where you set $sSDir=0.

I did some further testing and found out I was incorrect in my original assumption. The problem actually isn't that 1 and 2 for the $sSDir parameter return the same results, because they don't they return different results I just had made an error in my test script. The problem is that the $sSDir parameter takes a third option that's not mentioned, which is 0, which tells the script to not do a recursive search in the target folder. My apologies for that, but the header information is still missing that point, but definitely not a bug in this UDF.

EDIT:Corrected wrong information in my original post.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

  • 1 month later...
Posted

Hi,

I can't figure out a way to obtain non-recursive & fullpath result, instead of the non-recursive & filename only result (obtained through built-in parameter $sSDir=0).

Could someone help me please ?

Posted (edited)

If you search files (or subdirectory) in a specific directory without the recursive option, why did you need to store the path ???

You know it, so it's easy to add it on your result :

#include "_FileListToArrayEx.au3"
$Path = "C:"
$aArray = _FileListToArrayEx($Path, "*", 1, 0)
For $i = 1 To UBound($aArray) - 1
ConsoleWrite($Path & $aArray[$i] & @CRLF)
Next

EDIT : But I agree with you, DXRW4E would have been able to separate the recursive option and the return of full path. ;)

Edited by Tlem

Best Regards.Thierry

Posted (edited)

I did not want to change the original code, that and the setting and the original code for this reason the left so

However who wants to will be able to modify the code if they suffered, just put it so

$FPath = StringRegExpReplace(StringRegExpReplace($sSDir, '[^2]+', ""), "2+", StringRegExpReplace($sPath, '\\', "\\\\"))

in

$FPath = $sPath

So Default and always full path (no need to use more $sSDir = 2)

Sorry for my english

Edited by DXRW4E

apps-odrive.pngdrive_app_badge.png box-logo.png new_logo.png MEGA_Logo.png

Posted

Thank you both for your feed-back

@TIem: I'm looking for files in a list of directories (specified in an XML file) ; each of these directories has a recursivity search boolean param. I thought about your solution, but it seemed to me more convenient and elegant to change the _FileListToArrayEx behaviour itself.

At the end of the day, I'm able to implement your proposal, because it's so easy.

@DRXW4E: I'm afraid I do not understand where to implement this or these new lines of code (not because of your english!). So, I'm going to adopt TIen proposal, except if you were so kind to indicate more clearly where to put this code excatly in the function, or even, separate recursitvity param from full path param, as stated by TIem.

Bye,

Posted

First post of topics and code of _FileListToArrayEx.au3, Replace the line shown in post above, after _FileListToArrayEx.au3 always return full path

Ciao.

apps-odrive.pngdrive_app_badge.png box-logo.png new_logo.png MEGA_Logo.png

Posted (edited)

@DXRW4E

Your proposition doesn't work for root directory. You must modifie this part too

$sFileList &= $sDelim & $sFile
by
$sFileList &= $sDelim & $FPath & $sFile

I understand why you doesn't want make more modification on your function, but in my opinion, the recursive search and full path return must be a separate option.

To take care about what BrewManNH said about the $sSDir flag 0 and what FVn ask, this is my adaptation of your function :

#include-once
;#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
; #FUNCTION# ====================================================================================================================
; Name...........: _FileListToArrayEx
; Description ...: Lists files andor folders in a specified path (Similar to using Dir with the /B Switch)
; Syntax.........: _FileListToArrayEx($sPath[, $sFilter = "*"[, $iFlag = 0 [, $iSDir = 0 [, $iFPath = 1]]]])
; Parameters ....: $sPath   - Path to generate filelist for.
;                 $sFilter - Optional the filter to use, default is *. (Multiple filter groups such as "*.png|*.jpg|*.bmp") Search the Autoit3 helpfile for the word "WildCards" For details.
;                 $iFlag   - Optional: specifies whether to return files folders or both
;                   |$iFlag=0 (Default) Return both files and folders
;                   |$iFlag=1 Return files only
;                   |$iFlag=2 Return Folders only
;                 $iSDir   - Optional: specifies whether to return files folders or both
;                   |$iSDir=0 Do not Search subdirectory (default)
;                   |$iSDir=1 Search subdirectory
;                 $iFPath  - Optional: specifies whether to return files folders or both
;                   |$iFPath=0 Do not return full path
;                   |$iFPath=1 Return full path (Default)
; Return values .: @Error
;     |1 = Path not found or invalid
;                   |2 = Invalid $sFilter
;                   |3 = Invalid $iFlag
;                   |4 = No File(s) Found
; Author ........: SolidSnake <metalgx91 at="" gmail="" dot="" com="">
; Modified.......:
; Remarks .......: The array returned is one-dimensional and is made up as follows:
;                               $array[0] = Number of FilesFolders returned
;                               $array[1] = 1st FileFolder
;                               $array[2] = 2nd FileFolder
;                               $array[3] = 3rd FileFolder
;                               $array[n] = nth FileFolder
; Related .......:
; Link ..........: http://www.autoitscript.com/forum/topic/131277-filelisttoarrayex
; Example .......: Yes
; Note ..........: Special Thanks to Helge and Layer for help with the $iFlag update speed optimization by code65536, pdaughe
;                 Update By DXRW4E
;                 Update By Tlem
; ===============================================================================================================================
Func _FileListToArrayEx($sPath, $sFilter = "*", $iFlag = 0, $iSDir = 0, $iFPath = 1)
Local $hSearch, $sFile, $sFileList, $sDelim = "|", $sSDirFTMP = $sFilter, $FPath
$sPath = StringRegExpReplace($sPath, "[/]+z", "") & "" ; ensure single trailing backslash
If Not FileExists($sPath) Then Return SetError(1, 1, "")
If StringRegExp($sFilter, "[/:><]|(?s)As*z") Then Return SetError(2, 2, "")
If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 3, "")
$hSearch = FileFindFirstFile($sPath & "*")
If @error Then Return SetError(4, 4, "")
If $iFPath = 1 Then $FPath = $sPath
Local $hWSearch = $hSearch, $hWSTMP = $hSearch, $SearchWD, $sSDirF[3] = [0, StringReplace($sSDirFTMP, "*", ""), "(?i)(" & StringRegExpReplace(StringRegExpReplace(StringRegExpReplace(StringRegExpReplace(StringRegExpReplace(StringRegExpReplace("|" & $sSDirFTMP & "|", '|h*|[|h]*', "|"), '[^$()+[]{},.=]', "$0"), "|([^*])", "|^$1"), "([^*])|", "$1$|"), '*', ".*"), '^|||$', "") & ")"]
While 1
  $sFile = FileFindNextFile($hWSearch)
  If @error Then
   If $hWSearch = $hSearch Then ExitLoop
   FileClose($hWSearch)
   $hWSearch -= 1
   $SearchWD = StringLeft($SearchWD, StringInStr(StringTrimRight($SearchWD, 1), "", 1, -1))
  ElseIf $iSDir Then
   $sSDirF[0] = @extended
   If ($iFlag + $sSDirF[0] <> 2) Then
    If $sSDirF[1] Then
     If StringRegExp($sFile, $sSDirF[2]) Then $sFileList &= $sDelim & $FPath & $SearchWD & $sFile
    Else
     $sFileList &= $sDelim & $FPath & $SearchWD & $sFile
    EndIf
   EndIf
   If Not $sSDirF[0] Then ContinueLoop
   $hWSTMP = FileFindFirstFile($sPath & $SearchWD & $sFile & "*")
   If $hWSTMP = -1 Then ContinueLoop
   $hWSearch = $hWSTMP
   $SearchWD &= $sFile & ""
  Else
   If ($iFlag + @extended = 2) Or StringRegExp($sFile, $sSDirF[2]) = 0 Then ContinueLoop
   $sFileList &= $sDelim & $FPath & $sFile
  EndIf
WEnd
FileClose($hSearch)
If Not $sFileList Then Return SetError(4, 4, "")
Return StringSplit(StringTrimLeft($sFileList, 1), "|")
EndFunc   ;==>_FileListToArrayEx
Edited by Tlem

Best Regards.Thierry

Posted (edited)

@Tlem

Hi Tlem, Thank You, you're right, or forget that the full path is used only for subdir

to be honest I do not like much the fate of using many flags in a function (I mean best to use as little as possible, BITAND and really nice), I think the better way and what he or posted in first post

However who like that way will be able to use your script (that is already OK) in post above (_FileListToArrayEx($sPath, $sFilter = "*", $iFlag = 0, $iSDir = 0, $iFPath = 1)), so I think everything is OK

Many Thanks Again

@FVN

Now you can use for example

$iFlag = 0 + 8 --> Return both files and folders and Full Path

Or

$iFlag = 0 + 8 + 4 --> Return both files and folders and subdirectory and Full Path

Or

$iFlag = 1 + 8 + 4 --> Return files only and subdirectory and Full Path

Or

$iFlag = 1 + 8 --> Return files only and Full Path

ect ect ect ect

@All

already updated the first post

sorry again for my English

Ciao.

Edited by DXRW4E

apps-odrive.pngdrive_app_badge.png box-logo.png new_logo.png MEGA_Logo.png

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
×
×
  • Create New...