ディレクトリを操作する

ディレクトリ操作の応用例:複雑なディレクトリ構造を作成する
ブラウザのコンソールを開き、以下のコードを実行して動作を確認してください

※Chromeからローカル環境のファイルを開き、コンソールから確認する場合、 "--allow-file-access-from-files" オプション付きのChromeを実行する必要があります。

window.webkitRequestFileSystem(TEMPORARY, 1024*1024, function(fs) {
  console.log('file system: ' + fs.name);

  mkdirp(fs.root, 'MyDirectory/dir1/dir2'.split('/'), function(error, dirEntries) {
    if (error) return console.log(error);
    console.log('OK', dirEntries);
 });
},
function(error) {
  console.log(error);
});
    

このページに埋め込まれているコード

function mkdirp(current, paths, callback, dirEntries) {
  if (!dirEntries) dirEntries = [];

  current.getDirectory(paths[0], { create: true }, function(dirEntry) {
    if (paths.length !== 0) {
      dirEntries.push(dirEntry);
      mkdirp(dirEntry, paths.slice(1), callback, dirEntries);
    } else {
      callback(null, dirEntries);
    }
  }, function(error) {
    callback(error);
 });
}