最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - getFiles() not folders - Stack Overflow

programmeradmin6浏览0评论

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.

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.

Share Improve this question edited Sep 5, 2015 at 16:48 Tunaki 137k46 gold badges366 silver badges437 bronze badges asked Feb 7, 2014 at 15:21 Ghoul FoolGhoul Fool 6,96713 gold badges75 silver badges139 bronze badges 1
  • possible duplicate of Exclude certain file extensions when get files from a directory – Max Commented Feb 7, 2014 at 15:24
Add a ment  | 

2 Answers 2

Reset to default 3

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.

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 remended 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);
发布评论

评论列表(0)

  1. 暂无评论