Reputation: 13
I'm trying to set a variable from a CSV filename, specifically the file with the last date modified. The CSV files are based on data from my tests, so that file will be constantly changing. I've tried this code but I can't seem to get it to save as a variable.
public static File getLatestFilefromDir(String dirPath) {
File dir = new File(dirPath);
File[] files = dir.listFiles();
if (files == null || files.length == 0) {
return null;
}
File lastModifiedFile = files[0];
for (int i = 1; i < files.length; i++) {
if (lastModifiedFile.lastModified() < files[i].lastModified()) {
lastModifiedFile = files[i];
}
}
return lastModifiedFile;
}
String fileName = lastModifiedFile;
vars.put("FILENAME", fileName);
Thank you for your help.
Upvotes: 0
Views: 300
Reputation: 58772
The fix for your code is converting File to its name
String fileName = getLatestFilefromDir("...").getName();
Upvotes: 0
Reputation: 168002
I would recommend using the following Groovy code to get the name of the newest file in the specified folder and save the result into the FILENAME
JMeter Variable:
vars.put("FILENAME", new File('/path/to/the/folder/with/CSV/files').listFiles()?.sort { -it.lastModified() }?.head().getName())
You can use this code with any of JSR223 Test Elements
See Apache Groovy - Why and How You Should Use It article for more details on using Groovy scripting in JMeter tests.
Upvotes: 1