Page 1 sur 1

[...] _FileListToArray modifié

Posté : ven. 14 mai 2010 14:01
par JohnWayne
Bonjour,

J'ai voulu faire un script qui permet de trouver des fichiers données dans un dossier aléatoire du bureau. Je suis donc tombé sur un sujet du forum autoIT anglais et j'ai trouvé une fonction je pense qui me permetterais de faire ce que je voudrais.

Voici le code :

Code : Tout sélectionner

  ;===============================================================================
;
; Description:      lists all files and folders in a specified path (Similar to using Dir with the /B Switch)
; Syntax:           _FileListToArray($sPath, $sFilter = "*", $iFlag = 0)
; Parameter(s):     $sPath = Path to generate filelist for
;                  $iFlag = determines weather to return file or folders or both
;                   $sFilter = The filter to use. Search the Autoit3 manual for the word "WildCards" For details
;                           $iFlag=0(Default) Return both files and folders
;                           $iFlag=1 Return both files and folders with full path
;                           $iFlag=2 Return Files Only
;                           $iFlag=3 Return Files Only with full path
;                           $iFlag=4 Return Folders Only
;                           $iFlag=5 Return Folders Only with full path
;
; Requirement(s):   None
; Return Value(s):  On Success - Returns an array containing the list of files and folders in the specified path
;                       On Failure - Returns the an empty string "" if no files are found and sets @Error on errors
;                       @Error=1 Path not found or invalid
;                       @Error=2 Invalid $sFilter
;                       @Error=3 Invalid $iFlag
;                       @Error=4 No File(s) Found
;
; Author(s):        SolidSnake <MetalGX91 at GMail dot com> (Modified by mrbond007)
; Note(s):          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
;
;                   Special Thanks to Helge and Layer for help with the $iFlag update
;===============================================================================
Func _FileListToArray($sPath, $sFilter, $iFlag)
    Local $hSearch = "", $sFile = 0, $asFileList = ""
    If Not FileExists($sPath) Then Return SetError(1, 1, "")
    If (StringInStr($sFilter, "\")) Or (StringInStr($sFilter, "/")) Or (StringInStr($sFilter, ":")) Or (StringInStr($sFilter, ">")) Or (StringInStr($sFilter, "<")) Or (StringInStr($sFilter, "|")) Or (StringStripWS($sFilter, 8) = "") Then Return SetError(2, 2, "")
    If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2 Or $iFlag = 3 Or $iFlag = 4 Or $iFlag = 5) Then Return SetError(3, 3, "")
    $hSearch = FileFindFirstFile($sPath & "\" & $sFilter)
    If $hSearch = -1 Then Return SetError(4, 4, "")
    While 1
        $sFile = FileFindNextFile($hSearch)
        If @error Then
            SetError(0)
            ExitLoop
        EndIf
        If $iFlag = 0 Then $asFileList = $asFileList & $sFile & "|"
        If $iFlag = 1 Then $asFileList = $asFileList & $sPath & "\" & $sFile & "|"
        If $iFlag = 2 And StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") = 0 Then $asFileList = $asFileList & $sFile & "|"
        If $iFlag = 3 And StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") = 0 Then $asFileList = $asFileList & $sPath & "\" & $sFile & "|"
        If $iFlag = 4 And StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") <> 0 Then $asFileList = $asFileList & $sFile & "|"
        If $iFlag = 5 And StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") <> 0 Then $asFileList = $asFileList & $sPath & "\" & $sFile & "|"
    WEnd
    FileClose($hSearch)
    Return StringSplit(StringTrimRight($asFileList, 1) , "|")
EndFunc
:arrow: J'ai un peu de mal à tout comprendre. Il est indiqué
$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


Mais ou sont-ils placés ? Es ce qu'il faut que je les mettent ? Si oui, ou sa ?

Merci,

JW

Re: [...] _FileListToArray modifié

Posté : ven. 14 mai 2010 14:41
par peuchere83
Bonjour,

La fonction FileListtoArray (et toutes ses déclinaisons : FileListtoArrayXT, ...) te retourne un tableau avec les informations filtrées en fonction de ce que tu as défini.
Exemple :

Code : Tout sélectionner

#Include <File.au3>
#Include <Array.au3>
$FileList=_FileListToArray(@DesktopDir, "*.*", 1)
msgbox(0,"Information", "il y a " & $FileList[0] & " fichiers sur mon bureau.")
For $i=1 to $FileList[$i] step 1
msgbox(0," Fichier n° " & $i, $FileList[$i])
Next
_ArrayDisplay($FileList,"$FileList")
Dans le code ci-dessus, la première ligne renvoyée (ligne 0) contient le nombre de fichiers trouvés sur ton bureau.
Ensuite je fais une boucle pour pouvoir lancer le cas échéant un traitement sur chaque fichier.
Et pour que tu comprennes bien le but de cette fonction, la dernière ligne de code t'affiche le retour de la fonction dans un tableau.

Je te conseille vivement de regarder ce post (http://www.autoitscript.fr/forum/viewto ... ray#p23428) car tu y trouveras des fonctions de recherches bien plus puissantes que celle que tu as trouvé (gestion de la récursivité, ....)

@+

Re: [...] _FileListToArray modifié

Posté : dim. 16 mai 2010 11:09
par JohnWayne
Salut

Merci de cette précision. J'ai déjà regardé ce post mais cela ne me fera pas de mal de le re regarder :lol:

++