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 generated is dynamic and has a timestamp at the end [with format - Output_2022-07-21.csv]. I am trying to use regular expression in the file path as shown below but it fails. Can you please help?
function Test1()
{
Files.BaseFile.Check("C:\\Exported_Data\\Output_.*.csv");
}
Also if there is any other way to read such type of file please advise.
Thanks in advance!
Sravani
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."); } }
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"); }