Question

I'm getting the following error in my AutoIt script:

"Array variable subscript badly formatted."

and it is thrown on this line: Local $allDirs[$countDirs]

Func archiveDir($dir)

    ; Get all the files under the current dir
    $allOfDir = _FileListToArray($dir)
    Local $countDirs = 0
    Local $countFiles = 0

    $imax = UBound($allOfDir)
    For $i = 0 to $imax - 1
        If StringInStr(FileGetAttrib($allOfDir[$i]),"D") Then
            $countDirs = $countDirs + 1
        Else
            $countFiles = $countFiles + 1
        EndIf   
    Next

    Local $allDirs[$countDirs]
    Local $allFiles[$countFiles]

Any ideas?

Was it helpful?

Solution

I'm guessing you either don't have any subdirectories or your code to find them isn't working correctly. So your code is trying to declare an array of 0 length.

Add this line right before the line where you get the error.

MsgBox(0, "Value of $countDirs", $countDirs)

UPDATE

_FileListToArray only returns the file/folder names, not the full path. The call to FileGetAttrib is returning an empty string because it does not find the file/folder. Modify your If to include the parent path with the file name.

If StringInStr(FileGetAttrib($dir & "\" & $allOfDir[$i]), "D") Then

OTHER TIPS

Running your code, I only get an error if $countDirs or $countFiles is equal to 0. You should be checking for this before trying to use these values when declaring your arrays.

Also, a quick note, your For Loop is starting at 0... in AuotIt the 0 index of an array holds the count of items in the array. You could do it like this instead:

For $i = 1 to $allOfDir[0]
    If StringInStr(FileGetAttrib($allOfDir[$i]), "D") Then
        $countDirs+=1
    Else
       $countFiles+=1
    EndIf
Next

If ($coundDirs > 0) Then
   Local $allDirs[$countDirs]
   ; do whatever else you need to do here.
EndIf

If ($countFiles > 0) Then
   Local $allFiles[$countFiles]
   ; do whatever else you need to do here.
EndIf
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top