tags:

views:

127

answers:

2
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]

The error text is : "Array variable subscript badly formatted." and comes on this line:

Local $allDirs[$countDirs]

Any ideas?

+1  A: 

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
aphoria
I assume that is the error too but do you see anything wrong with my code to get directories and/or files for that matter?
Scott
Does @WorkingDir contain the parent path (without the ending '\')? I'm using that now and it doesn't seem to be working
Scott
From the AutoIt help document -- @WorkingDir Current/active working directory. (Result does not contain a trailing backslash) Actually, I'm not sure what @WorkingDir represents. When I reference it, it points to a different location than where I'm running the script from.
aphoria
+1  A: 

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
JohnForDummies
Are you sure? This is a quote from http://www.autoitscript.com/wiki/Arrays"Assigning Data to Array ElementsWhen we declare the array we make some room in memory for future data. We want to assign some data to the items in the array. Now here is the catch. The array always starts it's index at 0. So, the first element in the array referenced by a 0. "
Scott
Sorry, I shouldn't have said all arrays in AutoIt, but some of the UDF's pertaining to arrays use index 0 for storing the number of items in the array. _FileListToArray is one of them that uses this convention.
JohnForDummies

related questions