-
I'm using the below code to read in files from a folder. Is there a way to get the file's age (or specify it in the fetch) so as to not get a file that is currently being written?
|
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 7 replies
-
https://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/filefilter/package-summary.html shows how to combine filters using an |
Beta Was this translation helpful? Give feedback.
-
I’m new…In my opinion…all you need to do is create a function that checks for the age of the file. Then just use simple if/else statements to adjust which files you want…this was most likely useless…but further explanation would be helpful. |
Beta Was this translation helpful? Give feedback.
-
The original poster was likely looking for help in the java methods to check the file age which @jon posted. |
Beta Was this translation helpful? Give feedback.
-
Is there a way, using the date object, to set to a specific date and time? I'm trying to use the AgeFileFilter
|
Beta Was this translation helpful? Give feedback.
-
Well, I waited until this time to make sure I had some files there were older than 24 hours. But this code will not return any files when I add in the age filter.
Here are the files: Am I thinking about this incorrectly (current time zone is ET)? |
Beta Was this translation helpful? Give feedback.
-
When I try just using the age filter I get this error in Mirth: Can't find method java.io.File.list(number). I think my code if following the example in https://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/filefilter/AgeFileFilter.html |
Beta Was this translation helpful? Give feedback.
-
This one get newer files, just change the check: function getFilesModifiedWithinHours(directoryPath, hours) {
var fileFilter = new java.io.FileFilter({
accept: function(file) {
var hoursInMillis = hours * 60 * 60 * 1000;
return file.lastModified() > Date.now() - hoursInMillis;
}
});
var directory = new java.io.File(directoryPath);
if (!directory.exists() || !directory.isDirectory()) {
throw "Path provided does not exist or is not a directory: " + directoryPath;
}
var files = directory.listFiles(fileFilter);
var filePaths = [];
for (var i = 0; i < files.length; i++) {
filePaths.push(files[i].getAbsolutePath());
}
return filePaths;
}
var directoryPath = '/home/mirth';
var hours = 24;
var recentFiles = getFilesModifiedWithinHours(directoryPath, hours);
// Print all recent file paths
for (var i = 0; i < recentFiles.length; i++) {
logger.info(recentFiles[i]);
} |
Beta Was this translation helpful? Give feedback.
-
Seriously folks, ask chatGPT this stuff. |
Beta Was this translation helpful? Give feedback.
-
I will end with this: I asked chatGPT to comment the function. /**
* This function returns a list of files from a given directory, filtered by modification time and file extension.
*
* @param {string} directoryPath - The path of the directory to scan for files.
* @param {number} hours - Only files modified within these many hours will be included.
* @param {string} fileExtension - The extension of the files to include. This check is case-insensitive.
* @return {string[]} An array of file paths for files that match the criteria.
*/
function getFilesModifiedWithinHours(directoryPath, hours, fileExtension) {
// Define a custom FileFilter
var fileFilter = new java.io.FileFilter({
accept: function(file) {
// Calculate the time in milliseconds for the given number of hours
var hoursInMillis = hours * 60 * 60 * 1000;
// Get the extension of the current file
var extension = file.getName().substring(file.getName().lastIndexOf(".") + 1);
// Check that the file is a file (not a directory), it was modified within the given time period, and its extension matches the given extension
return file.isFile() && file.lastModified() > Date.now() - hoursInMillis && extension.toLowerCase() === fileExtension.toLowerCase();
}
});
// Create a File object for the directory
var directory = new java.io.File(directoryPath);
// If the directory does not exist or is not a directory, throw an error
if (!directory.exists() || !directory.isDirectory()) {
throw "Path provided does not exist or is not a directory: " + directoryPath;
}
// Get a list of files in the directory that match the file filter
var files = directory.listFiles(fileFilter);
var filePaths = [];
// Loop through the list of files and add their absolute paths to the filePaths array
for (var i = 0; i < files.length; i++) {
filePaths.push(files[i].getAbsolutePath());
}
// Return the list of file paths
return filePaths;
} |
Beta Was this translation helpful? Give feedback.
I will end with this: I asked chatGPT to comment the function.