AutoIt中的“数组变量下标格式错误”

时间:2010-05-17 18:31:26

标签: autoit

我的AutoIt脚本中出现以下错误:

  

“数组变量下标格式错误。”

并抛出此行: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]

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

我猜你要么没有任何子目录,要么找到它们的代码无法正常工作。所以你的代码试图声明一个0长度的数组。

在收到错误的行之前添加此行。

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

更新

_FileListToArray仅返回文件/文件夹名称,而不是完整路径。对FileGetAttrib的调用返回一个空字符串,因为它找不到文件/文件夹。修改您的If以包含带有文件名的父路径。

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

答案 1 :(得分:1)

运行代码,如果$ countDirs或$ countFiles等于0,我只会收到错误。在声明数组时尝试使用这些值之前,应该检查这个。

另外,快速说明,你的For循环从0开始......在AuotIt中,数组的0索引保存数组中的项目数。你可以这样做:

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