JavaScript获取所有文件减去某些文件扩展名 [英] Javascript get all files minus certain file extentions

查看:96
本文介绍了JavaScript获取所有文件减去某些文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,可以返回文件夹中包含的所有文件.但是,其中有些文件类型我不希望脚本执行任何操作.我只希望它从字面上跳过它,就好像它不在那儿一样,只处理其他文件类型.

I have a script that returns all the files contained within a folder. However, there are some file types in there that I do not want my script to do anything with. I just want it to literally skip over it as if it wasn't there and only deal with the other file types.

我该如何实现?

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

So far this is how I'm getting all the files contained within a folder:

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  
    }

 }

这不是用于网络目的,我​​应该尽快添加.

This is not used for web purposes I should hasten to add.

编辑

以上是完成代码后,我必须将所有图像文件保存在一个文件夹中,并在用户选择了要使用的文件夹后将它们带入Photoshop.目前,当有一个我想忽略的单一类型时,它正在引入文件夹中的每个单一图像.

Above is the complete code I have to get all the image files in a folder and bring them into photoshop once the user has selected the folder they want to use. At the moment it is bringing in every single image in the folder when there is a single type I want it to ignore.

推荐答案

假设fileList只是一个字符串数组,您可以按照以下方式进行操作:

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
}

如果txthtmlgif是要跳过的文件扩展名,则可以通过使用|

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

这篇关于JavaScript获取所有文件减去某些文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆