Forum Discussion

Alina's avatar
Alina
Occasional Contributor
12 years ago

Array in a Loop - to get a list of Folder names - VBScript


Sub TestArray()



Dim FolderList(5), i



Dim Table



FolderList(0)="NAME1"



FolderList(1)="NAME2"



FolderList(2)="NAME3"



FolderList(3)="NAME4"



if Not



Project.Variables.VariableExists("MyTable") Then



Call Project.Variables.AddVariable("MyTable", "Table")



End



If



Set Table = Project.Variables.VariableByName("MyTable")



For



i = 0 to ubound(FolderList)



 



Log.Message (FolderList)



for (var i = 0; i < tbl.rowCount; i++)



Log.Message (FolderList)



next



Next





End Sub


9 Replies


  • Hi Alina,


     


    In my opinion, there is no need to use the Windows Explorer window for this. You can use special methods that allow working with files and folders. For example, the script below gets all subfolders inside the specified folder and posts their names to the Test Log (taken from this article):




    //JScript


    function SubFoldersFinder()


    {


      // Specifies the path to the desired folder


      var sPath = "C:\\MyFolder";


     


      // Obtains information about the folder


      var FolInfo = aqFileSystem.GetFolderInfo(sPath);


     


      // Obtains the collection of subfolders


      var colSubFolders = FolInfo.SubFolders;


     


      // Checks whether the collection is not empty


      if (colSubFolders != null)


     


      {


        // Posts the names of the folder's subfolders to the test log


        Log.AppendFolder("The " + sPath + " folder contains the following subfolders:");


        while (colSubFolders.HasNext())


        {


          // Obtains the current subfolder


          var FolItem = colSubFolders.Next();


          // Posts the subfolder's name to the test log


          Log.Message(FolItem.Name);


        }


        Log.PopLogFolder();


     


      }


      else


        Log.Message("The specified folder does not contain any subfolders.");


     





  • You've got a mix of VB and C or JS and some other issues. See below for individual comments.



    I haven't messed with the project variables so I can't help you there but I would recommend a few things. Declare all your variables as a specific type, e.g. Dim i as Integer. It will help you debug problems later when you mistakenly try to assign an Integer to a variable typed as String, etc.



    You have nested loops that both loop i. I would change the inner loop to j or whatever your second favorite variable name is.



    When you do a For... Next loop, put the variable in the Next. It will make things clearer as your code gets more complex. e.g.

    For i = 1 to 10

       ... do something

    Next i <-- variable name goes here, don't just put Next



    You have declared FolderList to have 6 (index starts at 0) elements but are only assigning 4. I'm assuming that it's just demo code but just in case.



    Your second If doesn't have a condition or an EndIf, I'm assuming you just missed it when you copied pasted code and trimmed it down.



    Your second/inner For loop is written in C or JS or some non-VB language. I don't know if TS allows the mix but I wouldn't recommend mixing languages in a file or probably even a project. Just my opinion...



    I've pasted some code below that works that has some commented areas that you will need to fix because I'm missing information on what you want done there.



    I would suggest that you take the code snippets (like you posted) and put them in a separate file and get a simple example working. It will help you more easily find and fix problems. Also, when you post code here you can post the entire non-working sample rather than a mix of code that clearly was pulled from multiple areas in your code. It will help us help you better/faster.



    Sub TestArray()

        Dim FolderList(5) As String

        Dim i As Integer

        Dim j As Integer

        Dim Table As Object

       

        FolderList(0) = "NAME1"

        FolderList(1) = "NAME2"

        FolderList(2) = "NAME3"

        FolderList(3) = "NAME4"

       

    '    If Not Project.Variables.VariableExists("MyTable") Then

    '        Call Project.Variables.AddVariable("MyTable", "Table")

    '    End

    '

    '    If

    '        Set Table = Project.Variables.VariableByName("MyTable")

        For i = 0 To UBound(FolderList)

            Log.Message "#" & i & ": " & FolderList(i)

    '        For j = 0 To tbl.RowCount

    '            Log.Message (FolderList)

    '        Next j

        Next i

    End Sub



    ... quick edit to remove Debug.Print and replace with Log.Message... I was playing with this in Excel VBA to get it working quickly.
  • You don't need

        set size...

    just use

        size = UBound(ItemList())



    The rest should work. What you are asking here is specific to the VB language and not Test Complete. I would suggest that you search sites like http://stackoverflow.com/ or just google for "vb get folder names" or something like that. http://www.mrexcel.com/ is also good but it's mostly about Excel & Excel VBA.
  • Alina's avatar
    Alina
    Occasional Contributor
    Hi,



    Ive managed it, Thanks to both Jeff and Tanya; you are starts :-)



    I used the VB script article on th link that Tanya sent. I think what I was trying to do with the For-Next loop was more complicated; I done the same using a simpler method.

    Makes life much easier,



    Thanks once again,
  • Alina's avatar
    Alina
    Occasional Contributor
    Sorry, please see code in my post above,

    What I want to achieve is:

    Loop through a list of folders in an explorer window (Folder List)

    and get the names of the Folders only and write these to a variable i.e. Projects.variables...

    I have created the array and the For Next loop as you can see in the code above, but it is failing miserably.



    Any help will be greatly appreciated.



    Thanks,

  • Alina's avatar
    Alina
    Occasional Contributor
    Hi Jeff,



    Thanks for your help, im afraid I still cant get it working.



    I have a list of four folders in a path.

    I want to create a For Next Loop that loops through the list of folders..

    and Return the Folder name. Thats all :-)



    Its quite simple but I'm making it so complicated. The code I have is below:

    Please can someone help in getting it working:


    Sub TestArray()



    Dim ItemList(5)



    Dim i



    Dim j



    Dim size



    ItemList(0) = "name1"



    ItemList(1) = "name2"



    ItemList(2) = "name3"



    ItemList(3) = "name4"



    set size = UBound(ItemList())



    Call Aliases.Explorer.Window.SetFocus



    For i = 0 To size



    Log.Message "The Item names are" & ItemList(i)



    ' For j = 0 To Table.



    ' Log.Message (ItemList)



    ' Next j



    Next



    End Sub


  • Alina's avatar
    Alina
    Occasional Contributor
    Thanks for your help Tanya, im scripting using VBScript and what I want to achieve is: I have 3 top level folders and one text file in a list and I want to create a For Next loop that iterates through each row and logs the name of the Folder/File to the test log.

    Thanks,



    Im getting the subscript out of range error on ItemList.

    The code now reads:


    Sub



    TestArray()



     



    Dim



    ItemList(5)



     



    Dim



    i, j, size



     



     



     



    ItemList(0) = "name1"



     



    ItemList(1) = "name2"



     



    ItemList(2) = "name3"



     



    ItemList(3) = "name4"



     



    size = UBound(ItemList())



    'Call Aliases.Explorer.Window.SetFocus



     



    For



    i = 0 To size



     



    Log



     



     



     



    .Message "The Item names are" & (ItemList())



    ' For j = 0 To Table.



     



    ' Log.Message (ItemList)



     



    ' Next j



     



    Next



     



    End



    Sub


  • Alina's avatar
    Alina
    Occasional Contributor
    Hi,



    I've managed to get the list of subfolders in a folder but not the files in it.

    I understand there is a getFile routine, however i'd like to combine both 'GetFolder' and 'GetFile' routine into one.. i.e. so it lists the Folder contents.



    I am having trouble combining the two... any suggestions if there is an easier way of doing this? I.e. GetFolderContents would be just Ideal! but its not available!!!!



    Please suggest, Thanks