How to get only file name without its full path

Hello There,

I have a keyword which helps me find the last modified file in a specific location:
private File getLatestFileFromDirectory(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;

}

my scenario is i click on a button to download a file, i added a 5 sec delay for it to get downloaded and then i need to get the last file downloaded/Modified.

This is the script + the location of the file:
WebUI.click(findTestObject(‚ÄúMon releve bancaire/4.0 Export mes operations/btnTelechargerLesOperations‚ÄĚ))

WebUI.delay(5)
Path absolutePath = Paths.get(RunConfiguration.getProjectDir(), ‚Äú/Data Files‚ÄĚ);

LastFileExported=CustomKeywords.‚Äėessentials.getLatestFileFromDirectory.getLatestFileFromDirectory‚Äô(absolutePath.toString())
KeywordUtil.logInfo(‚ÄúLast file received‚ÄĚ +LastFileExported)

My problem is that it is outputting the file with its full path I only need the file to check that it actually got downloaded and later on i will delete it by storing it on some variable. The file downloaded has a dynamic name.

This is what i am getting in the console:
Last file received/xxx/xxx/xxxx/xxx/Data Files/_08-02-21_14_48_19.csv

I need only _08-02-21_14_48_19.csv
What can i do to get only the file name?

Thank you

Try…

http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FilenameUtils.html

1 Like