admin管理员组文章数量:1332107
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
2 Answers
Reset to default 3Since 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);
本文标签: javascriptgetFiles() not foldersStack Overflow
版权声明:本文标题:javascript - getFiles() not folders - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742232734a2437518.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论