0
var fileFolderArray = Folder( "~/Downloads/" ).getFiles();

How to sort by files created time when use getFiles function ? thanks:)

Matoyo
  • 3
  • 2
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 20 '22 at 13:48
  • you could sort your array manually by inspecting the file date for each item – Psi Aug 20 '22 at 13:50

1 Answers1

1

You'll be able to get the created time with .created notation. You can't use filter or more complex JavaScript functions as Photoshop relies on earlier version of ECMAScript :(

This will work for ya:

myFolder = "C:\\temp";

var sorted = sort_files_by_creation_date(myFolder);
var msg ="";
for (var i = 0;  i < sorted.length; i++)
{
   msg+= sorted[i][0] + "\n";
}

alert(msg);

function sort_files_by_creation_date (afolder)
{

   var fileFolderArray = Folder(afolder).getFiles();

   var dates = [];
   for (var i = 0;  i < fileFolderArray.length; i++)
   {
      var fileCreationDate = fileFolderArray[i].created.toString();
      // var fileCreationDate = fileFolderArray[i].modified.toString();
      dates.push([fileCreationDate, fileFolderArray[i]]);
   } 

   //dates.sort()
   return dates.sort(sortFunction);  
}

function sortFunction(a, b)
{
    if (a[0] === b[0])
    {
      return 0;
    }
    else
    {
      return (a[0] < b[0]) ? -1 : 1;
    }
}

Sort function taken from here.

Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125