Forum Discussion
//Goes thru a folder and finds the last modified file with a sertain filename
//Param FolderPath String: The Path to the folder to look in
//Param FilNameContains RegularExpression: The file name to match (write the regular expr // if you want to find all)
//Return String with the full path to the last modified file
//By Bjorn Kruuse-Meyer 05.12.2011
function FindLastModifiedFileInFolder(FolderPath,FileNameContains)
{
var FolderObject = aqFileSystem.GetFolderInfo(FolderPath); //The folder to look in
var FileItems = FolderObject.Files //Collection of all the files in the folder
var FileDateModified = new Array(); //Array of all the relevant date modified info for the files
var FileNumber = new Array(); //The absolute index number for the files sorted
var LatestFileNumber; //The index to the latest file modified
var NewestTime; //Placeholder for the Newest time when searching
//Builds up the arrays with the filnames containing FileNameContains reg exp
for (var i=0; i < FileItems.Count; i++)
{
if(FileItems.Item(i).Name.search(FileNameContains) > -1)
{
FileNumber.push(i);
FileDateModified.push(FileItems.Item(i).DateLastModified);
}
}
//Finds the most resent modified file
NewestTime = FileDateModified[0];
LatestFileNumber = FileNumber[0];
for (var i=0; i < FileNumber.length; i++)
{
if(aqDateTime.Compare(NewestTime, FileDateModified) < 0)
{
NewestTime = FileDateModified;
LatestFileNumber = FileNumber;
}
}
return FileItems.Item(LatestFileNumber).Path;
}
- swapnaliuk9 years agoOccasional Contributor
We had to modify the mothod below to get LatestModifiedFile in the specified folder.
function FindLastModifiedFileInFolder(FolderPath, FileNameContains)
{
var FolderObject = aqFileSystem.GetFolderInfo(FolderPath); //The folder to look in
var FileItems = FolderObject.Files //Collection of all the files in the folder
var FileDateModified = new Array(); //Array of all the relevant date modified info for the files
var FileNumber = new Array(); //The absolute index number for the files sorted
var LatestFileNumber; //The index to the latest file modified
var NewestTime; //Placeholder for the Newest time when searching
//Builds up the arrays with the filnames containing FileNameContains reg expfor (var i=0; i < FileItems.Count; i++)
{
if(FileItems.Item(i).Name.search(FileNameContains) > -1)
{
FileNumber.push(i);
FileDateModified.push(FileItems.Item(i).DateLastModified);
}
}//Finds the most resent modified file
NewestTime = FileDateModified[0];
LatestFileNumber = FileNumber[0];for (var i=0; i < FileNumber.length; i++)
{
// Log.Message(i);
// Log.Message(NewestTime);
if(aqDateTime.Compare(NewestTime, FileDateModified[i]) < 0)
{
NewestTime = FileDateModified[i];
LatestFileNumber = FileNumber[i];
}
}return FileItems.Item(LatestFileNumber).Path;
}
Related Content
- 2 years agonastester
- 5 years agomortenb123
Recent Discussions
- 6 hours agoSvetaG