Forum Discussion

Alina's avatar
Alina
Occasional Contributor
12 years ago

Get Folder Contents

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
  • IndianTCNoob's avatar
    IndianTCNoob
    Occasional Contributor
    Sure.





     '...

       

        dim path, folder, logfolder, i

        

        path = ... 'specify path to the folder here

        set folder = aqFileSystem.GetFolderInfo(path)

        logfolder = Log.CreateFolder(folder.Name)

        Call Log.PushLogFolder(logfolder)

        for i = 0 to folder.SubFolders.count-1

            call Log.CreateFolder(folder.SubFolders.Item(i).Name, "Folder")

        next

       for i = 0 to folder.Files.count-1

            call Log.Message(folder.Files.Item(i).Name, "File")

       next



    '...

  • maximojo's avatar
    maximojo
    Frequent Contributor
    I use



    var foundFiles = aqFileSystem.FindFiles(myDir, *.txt);



    where



       aqFileSystem.FindFiles(Path, SearchPattern, SearchInSubDirs)



    Check it out in the help as to how to iterate through the files! Of course the "SearchPattern" can be "*.*"



    Here's the jscript example from the help:



    function FileFinder()

    {

      var foundFiles, aFile;

      foundFiles = aqFileSystem.FindFiles("C:\\Work\\", "*.exe");

      if (foundFiles != null)

        while (foundFiles.HasNext())

        {

          aFile = foundFiles.Next();

          Log.Message(aFile.Name);

        }

      else

        Log.Message("No files were found.");

    }



    hope that helps

    Mark



  • IndianTCNoob's avatar
    IndianTCNoob
    Occasional Contributor
    You can also try GetFolderInfo. It returns the aqFolderInfo object containing detailed information about the specified folder, including its subfolders and files. For example, you can post folder contents to the test execution log:





    //...



        path = ... //specify path to the folder here

        folder = aqFileSystem.GetFolderInfo(path)

        logfolder = Log.CreateFolder(folder.Name)

        Log.PushLogFolder(logfolder)

        for (var i = 0;i < (folder.SubFolders.count);i++)

          {

            Log.CreateFolder(folder.SubFolders.Item(i).Name, "Folder")

          }

       for (i = 0;i < (folder.Files.count);i++)

          {

            Log.Message(folder.Files.Item(i).Name, "File")

          }  



    //...



  • Alina's avatar
    Alina
    Occasional Contributor
    Thanks Mark and TC Guy,

    TC Guy, could you please post the VB equivelent of the code you posted, my project is in vbscript its not recognising any of the curly brackets etc..

    it would be a great help !



    Thank you!!!!
  • Alina's avatar
    Alina
    Occasional Contributor
    TC Guy - Thanks thats worked exactly how I wanted it to!



    Cheers,