Ask a Question

Find last changed file using JScript

Bjorn_Kruuse-Me
Occasional Contributor

Find last changed file using JScript

Hi all



I am pretty new to scripting and TC so I need some help.



I want to make a script that finds the last modified file in a folder and returns it's filename.

I'm not sure where to start, so if someone would point me in the right direction...?



We are using JScript, so if someone makes an example please do so in JScript.



Thank you in advance

Bjørn Kruuse-Meyer

6 REPLIES 6
irina_lukina
Super Contributor

RE: Find last changed file using JScript

Hi Bjшrn,


I'm not sure where to start, so if someone would point me in the right direction...?


I think you can start with something like this:



function LastModifiedFile()

{

  var FolderName = "D:\\Test";

  var FolderInfo = aqFileSystem.GetFolderInfo(FolderName);

  var Num = FolderInfo.Files.Count;

  Log.Message("The folder contains " + Num + " files.")

  for (var i=0; i < Num; i++)

  {

      var FileDate = FolderInfo.Files.Item(i).DateLastModified;

      var a = new Array();

      a = FileDate;

      Log.Message(a);

      // Develop your own sorting algorithm to sort this Array

      // When you find an appropriate date (the last one)

      // You can obtain the file name using the following code

      //  FolderInfo.Files.Item(i).Path

  }

}

I’m not a member of the SmartBear Support Team, I’m just helping users in this community. Any views or opinions expressed do not necessarily represent those of SmartBear Software.
Bjorn_Kruuse-Me
Occasional Contributor

RE: Find last changed file using JScript

Hi Irina



A million thanks, exactly what I was looking for.:)



Bjørn

gentlesea
Frequent Contributor

RE: Find last changed file using JScript

Did you finish the method? Please post it here.
Using TestComplete 9.0.2460.7
Bjorn_Kruuse-Me
Occasional Contributor

RE: Find last changed file using JScript

Here is what I did:




//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;   

}    
gentlesea
Frequent Contributor

RE: Find last changed file using JScript

Thank you. Works for me. I used ".*" as regular expression.





var settingsFileToSearchIn = FindLastModifiedFileInFolder(folderName, ".*");

Using TestComplete 9.0.2460.7
swapnaliuk
Occasional Contributor

Re: RE: Find last changed file using JScript

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 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++)
{
// Log.Message(i);
// Log.Message(NewestTime);

if(aqDateTime.Compare(NewestTime, FileDateModified[i]) < 0)
{
NewestTime = FileDateModified[i];
LatestFileNumber = FileNumber[i];
}
}

return FileItems.Item(LatestFileNumber).Path;
}

cancel
Showing results for 
Search instead for 
Did you mean: