4

I have tried this, but it didn't satisfy my request at all. I write a new one:

var file_system;
var fs_root;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 1024*1024, onInitFs, request_FS_fail);

function onInitFs(fs) {
  file_system= fs;
  fs_root= file_system.root;
  alert("ini fs");
  create_Directory();
  alert("ini fs done.");
}
var string_array;
var main_dir= "story_repository/"+ User_Editime;
string_array= new Array("story_repository/",main_dir, main_dir+"/rec", main_dir+"/img","story_repository/"+ User_Name );

function create_Directory(){
  var start= 0;
  var path="";
  while(start < string_array.length) {
    path = string_array[start];
    alert(start+" th created directory " +" is "+ path);
    fs_root.getDirectory(
      path,
      {create: true, exclusive: false},
      function(entry) {
        alert(path +"is created.");
      },
      create_dir_err()
    );
    start++;
  }//while loop
}//create_Directory

function create_dir_err() {
  alert("Recursively create directories error.");
}

function request_FS_fail() {
  alert("Failed to request File System ");
}

Although the directories are created, the it sends me

ErrorCallback:"alert("Recursively create directories error.");"

First, I don't think this code will work since I have tried this, which failed:

window.requestFileSystem(
  LocalFileSystem.PERSISTENT,
  0,
  //request file system success callback.
  function(fileSys) {
    fileSys.root.getDirectory(
      "story_repository/"+ dir_name,
      {create: true, exclusive: false},
      //Create directory story_repository/Stallman_time.
      function(directory) {
        alert("Create directory: "+ "story_repository/"+ dir_name);
        //create dir_name/img/
        fileSys.root.getDirectory {
          "story_repository/"+ dir_name + "/img/",
          {create: true, exclusive: false},
          function(directory) {
            alert("Create a directory: "+ "story_repository/"+ dir_name + "/img/");
            //check.
            //create dir_name/rec/
            fileSys.root.getDirectory {
              "story_repository/"+ dir_name + "/rec/",
              {create: true, exclusive: false},
              function(directory) {
                alert("Create a directory: "+ "story_repository/"+ dir_name + "/rec/");
                //check.
                //Go ahead.
              },
              createError
            }
            //create dir_name/rec/
          },
          createError
        }
        //create dir_name/img
      },
      createError
    );
  },
  //Create directory story_repository/Stallman_time.
  createError());
}

I just repeatedly call fs.root.getDirectory only but it failed. But the first one is almost the same...

  1. What is the problem at all? Why does the first one always gives me the ErrorCallback?
  2. Why can't the second one work?
  3. Does anyone has a better solution? (no ErrorcallBack msg)

ps: I work on Android and PhoneGap 1.7.0.

Community
  • 1
  • 1
Alston
  • 2,085
  • 5
  • 30
  • 49
  • please use try catch in javascript and then let me know if i can help – skhurams Dec 15 '12 at 09:14
  • try this function onSuccess(fileSystem) { console.log(fileSystem.name); } // request the persistent file system window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, onError); – skhurams Dec 15 '12 at 09:18
  • http://docs.phonegap.com/en/2.2.0/cordova_file_file.md.html#DirectoryEntry – skhurams Dec 15 '12 at 09:20
  • @skhurams The 2.2.0 doc still doesn't tell us how to create the nested directories... And I'll try to use onSuccess(filesystem)... later. – Alston Dec 15 '12 at 09:34
  • when u use onsuccess it will tell you what is the error – skhurams Dec 15 '12 at 10:32
  • also use error call back this will give you the error code, function onError(evt) { console.log(evt.target.error.code); } – skhurams Dec 15 '12 at 10:37

3 Answers3

1

I am not able to figure out mistake in your code. I have a library written to do localIO via PhoneGap 2.0. I have abstracted code for your requirements from there. See if it works for 1.7. I have not tested the code after abstraction. You may need to fix error, if any.

  var fsroot = fs.root; // initialize this

  function fileGetDir(path, cb) {
    var fnGetOrCreateDir = function(p, de) {
      var entry = p.shift();
      if (entry) {
        de.getDirectory(entry, {
          create : true
        }, function(dirEntry) {
          fnGetOrCreateDir(p, dirEntry);
        }, fileFSError);
      } 
      else
        if (cb) cb(de);
    };

    if (path) {
      var arPath = path.split("/");
      fnGetOrCreateDir(arPath, fsroot);
    }
    else {
      if (cb) cb(fsroot);
    }
  }

  function fileFSError(e) {
    console.log(e.code);
    try {
      console.log("fileFSError: " + JSON.stringify(e));
    } catch (err) {}
  }

function printSuccess(dirEntry) {
    console.log(dirEntry.fullPath);
}

// Now create your directories like:
var main_dir= "story_repository/"+ User_Editime;
fileGetDir(mainDir + "/rec", printSuccess);
fileGetDir(mainDir + "/img", printSuccess);
fileGetDir("story_repository/"+ User_Name, printSuccess);
closure
  • 7,412
  • 1
  • 23
  • 23
  • I'll check it out later. I am currently interrupted by some other bothering issues... Sorry. – Alston Dec 15 '12 at 11:09
  • Your example here only create a directory "story_repository/" + User_Editime + "/rec". But what I need is to successively create lots of directories: "story_repository/"+ dir_name + "/img/", "story_repository/"+ dir_name + "/rec/", "XXX", at any path. – Alston Dec 17 '12 at 05:33
  • Why don't you call it for each directory? – closure Dec 17 '12 at 05:35
1

There is a simple file manager for cordova-phoengap:

https://github.com/torrmal/cordova-simplefilemanagement

You can recursively create directories:

//CREATE A DIRECTORY RECURSIVELY

var a = new DirManager(); // Initialize a Folder manager
a.create_r('folder_a/folder_b',Log('created successfully'));
Jorge Torres
  • 271
  • 3
  • 2
-1

If the first directory off the root "story_repository" does not exist your first call to getDirectory will call the error callback because the call to create dir_name in a non-existent directory will error out on the native side.

Does "story_repository" exist?

Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74
  • Yes it does exist, I tried two method: one is to create "story_repository" manually and the second one is this example: I create the directory by giving the first element in the string_array. string_array= new Array("story_repository/",main_dir, main_dir+"/rec", main_dir+"/img","story_repository/"+ User_Name ); story_repository should be created first,and it did. However, the errorCallback still occurs. And the successfulCallback happens, too... – Alston Dec 17 '12 at 05:41