Jump to content

Problem Array with _FileListToArray (list files)


Recommended Posts

Hi,

My probleme is the following.

I want to scan files in a folder which can contain sub-folders and files.

If my folder is empty (without files or folders) this function hangs :

$Files=_FileListToArray("C:\Scan","*",1) <== flag=1 is for files only
Msgbox(64,"Count Files","In this folder, there is : " & $Files[0])
                                                              ^ <== hangs here because no files are in this folder

There is no problem if the folder contains a least one sub-folder !!!

I think $Files[0] returns the number of elements in the folder (sub-folder and files), but why $Files[0] doesn't return 0 if no file is present ?

Have you an idea ?

Thanks

For information, I use AutoIt Version: 3.2.12.1.

Edited by HelpUsers
Link to comment
Share on other sites

$Files=_FileListToArray("C:\Scan","*.*",1) <== flag=1 is for files only
Msgbox(64,"Count Files","In this folder, there is : " & $Files[0])
                                                              ^ <== hangs here because no files are in this folde

I think it needs a *.* wildcard, not *

Link to comment
Share on other sites

$Files = _FileListToArray("C:\Scan", "*", 1) 
If @error Then
     Msgbox(64,"Error !", "Error Code: " & @error)
Else
     Msgbox(64,"Count Files","In this folder, there is : " & $Files[0])
EndIf

@Dampe.. " *" is a wildcard... it's actually the Default value used in the _FileListToArray() function.

I've already tried both "*" and "*.*" before post in this forum, but it still not working.

Do you think the problem can be in file.au3 for the '_FileListToArray' function ?

Thanks

Link to comment
Share on other sites

_FileListToArray function runs normally but there is some aspects that you didn't clear. Do you have C:/Scan as a directory on your computer or any files in there? If either of these is true then you should get a error coding for 1.

This works:

$Files = _FileListToArray("C:\", "*", 1)
If @error Then
     Msgbox(64,"Error !", "Error Code: " & @error)
Else
     Msgbox(64,"Count Files","In this folder, there is : " & $Files[0])
EndIf

But what you want you'll have to ensure it is created

If FileExists("C:\Scan") Then
    $Files = _FileListToArray("C:\Scan", "*", 1)
    If IsArray($Files) Then
        MsgBox(64, "Count Files", "In this folder, there is : " & $Files[0]))
    Else
        MsgBox(64, "Count Files", "In this folder, there is : 0")
    EndIf
EndIf

First I check if the directory is created and if it is run _FileListToArray and store it in $Files. Now what I did was check to see what it is, a array would mean the function worked those getting the Files[0] size. Then that leaves 0 being a error the only true problem that you could have are Invalid $sFilter or Invalid $iFlag while running the function

Contact via MSN: [email=terarink_msn@hotmail.com]terarink_msn@hotmail.com[/email], yahoo: terarink_yah

Link to comment
Share on other sites

I've tried your example.

Thanks

But it still not working correctly if C:\scan contains only sub-folder(s) but no files.

I've modified your script to make this :

While 1
$FILES=_FileListToArray("C:\Scan", "*", 1)
    If IsArray($FILES)=1 Then
    ; select if scan object is a file or a folder
    Select
    ; If number of files is >0
    Case $FILES[0]>0
    ExitLoop
    ; If folder 
    Case Else
    
    EndSelect
        
    EndIf

WEnd

; Script continue only if a file has been found

I think it can be useful for other users

Edited by HelpUsers
Link to comment
Share on other sites

See you have a problem then because then you goto create the directory if it doesn't exist

A more up-to-date function

CODE
#include <File.au3>

$s_Directory = ""

If FileExists($s_Directory) Then

$a_Files = _FileListToArray("$s_Directory", "*", 1)

MsgBox(0, "", "Test")

If IsArray($a_Files) Then

If $a_Files[0] > 0 Then

MsgBox(64, "Count Files", "In this folder, there is : " & $Files[0])

Else

MsgBox(64, "Count Files", "In this folder, there is : " & $s_Directory & " has no files")

EndIf

Else

MsgBox(64, "Count Files", "In this folder, there is : 0")

EndIf

Else

MsgBox(64, "Count Files", "There is no " & $s_Directory & " folder")

EndIf

Edited by TerarinKerowyn

Contact via MSN: [email=terarink_msn@hotmail.com]terarink_msn@hotmail.com[/email], yahoo: terarink_yah

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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