在JavaScript中读取具有动态文件名的文件 [英] Read a file with dynamic file name in JavaScript

查看:81
本文介绍了在JavaScript中读取具有动态文件名的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在JavaScript中读取文件名为Sometext_ddmmyy.xls的文件? 文件名每天更改一次,但日期ddmmyy是该周或之前的随机日期. 但是文件的路径永远不会改变. 您可以帮助编写JavaScript编码来读取文件吗?

Is it possible to read a file with file name as Sometext_ddmmyy.xls in JavaScript? The file name changes daily but date ddmmyy are random date in that week or previous. But the path of the file never changes. Can you help to code in JavaScript to read the file?

代码:

var filename =  /^path\\sometext_.*.xls$/;
....
var neobjectexcel = new ActiveXObject("Excel.Application");
var sheet = neojectexcel.Workbooks.Open(filename).ActiveSheet;
...

我使用了正则表达式,但得到的提示是抱歉,我们找不到/^path\sometext_.*.xls$/是否有可能被移动,重命名或删除了?"错误!

I used Regex but I am getting "Sorry, we couldn't find /^path\sometext_.*.xls$/ Is it possible it was moved, renamed or deleted?" Error!

即使正则表达式/^path\\sometext_\d\d\d\d\d\d.xls$/也给出相同的错误.

Even the regex /^path\\sometext_\d\d\d\d\d\d.xls$/ is giving the same error.

也尝试用\ d +代替\ d \ d \ d \ d \ d \ d不起作用. 请帮忙!

Also tried \d+ instead of \d\d\d\d\d\d does not works. Please help!

更新:该文件将与其他类似名称的文件(例如sometext_ddmmyy.xls randomtext_ddmmyy.xls randomothertext_ddmmyy.xls)一起位于服务器上的文件夹中,并将通过同一服务器上的脚本进行访问.

Update: The file will be in a folder on a server along with other files of similar names like sometext_ddmmyy.xls randomtext_ddmmyy.xls randomothertext_ddmmyy.xls and will be accessed with script on the same server.

推荐答案

使用FileSystemObject,您可以使用RegExp过滤文件名,并找到添加到文件夹的最新过滤文件,如下所示:

With FileSystemObject you can filter a filename with a RegExp and find the latest filtered file added to a folder like so:

function getLatestFile (path, filter) {
    var fileIO = new ActiveXObject('Scripting.FileSystemObject'),
        folder = fileIO.GetFolder(path), // Get a folder object
        files = new Enumerator(folder.files), // Create files collection
        latest = 0,
        file, date, isTargetFile, temp, target;
    while (!files.atEnd()) { // Iterate files collection
        file = files.item(); // Get a file from the collection
        temp = new String(file); // Convert a file object to string
        isTargetFile = filter.test(temp); // Filter the filename
        date = new Date(file.DateCreated).getTime(); // Get the creation date of the file
        if (isTargetFile && date > latest) { // Check if this file passed the filter and is newer than the previous filter-passed file
            target = temp;
            latest = date;
        }
        files.moveNext(); // Continue iteration
    }
    return target;
}

针对您的案例的示例通话为:

An example call for your case would be:

var filename = getLatestFile(path_to_folder, /sometext_\d{6}\.xls$/);

参数:path =搜索文件所在文件夹的路径. filter =用于过滤文件名的RegExp对象(文字或构造).创建RegExp时,请注意,虽然可以创建RegExp仅过滤文件名,但转换后的文件对象(temp)使用反斜杠作为文件夹分隔符.如果该函数返回undefined,则没有任何文件名与RegExp匹配.

Arguments: path = the path to the folder where to search files. filter = a RegExp object (literal or constructed) to filter the filename. When creating the RegExp, note that the converted file object (temp) uses a backslash as a folder separator, though the RegExp can be created to filter the filename only. If the function returns undefined, none of the filenames has matched the RegExp.

这篇关于在JavaScript中读取具有动态文件名的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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