1

我有一个脚本,它返回文件夹中包含的所有文件。但是,其中有一些文件类型我不希望我的脚本做任何事情。我只是希望它从字面上跳过它,就好像它不存在一样,只处理其他文件类型。

我怎样才能做到这一点?

到目前为止,这就是我获取文件夹中包含的所有文件的方式:

var samplesFolder = Folder(Path)
//Get the files 
var fileList = samplesFolder.getFiles()
//Creat Array to hold names
var renderTypes = new Array();
//Parse Initial name to get similar render elements
var beautyRender = fileList[0].name
beautyRender = beautyRender.substr(0, beautyRender.length-4)


//Get the render elements with a similar name
for (var i = 0; i < fileList.length; i++)
{
    if(fileList[i].name.substring(0,beautyRender.length) === beautyRender)
    {
            renderTypes[i] = fileList[i].name  
    }

 }

这不用于网络目的,我​​应该赶紧添加。

编辑

上面是完整的代码,一旦用户选择了他们想要使用的文件夹,我必须将所有图像文件放在一个文件夹中并将它们带入 Photoshop。目前,当我希望它忽略一个类型时,它会引入文件夹中的每一个图像。

4

2 回答 2

2

You can iterate over the list and only collect those with extensions you care about. i see so I'll assume image files only:

var distilledFileList = [];
for (var i = 0; i < fileList.length; i++){
  if (/\.(?:jpe?g|png|gif|psd)$/i.test(fileList[i].name)){
    distilledFileList.push(fileList[i]);
  }
}

Now distilledFileList contains only *.jpg, *.jpeg, *.png, *.gif, and *.psd files.

if you want an easier (more readable) way to check extensions (maybe you're not as fluent as regular expressions):

// fileList = ....

// setup an array of bad extensions here:
var bad = ['txt', 'log', 'db'],
    // holds new list of files that are acceptable
    distilledFileList = [];

// iterate over entire list
for (var i = 0; i < fileList.length; i++){
  // grab the file extenion (if one exists)
  var m = fileList[i].name.match(/\.([^\.]+)$/);
  // if there is an extenions, make sure it's now in the
  // 'bad' list:
  if (m && bad.indexOf(m[1].toLowerCase()) != -1){
    // it's safe, so add it to the distilled list
    distilledFileList.push(fileList[is]);
  }
}
于 2013-10-29T12:18:44.450 回答
1

Assuming fileList is just an array of strings you could do something along the lines of:

for (var i = 0, len = fileList.length; i < len; i++) {
    var filename = fileList[i].name;
    if (filename.match(/\.(txt|html|gif)$/i) !== null) { continue; }
    // Your logic here
}

Where txt, html and gif are file extensions you want to skip over, you can add more by separating them with |

于 2013-10-29T12:23:32.290 回答