Forum Discussion

indraniria's avatar
indraniria
Contributor
2 years ago

How to find a file containing a particular substring from a list of files in a folder?

I have a list of files inside a folder. From these files , I have to find a file that is containing a particular transaction (a substring). How do I find that particular file using groovy scripting?

1 Reply

  • Kitt's avatar
    Kitt
    Regular Contributor

    Groovy scripts have full access to the object model in ReadyAPI, not TestComplete. You might want to try that community forum [here] for Groovy specific scripts.

     

    You can accomplish this in TestComplete by using the aqFileSystem object and searching through all files within a folder. Below is a working example which counts the number of files in the folder (for reference) then iterates through every file searching for the specified 'aSubString' and prints the name of the file to the log when the 'aSubString' is found.

     

    function findStringInFolderFiles() {
      var file;
      var folderPath = "C:\\your\\folder\\path";
      var folderObject = aqFileSystem.GetFolderInfo(folderPath);
      var folderItems = folderObject.Files;
      var aSubString = "Replace"; // string to search for                       
    
      Log.Message("Found " + folderItems.Count + " files in " + folderPath);
    
      while (folderItems.HasNext()) {
        file = folderItems.Next();
        var fullPath = folderPath + "\\" + file.Name;
        var str = aqFile.ReadWholeTextFile(fullPath, aqFile.ctANSI);
        var res = aqString.Find(str, aSubString);
        if (!equal(res, -1)) {
          Log.Checkpoint(file.Name);
        }
        else {
          Log.Message("NOT FOUND: There are no occurances of " + aSubString + " in " + fullPath);
        }
      }
    }

     

    You should be able to apply the same logic to your Groovy scripts. These references may provide a good starting point: [read files w/Groovy] and [search files w/Groovy].