2009-09-20

Verify or Create Folder Method

One of the limitations of ExtendScript's Folder object is its inability to create a chain of folder paths. It has an .exists property and a .create() method, but each only looks at the final folder in the path. If you pass .create() a path that is several folders away from a parent folder that exists, the creation will fail. Each folder in the chain of folders must be created individually.
This function does that. There are two ways of doing this, build and parse an array like a stack or use a recursive function. This uses a stack.

Folder.prototype.verify = function() {
//-------------------------------------------------------------------------
//-- V E R I F Y
//-------------------------------------------------------------------------
//-- Generic: Yes
//-------------------------------------------------------------------------
//-- Purpose: A folder object method to verify if the folder specified
//-- actually exists. If the folder doesn't exist, the method will
//-- attempt to create it.
//-------------------------------------------------------------------------
//-- Returns: True if the folder exists, false if it couldn't create it.
//-------------------------------------------------------------------------
//-- Sample Call:
//-- Folder ( '~/IsItHere' ).verify() ;
//-------------------------------------------------------------------------
//-- Calls: Nothing
//-------------------------------------------------------------------------
//-- Written 2009.09.19 on board US Airways flight 4376.
//-- eps@electronicpublishingsupport.com
//-------------------------------------------------------------------------
if ( this.exists ) {
return true ;
}
else {
//-- Create an array like a stack of folders that will need
//-- to be created.
var foldersToCreate = new Array () ;
//-- Create a local copy of the original folder
//-- because it needs to be modified within
//-- this routine.
//-- Using the absoluteURI version of the name to point
//-- all the way through to the sever. Only this version
//-- of the activeFolder needs to use this absoluteURI
//-- notation because.
var activeFolder = new Folder ( this.absoluteURI ) ;

//-- Loop through the path structure until every part of
//-- of the folder path is checked. Any non-existanet
//-- folder path will be added to the array to create.
while ( ! activeFolder.exists ) {
//-- Add the activeFolder to the array of folders to create
foldersToCreate.push( activeFolder ) ;
//-- Now get to the parent of the folder.
//-- Can't use .parent because if the active folder
//-- doesn't exist, it won't have a parent.
activeFolder = new Folder ( activeFolder.path ) ;
}
//-- At this point we have an array of folders that need to be
//-- created. The array will have one element too many because
//-- of the final line of the
while ( foldersToCreate.length > 0 ) {
//-- Remove the last item added to the array of folders to create
activeFolder = foldersToCreate.pop() ;
//-- Try to create this folder
if ( ! activeFolder.create() ) {
return false ;
} //-- end of if create
} //-- end of lower while
} //-- end of else from way up top
//-- At this point the folder has to exist else we already returned false
return true ;
}
//

No comments:

Post a Comment