Samsung Electronics logo

Creating and Deleting Files and Directories

Learning how to create and delete files and directories is a basic data management skill:

  1. To create a file in the current directory, use the createFile() method of the File interface:

    var newFile = dir.createFile("newFilePath");
  2. To create a directory within the file system, use the createDirectory() method.

    The directory (and any sub-directories defined in the method parameter) is created relative to the current directory where the operation is performed on.

    var newDir = dir.createDirectory("newDir");
    var anotherNewDir = dir.createDirectory("newDir1/subNewDir1");
  3. To delete a file, use the deleteFile() method:

    for (var i = 0; i < files.length; i++) 
       if (files[i].isDirectory == false)
          documentsDir.deleteFile(files[i].fullPath, onSuccess);
  4. To delete a directory, use the deleteDirectory() method.

    The second parameter defines whether the deletion is performed recursively for the sub-directories as well. If the parameter is set to false, the directory is deleted only if it is empty.

          documentsDir.deleteDirectory(files[i].fullPath, false, onSuccess);