Recursively handling files using Dossier

When creating your Titanium Enterprise app, you will often find the need to copy the contents of a directory to another place. Two of the most common examples of this would be: implementing a caching approach and performing lazy, loaded installations. For example, Dossier can be used to create an initial content cache, by copying files from your app's Resources directory into a working directory under Ti.Filesystem.applicationDataDirectory. This would allow for the user to see the initial content while data is being refreshed in the background.

The Dossier CommonJS module provides a cross-platform API for handling these types of folder operations. The next section demonstrates how to install and use the Dossier module within your Titanium Enterprise app.

Getting ready

The Dossier CommonJS module is installed by including the dossier.js file into your project.

Adding the Dossier module into your project

Adding the dossier module to your project is easy. Simply copy the dossier.js file and the SampleData folder into the Resources folder of your Titanium project, as highlighted in the following screenshot. This will install all the files needed for this recipe.

Adding the Dossier module into your project

How to do it…

Once you've added the dossier.js file to your project, you need to use require in order to import the module into your code:

//Create our application namespace
var my = {
  dossier : require('dossier'),
};

Creating sample directories

To demonstrate the copy and move features of Dossier, a Ti.Filesystem object is created for both our source and destination directories. In the next snippet, the sourceDir variable contains a directory reference to the SampleData folder we copied as part of the recipe setup, and targetDir references a new folder named NewSampleData, to be created in your device's data directory.

var sourceDir = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'SampleData'),

var targetDir = Ti.Filesystem.getFile(
  Ti.Filesystem.applicationDataDirectory
  + '/NewSampleData'),

Recursive listing of directory contents

When dealing with dynamic or downloadable content, you will often need to list all of the content of a specific directory. The listContents method allows you to recursively query the content of a directory, listing all files and subdirectories within.

The following code snippet demonstrates how to query our recipe's sourceDir for a list of all files and folders:

var listTargetContents = my.dossier.listContents(sourceDir.nativePath);

The listContents method returns a file explorer dictionary object, listing all of the files and subdirectories in a hierarchal format, as highlighted in the following screenshot:

Recursive listing of directory contents

Recursively copying directory contents

Many Enterprise apps are content driven. To improve your first-time installation experience, you might wish to bundle introductory content within your app and copy this bundled content into local cache on startup.

Using dossier module's copy method, you can copy the entire content of one folder to another. The following code demonstrates how to copy all the content of our source directory to our new target directory:

my.dossier.copy(sourceDir.nativePath,targetDir.nativePath);

Tip

During the copy process, any existing content in the target folder will be removed.

Recursively moving directory contents

The move method creates a new copy of all the content of your source directory to your target folder. Once the copying process has been successfully completed, the source directory is removed.

my.dossier.move(sourceDir.nativePath,targetDir.nativePath); 

Tip

During the move process, any existing content in the target folder will be removed and replaced with the content of our source folder. Additionally, after all the files have been moved, the source content will be removed if possible. In cases where the source directory is read-only, the contents will be retained.

See also

  • The Dossier module used in this recipe is an open source project available on Github. If you are interested in learning more or contributing, please visit the project at https://github.com/benbahrenburg/Dossier.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.242.204