Sign in to follow this  
Followers 0
hot202

Array help

11 posts in this topic

hi i have this script but if i keep playing with it and adding folders or deleting folders i get a error

C:\Users\myname\Desktop\test.au3 (35) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

If $x_Files[$x] <> $o_Files[$x] Then

If $x_Files[$x] <> ^ ERROR

#include <File.au3>
#include <Array.au3>
#include <Constants.au3>
#NoTrayIcon
Opt("TrayMenuMode", 1)
TraySetState()
TraySetToolTip("File List Auto-Update")
TrayTip("File List Auto-Updater Loaded Succesfully!", "This program will check for changes on the folder specified, and will Auto-update the List of files in the specified folder into the file: songs.txt * Software Created By SeeR *", 5, 1)
$howto = TrayCreateItem("How to configure")
$filename = TrayCreateItem("File Name")
$selectfolder = TrayCreateItem("Select Folder")
$reset = TrayCreateItem("Reset Config_data")
$aboutitem = TrayCreateItem("About")
$exititem = TrayCreateItem("Exit")
If FileReadLine("config.txt", 1) = "" Then
    $folderselect = fileselectFolder("Select folder to check for files", "", 1)
    FileWriteLine("config.txt", $folderselect)
EndIf
If FileReadLine("config.txt", 2) = "" Then
    $fileselect = filesaveDialog("Select name of the file List file", "", "File_List Text file (*.txt)", 2)
    If StringInStr($fileselect, ".txt") = False Then
        $fileselect = $fileselect & ".txt"
    EndIf
    FileWriteLine("config.txt", $fileselect)
EndIf
$folderinfo = FileReadLine("config.txt", 1)
$fileinfo = FileReadLine("config.txt", 2)
Global $oFiles, $nFiles, $xFiles, $x_Files, $o_Files
CheckFiles()
While 1
    If FileExists(@ScriptDir & "\config.txt") Then
        ReCheck()
        If IsArray($x_Files) And IsArray($o_Files) Then
            For $x = 1 To $x_Files[0]
                If $x_Files[$x] <> $o_Files[$x] Then
                    TraySetState(4)
                    TrayTip("File List Auto-Update", "File List is now Auto-updating...", 2, 1)
                    Sleep(100)
                    For $i = 1 To $x_Files[0];checks to see if a file was added to the folder
                        If StringInStr($oFiles, $x_Files[$i]) = False Then
                            $Listed = "Added file:   " & $x_Files[$i]
                        EndIf
                    Next
                    For $i = 1 To $o_Files[0];checks to see if a file was deleted from the folder
                        If StringInStr($xFiles, $o_Files[$i]) = False Then
                            $Listed = "Deleted file:   " & $o_Files[$i]
                        EndIf
                    Next
                    FileWrite($fileinfo, $Listed & @CRLF)
                    Sleep(20)
                    TraySetState(8)
                    TrayTip("File List Auto-Update", "File List have been updated succesfully!", 2, 1)
                    CheckFiles()
                EndIf
            Next
        EndIf
    EndIf
    $msg = TrayGetMsg()
    Select
        Case $msg < 1
            ContinueLoop
        Case $msg = $aboutitem
            MsgBox(0, "About", "This software is created by SeeR * © by SeeR * Feel free to use, but don't take the credits for it.")
        Case $msg = $exititem
            ExitLoop
        Case $msg = $selectfolder
            FileWriteLine(@ScriptDir & "\config.txt", $folderselect & @CRLF)
        Case $msg = $filename
            FileWriteLine(@ScriptDir & "\config.txt", $fileselect & @CRLF)
        Case $msg = $howto
            MsgBox(0, "Configuration", "To configure this program correct you need to do the following things: " & @CRLF & "If you already tried to configure, reset the configuration data (click on the programs icon down in right corner)" & @CRLF & "1. Select what folder is going to be checked" & @CRLF & "2. Select the filename for the saved file List (IMPORTANT: you MUST add .txt after the filename when you save" & @CRLF & "3. Don't delete the file 'config.txt'. If you move the .exe file, also move the 'config.txt'" & @CRLF & @CRLF & "Enjoy! © by SeeR")
        Case $msg = $reset
            If FileExists(@ScriptDir & "\config.txt") Then
                FileDelete(@ScriptDir & "\config.txt")
                TrayTip("File List Auto-Update", "Config succesfully reset!", 2, 1)
            Else
                MsgBox(0, "Config file not found!", "The config file needed by file List Auto-Updater could not be found!" & @CRLF & "The file may have been moved, or just not created yet - please follow the instructions and try again")
            EndIf
    EndSelect
WEnd
Exit
Func CheckFiles()
    $files = ""
    $oFiles = ""
    $o_Files = ""
    $files = _FileListToArray($folderinfo)
    If IsArray($files) Then
        For $x = 1 To $files[0]
            $oFiles = ($oFiles & $folderinfo & "\" & $files[$x] & ";")
        Next
    EndIf
    $oFiles = StringTrimRight($oFiles, 1)
    $o_Files = StringSplit($oFiles, ";")
EndFunc  ;==>CheckFiles
Func ReCheck()
    $nFiles = ""
    $x_Files = ""
    $xFiles = ""
    $nFiles = _FileListToArray($folderinfo)
    If IsArray($nFiles) Then
        For $x = 1 To $nFiles[0]
            $xFiles = ($xFiles & $folderinfo & "\" & $nFiles[$x] & ";")
        Next
    EndIf
    $xFiles = StringTrimRight($xFiles, 1)
    
    $x_Files = StringSplit($xFiles, ";")
EndFunc  ;==>ReCheck

Share this post


Link to post
Share on other sites



try running it with

#AutoIt3Wrapper_Run_Debug_Mode=y

(place at the top of your script.)


"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

i ran that it dosnt tell me anything else thats wrong with it it shows where at the script it stops working.

Share this post


Link to post
Share on other sites

Erm. Are you using Scite4AutoIt to run the script ?

It should output(STDOUT) a log of the code/lines that are executed.

Making it easier to see why your array is not a array anymore.


"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

this is what i get. just the last bit didnt copy it all cos there is heaps.

0033: 0-0:         ReCheck()
0097: 0-0:     $nFiles = ""
0098: 0-0:     $x_Files = ""
0099: 0-0:     $xFiles = ""
0100: 0-0:     $nFiles = _FileListToArray($folderinfo)
0101: 0-0:     If IsArray($nFiles) Then
0102: 0-0:         For $x = 1 To $nFiles[0]
0103: 0-0:             $xFiles = ($xFiles & $folderinfo & "\" & $nFiles[$x] & ";")
0104: 0-0:         Next
0105: 0-0:     EndIf
0106: 0-0:     $xFiles = StringTrimRight($xFiles, 1)
0108: 0-0:     $x_Files = StringSplit($xFiles, ";")
0109: 1-0: EndFunc  ;==>ReCheck
0034: 1-0:         If IsArray($x_Files) And IsArray($o_Files) Then
0035: 0-0:             For $x = 1 To $x_Files[0]
0036: 0-0:                 If $x_Files[$x] <> $o_Files[$x] Then
0056: 0-0:             Next
0057: 0-0:         EndIf
0058: 0-0:     EndIf
0059: 0-0:     $msg = TrayGetMsg()
0060: 0-0:     Select
0062: 0-0:             ContinueLoop
0032: 0-0:     If FileExists(@ScriptDir & "\config.txt") Then
0033: 0-0:         ReCheck()
0097: 0-0:     $nFiles = ""
0098: 0-0:     $x_Files = ""
0099: 0-0:     $xFiles = ""
0100: 0-0:     $nFiles = _FileListToArray($folderinfo)
0101: 0-0:     If IsArray($nFiles) Then
0102: 0-0:         For $x = 1 To $nFiles[0]
0103: 0-0:             $xFiles = ($xFiles & $folderinfo & "\" & $nFiles[$x] & ";")
0104: 0-0:         Next
0105: 0-0:     EndIf
0106: 0-0:     $xFiles = StringTrimRight($xFiles, 1)
0108: 0-0:     $x_Files = StringSplit($xFiles, ";")
0109: 1-0: EndFunc  ;==>ReCheck
0034: 1-0:         If IsArray($x_Files) And IsArray($o_Files) Then
0035: 0-0:             For $x = 1 To $x_Files[0]
0036: 0-0:                 If $x_Files[$x] <> $o_Files[$x] Then
0056: 0-0:             Next
0057: 0-0:         EndIf
0058: 0-0:     EndIf
0059: 0-0:     $msg = TrayGetMsg()
0060: 0-0:     Select
0062: 0-0:             ContinueLoop
0032: 0-0:     If FileExists(@ScriptDir & "\config.txt") Then
0033: 0-0:         ReCheck()
0097: 0-0:     $nFiles = ""
0098: 0-0:     $x_Files = ""
0099: 0-0:     $xFiles = ""
0100: 0-0:     $nFiles = _FileListToArray($folderinfo)
0101: 0-0:     If IsArray($nFiles) Then
0102: 0-0:         For $x = 1 To $nFiles[0]
0103: 0-0:             $xFiles = ($xFiles & $folderinfo & "\" & $nFiles[$x] & ";")
0104: 0-0:         Next
0105: 0-0:     EndIf
0106: 0-0:     $xFiles = StringTrimRight($xFiles, 1)
0108: 0-0:     $x_Files = StringSplit($xFiles, ";")
0109: 1-0: EndFunc  ;==>ReCheck
0034: 1-0:         If IsArray($x_Files) And IsArray($o_Files) Then
0035: 0-0:             For $x = 1 To $x_Files[0]
0036: 0-0:                 If $x_Files[$x] <> $o_Files[$x] Then
0056: 0-0:             Next
0057: 0-0:         EndIf
0058: 0-0:     EndIf
0059: 0-0:     $msg = TrayGetMsg()
0060: 0-0:     Select
0062: 0-0:             ContinueLoop
0032: 0-0:     If FileExists(@ScriptDir & "\config.txt") Then
0033: 0-0:         ReCheck()
0097: 0-0:     $nFiles = ""
0098: 0-0:     $x_Files = ""
0099: 0-0:     $xFiles = ""
0100: 0-0:     $nFiles = _FileListToArray($folderinfo)
0101: 0-0:     If IsArray($nFiles) Then
0102: 0-0:         For $x = 1 To $nFiles[0]
0103: 0-0:             $xFiles = ($xFiles & $folderinfo & "\" & $nFiles[$x] & ";")
0104: 0-0:         Next
0105: 0-0:     EndIf
0106: 0-0:     $xFiles = StringTrimRight($xFiles, 1)
0108: 0-0:     $x_Files = StringSplit($xFiles, ";")
0109: 1-0: EndFunc  ;==>ReCheck
0034: 1-0:         If IsArray($x_Files) And IsArray($o_Files) Then
0035: 0-0:             For $x = 1 To $x_Files[0]
0036: 0-0:                 If $x_Files[$x] <> $o_Files[$x] Then
0056: 0-0:             Next
0057: 0-0:         EndIf
0058: 0-0:     EndIf
0059: 0-0:     $msg = TrayGetMsg()
0060: 0-0:     Select
0062: 0-0:             ContinueLoop
0032: 0-0:     If FileExists(@ScriptDir & "\config.txt") Then
0033: 0-0:         ReCheck()
0097: 0-0:     $nFiles = ""
0098: 0-0:     $x_Files = ""
0099: 0-0:     $xFiles = ""
0100: 0-0:     $nFiles = _FileListToArray($folderinfo)
0101: 0-0:     If IsArray($nFiles) Then
0102: 0-0:         For $x = 1 To $nFiles[0]
0103: 0-0:             $xFiles = ($xFiles & $folderinfo & "\" & $nFiles[$x] & ";")
0104: 0-0:         Next
0103: 0-0:             $xFiles = ($xFiles & $folderinfo & "\" & $nFiles[$x] & ";")
0104: 0-0:         Next
0105: 0-0:     EndIf
0106: 0-0:     $xFiles = StringTrimRight($xFiles, 1)
0108: 0-0:     $x_Files = StringSplit($xFiles, ";")
0109: 0-0: EndFunc  ;==>ReCheck
0034: 0-0:         If IsArray($x_Files) And IsArray($o_Files) Then
0035: 0-0:             For $x = 1 To $x_Files[0]
0036: 0-0:                 If $x_Files[$x] <> $o_Files[$x] Then
0056: 0-0:             Next
0036: 0-0:                 If $x_Files[$x] <> $o_Files[$x] Then
C:\Users\hot202\Desktop\test_DebugIt.au3 (145) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
If $x_Files[$x] <> $o_Files[$x] Then
If $x_Files[$x] <> ^ ERROR
->19:27:10 AutoIT3.exe ended.rc:1

Share this post


Link to post
Share on other sites

If IsArray($x_Files) And IsArray($o_Files) Then

;; debug your data. (to lead you to the code parts thats are not compatible.)

;; assume nothing, check everything.

If ubound($o_Files,1) <> $o_Files[0] then EXIT 962 ;; array size index mismatch.

If ubound($x_Files,1) <> $x_Files[0] then EXIT 963 ;; array size index mismatch.

If $x_Files[0] <> $o_Files[0] then EXIT 961 ;; array's size mismatch.


"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

thats what im stuck on i cant really find whats wrong.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Than you need someone else to help you further. (having a headache anyway.)

GL

Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

At first glance it seems fairly simple. If the array returned by ReCheck() is bigger than the array returned by CheckFiles(), then it tries to acces an index that does exist in $x_files, but does not exist in $o_files. I think this happens when you add item while the script has executed Checkfiles() last and hasn't gotten to ReCheck() yet.

One way to fix this is by adding a second for loop, so you can compare every index in $x_files to every index in $o_files.

Keep in mind that I've only looked at the code quickly, so perhaps I'm being a bit hasty on the conclusion.

edit:

I think this is pretty foolproof, but I there might still be ways to make it crash.

I commented what I thought mattered, but if you have questions just ask.

#include <File.au3>
#include <Constants.au3>
#NoTrayIcon

Opt("TrayMenuMode", 1)
TraySetState()
TraySetToolTip("File List Auto-Update")
TrayTip("File List Auto-Updater Loaded Succesfully!", "This program will check for changes on the folder specified, and will Auto-update the List of files in the specified folder into the file: songs.txt * Software Created By SeeR *", 5, 1)

;Explicitly declaring variables and descriptive names are always a good idea
Global $sConfigPath = @ScriptDir & "\config.ini" ;changed to ini format as the filewriteline was too much trouble when changing the values.
Local $CtrlHowTo = TrayCreateItem("How to configure") ;technically all these are global too, but I don't use them that way, so I use Local (Local only exists in functions)
Local $CtrlFileName = TrayCreateItem("File Name")
Local $CtrlSelectFolder = TrayCreateItem("Select Folder")
Local $CtrlReset = TrayCreateItem("Reset Config_data")
Local $CtrlAbout = TrayCreateItem("About")
Local $CtrlExit = TrayCreateItem("Exit")
Local $nInterval = 500 ;check for changes every 500 ms

Global $sFolderPath = IniRead($sConfigPath,"Paths","Folder","")
If Not FileExists($sFolderPath) Then
    $sFolderPath = _SetFolder() ;moved this to a func, so it can be called again when changing the path
EndIf

Global $sFilePath = IniRead($sConfigPath,"Paths","File","")
If $sFilePath = "" Then
    $sFilePath = _SetFile() ;also so it can be called again
EndIf

Global $hListFile = FileOpen($sFilePath,1) ;fileopen if you want to write more then once

Global $aLastFileList = _FileListToArray($sFolderPath) ;completely did away with the check functions
If Not IsArray($aLastFileList) Then Local $aLastFileList[1] = [0] ;in case the folder is empty

AdlibRegister("_CheckChanges",$nInterval) ;I don't want to slow the main loop down, and I don't want to check for changes every few MS, so I use this

While 1
    Switch TrayGetMsg()
    Case 0
    ContinueLoop
    Case $CtrlAbout
    MsgBox(0, "About", "This software is created by SeeR * © by SeeR * Feel free to use, but don't take the credits for it.")
    Case $CtrlExit
            FileClose($hListFile)
            AdlibUnRegister("_CheckChanges")
    Exit
    Case $CtrlSelectFolder
            $sFolderPath = _SetFolder() ;this is why I created the _SetFolder function
    Case $CtrlFileName
    $sFilePath = _SetFile()
    Case $CtrlHowTo
    MsgBox(0, "Configuration", "To configure this program correct you need to do the following things: " & @CRLF & "If you already tried to configure, reset the configuration data (click on the programs icon down in right corner)" & @CRLF & "1. Select what folder is going to be checked" & @CRLF & "2. Select the filename for the saved file List (IMPORTANT: you MUST add .txt after the filename when you save" & @CRLF & "3. Don't delete the file 'config.txt'. If you move the .exe file, also move the 'config.txt'" & @CRLF & @CRLF & "Enjoy! © by SeeR")
    Case $CtrlReset
    If FileExists($sConfigPath) Then
    FileDelete($sConfigPath)
    TrayTip("File List Auto-Update", "Config succesfully reset!", 2, 1)
                $sFolderPath = _SetFolder() ;or exitloop here, whatever you like.
                $sFilePath = _SetFile()
    Else
    MsgBox(0, "Config file not found!", "The config file needed by file List Auto-Updater could not be found!" & @CRLF & "The file may have been moved, or just not created yet - please follow the instructions and try again")
    EndIf
    EndSwitch
WEnd

Func _CheckChanges()
    Local $sChangeList, $sMessage
    Local $aNewFileList = _FileListToArray($sFolderPath)
    If @error Then
        Local $aNewFileList[1] = [0]
    EndIf

    ;check for deleted files
    For $i_1 = 1 To $aLastFileList[0]
        For $i_2 = 1 To $aNewFileList[0]
            If $aLastFileList[$i_1] == $aNewFileList[$i_2] Then
                ContinueLoop 2 ;this file still exists
            EndIf
        Next
        ;if the script reaches this line a file has been added
        $sChangeList &= "Deleted file: " & $sFolderPath & "\" & $aLastFileList[$i_1] & @CRLF ;add to the changelist
        $sMessage &= "Deleted file: " & $aLastFileList[$i_1] & @CRLF ;small format for the popup
    Next

    ;check for added files
    For $i_1 = 1 To $aNewFileList[0] ;loop through
        For $i_2 = 1 To $aLastFileList[0]
            If $aNewFileList[$i_1] == $aLastFileList[$i_2] Then
                ContinueLoop 2 ;this file already existed
            EndIf
        Next
        ;if the script reaches this line a file has been added
        $sChangeList &= "Added file: " & $sFolderPath & "\" & $aNewFileList[$i_1] & @CRLF ;add to the changelist
        $sMessage &= "Added file: " & $aNewFileList[$i_1] & @CRLF ;small format for the popup
    Next

    If $sChangeList Then
        ;personal preference to have just the one message
;~      TraySetState(4)
;~      TrayTip("File List Auto-Update", "File List is now Auto-updating...", 2, 1)
        FileWrite($hListFile, $sChangeList)
        TrayTip($sFilePath, $sMessage, 2)
;~      TraySetState(8)
;~      TrayTip("File List Auto-Update", "File List has been updated succesfully!", 2, 1)
        $aLastFileList = $aNewFileList ;we've checked everything, so the new array is not the old array.
    EndIf
EndFunc

Func _SetFolder()
    Local $sPath
    Do
        $sPath = FileSelectFolder("Select folder to check for files", "", 1)
    Until FileExists($sPath)
    IniWrite($sConfigPath,"Paths","Folder",$sPath)
    Return $sPath
EndFunc

Func _SetFile()
    Local $sPath
    Do
        $sPath = FileSaveDialog("Select name of the file List file", "", "File_List Text file (*.txt)", 2)
    Until $sPath
    If StringRight($sPath,4) <> ".txt" Then
        $sPath &= ".txt"
    EndIf
    IniWrite($sConfigPath,"Paths","File",$sPath)
    Return $sPath
EndFunc
Edited by Tvern

Share this post


Link to post
Share on other sites

yeah i think your right

Share this post


Link to post
Share on other sites

thanks heaps i think thats working. thankyou so much.

Share this post


Link to post
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
Sign in to follow this  
Followers 0