Jump to content

Incorrect way to get an array within an array?


Recommended Posts

Hi Guys, I am really hoping someone here is able to help me or at least give me some kind of direction as to where I am going wrong.

Essentially what I am trying to do is read a list of directories defined withing a text file into an array, then loop through all of these directories and build a list of files within each directory....and then get the File Version of each file.

So far I have managed to get as far as build the list of files for each directory into an array. But every attempt to retrieve the File Version info after this fails miserably. I now get the following error: "Array variable has incorrect number of subscripts or subscript dimension range exceeded."

I am pretty sure I am doing this all kinds of wrong (such as having a For $ in a For $) but any guidance would be really appreciated, thanks

Script below:

Global $DirList = ("")
Global $DirListFile = (@ScriptDir & "\DirList.txt") ;File Containing a list of directories to be scanned
;Global $DirArray = (@ScriptDir & $DirList)


Func Test()
   $DirArray = _FileReadToArray ($DirListFile, $DirList)
   _ArrayDisplay($DirList)
   For $i = 1 To Ubound ($DirList) -1
      Global $Files = _FileListToArray ($DirList[$i], "*", 1, 0)
         _ArrayDisplay ($Files)
      Next
         For $f = 1 To Ubound ($Files) -1
            Global $version = FileGetVersion ($DirList[$i] & "\" & $Files[$f])
            _ArrayDisplay ($version)
         Next
EndFunc

Call ("Test")

 

 

Link to comment
Share on other sites

Good morning @vyeshaun, and welcome to the AutoIt forum :)
First, I would like to point you out about the indentation of your code, and some little things at which you should pay a little more attention.
First, the global variable $Files should not be in a function, just because you use local variables in it.
Second, you don't have a For...Loop in another For...Loop, because the first Next, closes the For...Loop, and the second one is the same.
Third, the declaration of $DirList... Just write $DirList, and you're ok. Same for $DirListFile... No needs to put parentheses to the declaration.
To point you out on the logic of your script, when you have read all the Directories, you have to loop through that array, and obtain a list of files, and then, for each file, get the version through the FileGetVersion() function :)

Hope that helps :)

EDIT: I think that you just started using AutoIt, so, I would suggest you, to read the Help file, in order to learn a bit about AutoIt :)
EDIT2: By the way, here you are a little script ( commented ), which I hope it will help you and will encourage you to use this powerful language which AutoIt is :)


 

#include <AutoItConstants.au3>
#include <File.au3>
#include <FileConstants.au3>

Local $strDirListFile = @ScriptDir & "\DirList.txt", _ ; Directory from which read the file
      $arrDirList, _                                   ; Array which contains the list of directories read from $strDirListFile
      $arrFileList, _                                  ; Array which contains the list of files for each directory
      $strFileVersion                                  ; File version of the file

; Read the content of the file $strDirListFile, and the result is stored in $arrDirList
_FileReadToArray($strDirListFile, $arrDirList, $FRTA_NOCOUNT)
; Always check for errors when you code! It helps a lot!
If @error Then
    ConsoleWrite("Error while obtaining the content of the file " & $strDirListFile & ". Error: " & @error & @CRLF)
Else
    _ArrayDisplay($arrDirList, "Dir List")
    ; Now that the result of $arrDirList is stored correctly, we can loop through it and obtain the list of files in each directory,
    ; using the _FileListToArray() function
    For $i = 0 To UBound($arrDirList) - 1
        $arrFileList = _FileListToArray($arrDirList[$i], "*", $FLTA_FILES, True)
        If @error Then
            ConsoleWrite("Error while obtaining the list of files in the directory " & $arrDirList[$i] & ". Error: " & @error & @CRLF)
        Else
            _ArrayDisplay($arrFileList, "File List in the Dir: " & $arrDirList[$i])
            ; Now that the list of files in the $arrDirList[$i] directoy has been stored correctly, you can get the info of them,
            ; one by one, using the FileGetVersion() function
            For $j = 1 To UBound($arrFileList) - 1
                $strFileVersion = FileGetVersion($arrFileList[$j])
                ; As it is reported in the Help file:
                ; If FileGetVersion() returns 0.0.0.0 or "", then, there are no information about what you were trying to obtain
                If @error Then
                    ConsoleWrite("No information availables from the file " & $arrFileList[$j] & @CRLF)
                Else
                    ; Finally, get the list of information about the file ( you can either get one information at time ),
                    ; specifying it as second parameter of the function FileGetVersion() ( look in the Help fil )
                    ConsoleWrite("File version of the file " & $arrFileList[$j] & " - " & $strFileVersion & @CRLF)
                EndIf
            Next
        EndIf
    Next
EndIf



Best Regards.
 

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

Thanks for the reply,

 

I am fairly new to AutoIt yes and constantly have the help pages open to lookup functions whilst I wokr, unfortunately I am just not very smart :)

The good news is thanks to your helpful suggestions I was able to get it working exactly as required before even seeing your edit :) So thank you you have been very helpful. I will look through your script with the comments to see if there are improvements I can make.

 

Thanks

Shaun 

Link to comment
Share on other sites

Slight modification of @FrancescoDiMuro code (see $arrFileVersion) this will place the files with a valid file version into an array.

#include <AutoItConstants.au3>
#include <File.au3>
#include <FileConstants.au3>

Local $strDirListFile = @ScriptDir & "\DirList.txt", _ ; Directory from which read the file
      $arrDirList, _                                   ; Array which contains the list of directories read from $strDirListFile
      $arrFileList, _                                  ; Array which contains the list of files for each directory
      $strFileVersion, _                               ; File version of the file
      $arrFileVersion[1][2]

; Read the content of the file $strDirListFile, and the result is stored in $arrDirList
_FileReadToArray($strDirListFile, $arrDirList, $FRTA_NOCOUNT)
; Always check for errors when you code! It helps a lot!
If @error Then
    ConsoleWrite("Error while obtaining the content of the file " & $strDirListFile & ". Error: " & @error & @CRLF)
Else
    _ArrayDisplay($arrDirList, "Dir List")
    ; Now that the result of $arrDirList is stored correctly, we can loop through it and obtain the list of files in each directory,
    ; using the _FileListToArray() function
    For $i = 0 To UBound($arrDirList) - 1
        $arrFileList = _FileListToArray($arrDirList[$i], "*", $FLTA_FILES, True)
        If @error Then
            ConsoleWrite("Error while obtaining the list of files in the directory " & $arrDirList[$i] & ". Error: " & @error & @CRLF)
        Else
            _ArrayDisplay($arrFileList, "File List in the Dir: " & $arrDirList[$i])
            ; Now that the list of files in the $arrDirList[$i] directoy has been stored correctly, you can get the info of them,
            ; one by one, using the FileGetVersion() function
            For $j = 1 To UBound($arrFileList) - 1
                $strFileVersion = FileGetVersion($arrFileList[$j])
                ; As it is reported in the Help file:
                ; If FileGetVersion() returns 0.0.0.0 or "", then, there are no information about what you were trying to obtain
                If @error Then
                    ConsoleWrite("No information availables from the file " & $arrFileList[$j] & @CRLF)
                Else
                    ; Finally, get the list of information about the file ( you can either get one information at time ),
                    ; specifying it as second parameter of the function FileGetVersion() ( look in the Help fil )
                    ConsoleWrite("File version of the file " & $arrFileList[$j] & " - " & $strFileVersion & @CRLF)
                    _ArrayAdd($arrFileVersion, $arrFileList[$j] & "|" & $strFileVersion)
                EndIf
            Next
        EndIf
    Next
    $arrFileVersion[0][0] = UBound($arrFileVersion) - 1
EndIf
_ArrayDisplay($arrFileVersion)

 

Link to comment
Share on other sites

@Subz
Good morning :)
Ye, if he wants to store the corresponding file version of any file, that's the way how to do it :)
Nice little improvement :)

@vyeshaun
Feel free to ask, if something is not clear to you :) Provide always the code you are working with, in order to see if we can help you from what you did :)
Have a good day everyone.


Best Regards.

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

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