Forum Discussion

Sravani93's avatar
Sravani93
Occasional Contributor
2 years ago
Solved

How to read a dynamically generated file using regular expression

Hello,   I want to compare a ".csv" file that's generated during the test to a baseline. I added the baseline to the Files check point using 'byte to byte' comparison. The issue is the file generat...
  • Kitt's avatar
    Kitt
    2 years ago

    Try this:

     

     

     

    function findFiles() {  
      var foundFiles, aFile;
      foundFiles = aqFileSystem.FindFiles("C:\\Exported_Data", "Output*.csv");
      if (!strictEqual(foundFiles, null)) {
        while (foundFiles.HasNext()) {
          aFile = foundFiles.Next();
          Log.Message(aFile.Name);
        }
      }
      else {
        Log.Message("No files were found.");
      }
    }

     

     

     

  • chriscc's avatar
    2 years ago

    Hi Sravani93 

     

    If the directory will only have one file then you could use aqFIleSystem.FindFiles function to get the path to that one file. Something like this:

    var pathToDirectory = "c:\\";
    var searchPattern = "*.txt";
    var files = aqFileSystem.FindFiles(pathToDirectory , searchPattern, false);
    var file = file.Next();
    var path = file.Path;

     

    If there will be more than one file in that directory then you could try a couple of different things. 

    1. Get the list of files before and after running your test. Then compare the lists to find the new file.  

    2. If each test creates a new file then you could just get the most recent file:  The code below comes from this post: Solved: Re: How to Validate recently downloaded output Exc... - SmartBear Community

        function getMostRecentCreatedFile(directoryPath, searchPattern)
        {
          var newestFilePath = "";
          var previousCreationTime = "1/1/1900";
          try
          {
            var files = aqFileSystem.FindFiles(directoryPath, searchPattern, false);
            if (files != null)
            {         
              while(files.HasNext())
              {
                var file = files.Next();
                var creationTime = aqFile.GetCreationTime(file.Path);
                if (aqDateTime.Compare(creationTime, previousCreationTime) == 1)
                {
                  previousCreationTime = creationTime;
                  newestFilePath = file.Path;
                }
              }
            }
          }
          catch(e)
          {
            Log.Error("Exception in getMostRecentCreatedFile", e.descrpition);
          }
          //
          return newestFilePath;
        }
    
        function testGetFile()
        {
          var filePath=getMostRecentCreatedFile("C:\\Users\\herrerae\\Downloads", "*.xlsx");
        }