Uploading Current Day Files

Hello!

Asked this question sometime last month. I want to upload a file that has today’s date in it where I dont have to manually update the date each day in the test case.

How would I accomplish this? And please answer as if you were talking to a 5-year old, I’m super green when it comes to this stuff. I figured out how to get files uploaded, but they are static and I have to update the absolute path each day.

Please and thank you!

There are third-party Java libraries that are much better equipped to handle a scenario like this (thinking specifically of this), but I can give you a way to do it using the standard Java libs as long as I can assume the following:

1.) The directory that your file(s) exists in is static, even if the filenames will be dynamic.
2.) Within that directory, there will never be any files other than the files you are trying to upload daily.

The idea is to look at the directory and find the file with the latest “Date modified” value:

public File getLatestFile() {
	File directory = new File("path/to/my/files");
	File[] files = directory.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;
}

With this method, you don’t actually care what the name of the file is, you just get the latest one. This is quite fragile (if either of the two assumptions I laid out are violated), and again, there are far better ways of doing this if you don’t mind working with third-party libraries that can do wildcard searches, etc.

1 Like