Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

If I use this when getting folder or files, I avoid write if() later:

var fileList = sourceFolder.getFiles(function(f) { return f instanceof File; });

The same when getting only folders:

var fileList = sourceFolder.getFiles(function(f) { return f instanceof Folder; });

However it is recommended to use the getFiles function as less as possible because the code will run faster.

I also use RegExp objects to pick only specific sub-folders in a folder.

For example, if I set a regular expression like the 'regthis' var as below. The folders collected with 'getFiles' will be the one that its name:
A) Must have '12345678' at the end or a uppercase letter before '12345678';
B) Must also have one of the 2 characters ('_' or a 'c') before A;
C) Must have 1 lowercase letter 'a-v' before B+A;
D) Must not have 'x' or 'y' or 'z' before C+B+A;

var ID_ = '12345678';
var regthis = new RegExp( '([^x-z]{1}[a-v]{1}[_|c]{1})([A-Z]?'+ID_+'?)$','i');
var sameIDfolder = Folder(myFolder).getFiles(regthis);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top