递归读取目录中的所有文件 - FileSystem API

时间:2014-08-27 13:04:11

标签: javascript html5 html5-filesystem

我越来越混淆如何递归地读取文件列表。 假设我的filesytem api根目录中有3个文本文件

  • text1.txt
  • text2.txt
  • text3.txt

我的目标是逐个读取每个文本文件,然后将每个文件中的所有条目连接成一个字符串,但我目前正在丢失 如何在Javascript FileSystem API中执行此操作。

window.requestFileSystem(window.TEMPORARY, 1024*1024, onInitFs, errorHandler);
function onInitFs(fs) {
  var dirReader = fs.root.createReader();
  var entries = [];

  // Call the reader.readEntries() until no more results are returned.
  var readEntries = function() {
     dirReader.readEntries (function(results) {
      if (!results.length) {
        readAllFiles(results);
      } else {
        entries = entries.concat(toArray(results));
        readEntries();
      }
    }, errorHandler);
  };
  readEntries(); // Start reading dirs.
}

function readAllFiles(entries){
    //Loop thru all the files and read each entries
}

我已经看过如何读取一个文本文件,但我不知道如何实现所有文件的读取并连接值。 它们都实现了回调函数,所以我对如何处理它感到困惑。有什么要点吗?

我实际上已将所有作品都放在http://www.html5rocks.com/en/tutorials/file/filesystem

更新2 按@Johan 我实际上改变了我的代码以使用回调

 window.requestFileSystem(window.TEMPORARY, 1024*1024, onInitFs, errorHandler);
    function onInitFs(fs) {
      var dirReader = fs.root.createReader();
      var entries = [];

      // Call the reader.readEntries() until no more results are returned.
      var readEntries = function() {
         dirReader.readEntries (function(results) {
          if (!results.length) {
            readAllFiles(results, concatMessages);
          } else {
            entries = entries.concat(toArray(results));
            readEntries();
          }
        }, errorHandler);
      };
      readEntries(); // Start reading dirs.
    }


    var concatMessage = '';

    function concatMessages(message){
        concatMessage += message;
    }

    function readAllFiles(logs, callBack) { 
        logs.forEach(function(entry, iCtr) {
            var message;

            entry.file(function(file) {
                var reader = new FileReader();
                reader.onloadend = function(e) {
                    //message += this.result;
                    if(callBack)
                        callBack('==================' + iCtr + '==========================');
                        callBack(this.result);
                };
                reader.readAsText(file); // Read the file as plaintext.
            }, function(err) {
                console.log(err);
            });

        });

    }

我唯一的问题是,回调函数不是顺序的。 它首先读取text2.txt然后是text3.txt然后是text1.txt,所以最终结果不是顺序的,这不是我想要做的。还有其他提示吗?

1 个答案:

答案 0 :(得分:0)

强烈建议您考虑使用类似caolan的async库来完成此任务。

您可以这样做:

async.each(openFiles, function(file, callback) {

    // Perform operation on file here.
    console.log('Processing file ' + file);
    callback();
}, function(err) {
    // if any of the file processing produced an error, err would equal that error
    if (err) {
        // One of the iterations produced an error.
        // All processing will now stop.
        console.log('A file failed to process');
    } else {
        // do your concatenation here
    }
});
相关问题