getFiles() not folders

Ghoul Fool picture Ghoul Fool · Feb 7, 2014 · Viewed 15.3k times · Source

I've got several files in a temporary folder. I can load them into Photoshop with scripting the following:

  var sourceFolder = Folder("C:\\temp");
  if (sourceFolder != null)
  {
     var fileList = sourceFolder.getFiles();
  }

This is all good, but how do I ignore directories (such as C:\temp\waffles) that might also be in there also.

I understand that I could do a check for valid image extensions and then add them to a new filelist array and then load that. I don't think the search option TopDirectoryOnly is valid here.

Answer

user268911 picture user268911 · Feb 12, 2014

Since getFiles() "Returns an array of File and Folder objects" You will need to iterate over each of the objects returned and test to see what kind of object it is. From Adobe's Creative Suite 5 Javacript Tools Guide

There are several ways to distinguish between a File and a Folder object. For example:
if (f instanceof File)...
if (typeof f.open == "undefined")... //Folders do not open.