Forum Discussion

Bjorn_Kruuse-Me's avatar
Bjorn_Kruuse-Me
Occasional Contributor
13 years ago

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

  • Bjorn_Kruuse-Me's avatar
    Bjorn_Kruuse-Me
    Occasional Contributor
    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;   

    }    
    • swapnaliuk's avatar
      swapnaliuk
      Occasional 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 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;
      }

  • irina_lukina's avatar
    irina_lukina
    Super Contributor

    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

      }

    }

  • gentlesea's avatar
    gentlesea
    Frequent Contributor
    Thank you. Works for me. I used ".*" as regular expression.





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