如何使用javascript通过文件系统的目录和文件迭代? [英] How to iterate through file system directories and files using javascript?

查看:83
本文介绍了如何使用javascript通过文件系统的目录和文件迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用JavaScript编写将与PhoneGap的可以用来做一个Android应用程序的应用程序。我使用PhoneGap的文件API来读取目录和文件。有关code如下所示:

I'm using Javascript to write an application that will be used with Phonegap to make an Android application. I'm using the Phonegap File API to read directories and files. The relevant code is shown below:

document.addEventListener("deviceready", onDeviceReady, false);

// PhoneGap is ready
//
function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
}

function onFileSystemSuccess(fileSystem) {
    fileSystem.root.getDirectory("/sdcard", {create: false, exclusive: false}, getDirSuccess, fail);
}

function getDirSuccess(dirEntry) {
    // Get a directory reader
    var directoryReader = dirEntry.createReader();

    // Get a list of all the entries in the directory
    directoryReader.readEntries(readerSuccess,fail);
}

var numDirs = 0;
var numFiles = 0;

function readerSuccess(entries) {
    var i;
    for (i=0; i<entries.length; i++) 
    {
        if(entries[i].isFile === true)
        {
        numFiles++;
        entries[i].file(fileSuccess,fail);
        }
        else if (entries[i].isDirectory === true)
        {
        numDirs++;
        getDirSuccess(entries[i]);
        }
    }
}

所以,截至目前,该程序工作正常。读者会读/ SD卡directory..if遇到一个文件,它会调用fileSuccess(我已经排除在code为简洁起见),如果它遇到另一个目录,它会调用getDirSuccess的内容再次。我的问题是:我怎么能知道当整个/ SD卡目录是只读?我想不出完成此不通过/ SD卡目录会超过一次的好方法。任何想法是AP preciated,并感谢你提前!

So as of now, the program works fine. The reader will read the contents of the /sdcard directory..if it encounters a file, it will call fileSuccess (which I've excluded in the code for brevity), and if it encounters another directory, it will call getDirSuccess again. My question is this: How can I know when the entire /sdcard directory is read? I can't think of a good way of accomplishing this without going through the /sdcard directory more than one time. Any ideas are appreciated, and thank you in advance!

推荐答案

+1一个很好的问题,因为我必须这样做,反正我自己。我会用旧的setTimeout的伎俩。一旦取消不会再发生,你知道你做,可以解雇你的事件,但只是确保其只发射一次。

+1 on a good question since I have to do this anyway myself. I would use the old setTimeout trick. Once the cancel doesn't occur anymore, you know you are done and can fire your event, but just ensure its only fired once.

下面就是我的意思,我已经命名了长期的变量只是为了更可读(不是我的风格)...

Here's what I mean and I've named the variables long simply to be more readable (not my style)...

// create timeout var outside your "readerSuccess" function scope
var readerTimeout = null, millisecondsBetweenReadSuccess = 100;

function readerSuccess(entries) {
    var i = 0, len = entries.length;
    for (; i < len; i++) {
        if (entries[i].isFile) {
            numFiles++;
            entries[i].file(fileSuccess,fail);
        } else if (entries[i].isDirectory) {
            numDirs++;
            getDirSuccess(entries[i]);
        }
        if (readerTimeout) {
            window.clearTimeout(readerTimeout);
        }
    }
    if (readerTimeout) {
        window.clearTimeout(readerTimeout);
    }
    readerTimeout = window.setTimeout(weAreDone, millisecondsBetweenReadSuccess);
}

// additional event to call when totally done
function weAreDone() {
   // do something
}

因此​​,在这种逻辑是你保持距离,你是通过阅读的东西被称为取消weAreDone功能。不知道这是最好的方法或更有效,但它不会导致多个循环给予适当的millisecondsBetweenReadSuccess。

So the logic in this is you keep cancelling the "weAreDone" function from being called as you are reading through stuff. Not sure if this is the best way or more efficient but it would not result in more than one loop given the appropriate "millisecondsBetweenReadSuccess".

这篇关于如何使用javascript通过文件系统的目录和文件迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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