Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Detailed Directory List Similar to CF's native DirectoryList but returns these additional pieces of info: datelastmodified, name, size, type, directory, hidden, pathname.
/**
* DetailedDirectoryList
* @displayname Detailed Directory List
* @hint Similar to CF's native DirectoryList but returns these additional pieces of info: datelastmodified, name, size, type, directory, hidden, pathname
* Inspired by code from Anuj Gakhar with modifications by Ed Martin. (http://www.anujgakhar.com/2007/11/08/java-version-of-cfdirectory-updated/)
* @author Ryan Mueller http://creativenotice.com, @CreativeNotice
* @param String path The system path for the directory to search. Required.
* @param Numeric count Number of results to return.
* @param Boolean recurse Should we recurse into child directories? Default = FALSE Required.
* @param String listInfo How should we respond? query or array are you options. Default = 'query' Required.
* @param String filter Regex to filter file names with.
* @param String sort Column name(s) to sort by. Qualify with 'asc' or 'desc'. Default = 'name DESC'. Required
* @param Query dirInfo The query object results will be in. You don't need to use this, it's for recursion Required
* @returntype Any
*/
function DetailedDirectoryList (
required string path,
required numeric count = 1000,
required boolean recurse = FALSE,
required string listInfo = 'query',
required string filter = '*.*',
required string sort = 'name DESC',
required query dirInfo = queryNew('datelastmodified,name,size,type,directory,hidden,pathname') )
{
var thisFile = '';
var listFiles = '';
var theType = '';
var theDate = '';
var theDateFormat = '';
var filesArray = [];
if( len( arguments.path ) ){
listFiles = createObject( 'java', 'java.io.File' ).init( trim( arguments.path ) ).listFiles();
theDate = createObject( 'java', 'java.util.Date' );
theDateFormat = createObject( 'java', 'java.text.SimpleDateFormat' );
// Loop through file list, adding query rows for each
for( var i=1; i<=arraylen( listFiles ); i=i+1 ){
// What type of item are we working with?
var this_type = iif( listFiles[i].isFile(), "'file'", "'dir'" );
// get the name, we are going to filter using it
var this_name = listFiles[i].getName();
// if no filter supplied, OR supplied and matches file name
if( len(arguments.filter) < 1 || ( len(arguments.filter) && ReFindNoCase( arguments.filter, this_name ) ) ){
// Add new row to query
queryAddRow( arguments.dirInfo );
querySetCell( arguments.dirInfo, 'datelastmodified', theDateFormat.format( listFiles[i].lastModified() ) );
querySetCell( arguments.dirInfo, 'name', listFiles[i].getName() );
querySetCell( arguments.dirInfo, 'size', listFiles[i].length() );
querySetCell( arguments.dirInfo, 'directory', listFiles[i].getParent() );
querySetCell( arguments.dirInfo, 'hidden', listFiles[i].isHidden() );
querySetCell( arguments.dirInfo, 'pathname', listFiles[i].getPath() );
querySetCell( arguments.dirInfo, 'type', this_type );
}
// Recurse if this_type is 'dir' and recurse is true
if( this_type == 'dir' && arguments.recurse ){
arguments.dirInfo = DetailedDirectoryList( listFiles[i].getPath(), arguments.recurse, arguments.listInfo, arguments.filter, arguments.sort, arguments.dirInfo );
}
}
// Filter and order using Query of Queries
var q = new Query();
q.setAttributes( QoQsrcTable = arguments.dirInfo, maxrows = arguments.count );
q.setSQL(
"SELECT *
FROM QoQsrcTable
ORDER BY #trim(arguments.sort)#"
);
dirInfo = q.execute( dbtype='query').getResult();
// How should we return, query object or array?
if( arguments.listInfo == 'query' ){
return dirInfo;
}else{
// We want results as an array of structures
for( var i=1; i<=dirInfo.RecordCount; i=i+1 ){
// populate array
var this_struct = {
'datelastmodified' = dirInfo.dateLastModified[i],
'name' = dirInfo.name[i],
'size' = dirInfo.size[i],
'directory' = dirInfo.directory[i],
'hidden' = dirInfo.hidden[i],
'pathname' = dirInfo.pathname[i],
'type' = dirInfo.type[i]
};
arrayAppend(filesArray, this_struct );
}
return filesArray;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment