Skip to Content
Using the HTML5 Filesystem API
book

Using the HTML5 Filesystem API

by Eric Bidelman
July 2011
Intermediate to advanced
72 pages
1h 13m
English
O'Reilly Media, Inc.
Content preview from Using the HTML5 Filesystem API

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, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

HTML5 Geolocation

HTML5 Geolocation

Anthony T. Holdener
HTML5 Hacks

HTML5 Hacks

Jesse Cravens, Jeff Burtoft
Grid Layout in CSS

Grid Layout in CSS

Eric A. Meyer

Publisher Resources

ISBN: 9781449311384Errata Page