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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

HTML5 Geolocation

HTML5 Geolocation

Anthony T. Holdener
HTML5 Hacks

HTML5 Hacks

Jesse Cravens, Jeff Burtoft
HTML5 Unleashed

HTML5 Unleashed

Simon Sarris

Publisher Resources

ISBN: 9781449311384Errata Page