O'Reilly logo

Using the HTML5 Filesystem API by Eric Bidelman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 6. Copying, Renaming, and Moving Entries

FileEntry and DirectoryEntry share common API calls for common tasks such as copying, renaming, and moving an entry. This chapter covers these operations in a generic way, but the concepts can be applied to either entry type.

Copying a File or Directory

Copying a file or folder to a different location on the filesystem is possible with copyTo(). By design, copying a folder is recursive, while files are simply duplicated:

entry.copyTo(parentDirEntry, opt_newName, opt_successCallback, opt_errorCallback);

The first parameter is a DirectoryEntry, the parent folder to move the entry into. The second argument is an optional new name to give the copied entry. The third and fourth parameters are our usual suspects, a success and error callback.

Warning

Attempting to copy an entry in an illegal way can result in an error. Two common errors are trying to copy an entry inside itself and trying to copy an entry in the same folder without specifying a new name.

As an example usage, the following snippet copies the file me.png from one directory to another.

Example 6-1. Copying a file to a different folder
/** * Copies a file to a different folder. * * @param {DirectoryEntry} cwd The current working directory. * @param {string} srcFile A relative path from the cwd to a file. * @param {string} dest A relative path of the destination directory. */ function copyFile(cwd, srcFile, dest) { cwd.getFile(srcFile, {}, function(fileEntry) { cwd.getDirectory(dest, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required