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.
/** * 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, ...
Get Using the HTML5 Filesystem API now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.