[...] _FileListToArray modifié

Aide et conseils concernant AutoIt et ses outils.
Règles du forum
.
Répondre
JohnWayne
Niveau 4
Niveau 4
Messages : 68
Enregistré le : mer. 24 mars 2010 17:48
Status : Hors ligne

[...] _FileListToArray modifié

#1

Message 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
Avatar du membre
peuchere83
Niveau 5
Niveau 5
Messages : 169
Enregistré le : mer. 17 déc. 2008 10:50
Status : Hors ligne

Re: [...] _FileListToArray modifié

#2

Message 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é, ....)

@+
Tous biens que tu possèdes est un souci qui te retient.
Skippy est là pour t'enlever tous tes soucis.
JohnWayne
Niveau 4
Niveau 4
Messages : 68
Enregistré le : mer. 24 mars 2010 17:48
Status : Hors ligne

Re: [...] _FileListToArray modifié

#3

Message 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:

++
Répondre