Jump to content

Checking folder for files if empty then crash


Go to solution Solved by LarsJ,

Recommended Posts

Hi,

i have problem, i started using an array for checking files in a folder and adding them to a combobox.

But if the folder is empty then my script crashes, i tried to check the array with:

$NAME   = GUICtrlRead($AFDELINGLIST)
                    $VAR    = IniRead("P:\Afdelingen\afdelingen.ini", "names", $NAME, "")
                GUICtrlSetState($SOFTWARE, $GUI_ENABLE)
                GUICtrlSetData($SOFTWARE, "")
                GUICtrlSetData($SOFTWARE, "Geen installatie bestanden")
                GUICtrlSetData($SOFTWARE, "================================================")
                    Local $FileList = _FileListToArray("P:\Software\Afdelingen\" & $VAR & "\", "*.*", $FLTA_FILES)
                        For $i = 1 To $FileList[0]
                            If $i <> "" Then
                                GUICtrlSetData($SOFTWARE, $FileList[$i])
                            EndIf
                        Next

But when i run my script and i get to the point where it reads the ini file (checked the values comming from that with msgbox, they are good), after that it should go to the array but then it crashes if the folder is empty, if i add a file the array works. 

How can i catch this error?

Link to comment
Share on other sites

  • Solution

Click _FileListToArray in the code box and read. Especially about failure and @error in the "Return Value"-section.

Link to comment
Share on other sites

 

Try this:

This "If $i <> "" Then" is useless because you start with $i with value 1 (That for). So that check is useless because it always happen this way!

Local $FileList = _FileListToArray("P:\Software\Afdelingen\" & $VAR & "\", "*.*", $FLTA_FILES)
If ($FileList[0] <> 0) Then
   For $i = 1 To $FileList[0]
        GUICtrlSetData($SOFTWARE, $FileList[$i])

   Next
EndIf

Read this too:

https://www.autoitscript.com/autoit3/docs/libfunctions/_FileListToArray.htm

Edited by DocTorCoder

DocTorCoder

Link to comment
Share on other sites

Link to comment
Share on other sites

DocTorCoder, What do you think happens if the folder is empty and $FileList is not an array?

Nothing,Filelist will be empty,so size "0" and will not enter in for. I am missing something?

Or maybe will return "4 - No File(s) Found" ? Didnt test it . I just think of what would happened!

Edited by DocTorCoder

DocTorCoder

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