GetFilesFromDirectory.ds

Note: This code is provided as a sample. It isn't a supported part of the SiteGenesis application.

/*
 * Gets a file list from an FTP/HTTP(S) server
 *
 * @input Directory  : dw.io.File   the directory to browse
 * @input SortDirection : String    'ASCENDING' or 'DESCENDING'
 * @input NamePattern  : String    The pattern for the filenames (use ".*" to get all)
 * @input sampleLogger  : Object Sample logger input for Sample logger
 * @output FileList  : dw.util.Collection  The files matching the pattern
 * @output sampleLogger : Object Sample logger debug output for Sample logger
 * @output sampleError  : Object Sample error error output for Sample logger
*
*/
importPackage( dw.system );
importPackage( dw.util );
importPackage( dw.io );

// Sample Logging ImportScript
importPackage(dw.system);
importPackage(dw.util);
importPackage(dw.io);

// Sample Logging ImportScript
importScript("bc_sample:library/common/libContext.ds");
importScript("bc_sample:library/utility/exception/libsampleException.ds");
importScript("bc_sample:library/utility/logging/libLog.ds");

function execute(pdict: PipelineDictionary): Number {
    // sample Logging var
    var _context: String = new Context("GetFilesFromDirectory.ds");
    var _logger: Log = Log.getLogger(pdict);

    // input variables 
    var directory: File = pdict.Directory;
    var sortDirection: String = pdict.SortDirection;
    var patternString: String = pdict.NamePattern;


    if (directory == null || !directory.isDirectory()) {
        return PIPELET_ERROR;
    }
    try {
        var sorted: SortedSet = getSortedFileList(directory, patternString, sortDirection);

        if (sorted != null) {
            pdict.FileList = sorted;

            _logger.info(_context, "Get files from " + directory);
            // sample Logging call into debug handler
            pdict.sampleLogger = _logger;

            return PIPELET_NEXT;
        } else {
            _logger.debug(_context, "App server directory " + directory + " is empty.");
            // sample Logging call into debug handler
            pdict.sampleLogger = _logger;
            return PIPELET_ERROR;
        }
    } catch (e) {
        var exception: sampleException = new sampleException(_context, "Error occurred calling get files from directory", e);
        _logger.error(_context, "Error occurred calling  get files from directory" + exception);
        pdict.sampleError = exception;

        return PIPELET_ERROR;
    }
}

// UTIL.ds ?

function descending(var1: Object, var2): Number {
    if (var1 == var2) return 0
    else if (var1 < var2) return 1
    else return -1;
}

function getSortedFileList(directory: File, patternString: String, sortDirection: String): SortedSet {
    var fileList: SortedSet = ((sortDirection == "DESCENDING") ? new SortedSet(descending) : new SortedSet());
    return fileList.addAll(getFileList(directory, patternString)) ? fileList : null;
}

function getFileList(directory: File, patternString: String): ArrayList {
    //@TODO : remove dir work around  
    Logger.debug("Get file list from app server directory {0}.", directory); 

    

    var fileList : ArrayList = new ArrayList();
	

 var content : Array = directory.list();
 if (content.length > 0)
 {
  if (!empty(patternString))
   Logger.debug("Apply pattern {0}", patternString);
  var pattern : RegExp = !empty(patternString) ? new RegExp(patternString) : null;
  / /
    var pattern = patternString;
    for (var i: Number = 0; i < content.length; i++) {
        var file: String = content[i];
        if ((pattern == null) || file.match(pattern)) {
            fileList.add(file);
            Logger.debug("File / directory {0} added to list.", file);
        } else
            Logger.debug("File / directory {0} skipped.", file);
    }
} else {
    //@TODO : remove dir work around  
    Logger.debug("App server directory {0} is empty.", directory);
}

return fileList;
}