GetFilesFromDirectory.ds

Review the GetFilesFromDirectory.ds referenced script files when scheduling inventory imports.

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

Note

1/*
2 * Gets a file list from an FTP/HTTP(S) server
3 *
4 * @input Directory  : dw.io.File   the directory to browse
5 * @input SortDirection : String    'ASCENDING' or 'DESCENDING'
6 * @input NamePattern  : String    The pattern for the filenames (use ".*" to get all)
7 * @input sampleLogger  : Object Sample logger input for Sample logger
8 * @output FileList  : dw.util.Collection  The files matching the pattern
9 * @output sampleLogger : Object Sample logger debug output for Sample logger
10 * @output sampleError  : Object Sample error error output for Sample logger
11*
12*/
13importPackage( dw.system );
14importPackage( dw.util );
15importPackage( dw.io );
16
17// Sample Logging ImportScript
18importPackage(dw.system);
19importPackage(dw.util);
20importPackage(dw.io);
21
22// Sample Logging ImportScript
23importScript("bc_sample:library/common/libContext.ds");
24importScript("bc_sample:library/utility/exception/libsampleException.ds");
25importScript("bc_sample:library/utility/logging/libLog.ds");
26
27function execute(pdict: PipelineDictionary): Number {
28    // sample Logging var
29    var _context: String = new Context("GetFilesFromDirectory.ds");
30    var _logger: Log = Log.getLogger(pdict);
31
32    // input variables
33    var directory: File = pdict.Directory;
34    var sortDirection: String = pdict.SortDirection;
35    var patternString: String = pdict.NamePattern;
36
37
38    if (directory == null || !directory.isDirectory()) {
39        return PIPELET_ERROR;
40    }
41    try {
42        var sorted: SortedSet = getSortedFileList(directory, patternString, sortDirection);
43
44        if (sorted != null) {
45            pdict.FileList = sorted;
46
47            _logger.info(_context, "Get files from " + directory);
48            // sample Logging call into debug handler
49            pdict.sampleLogger = _logger;
50
51            return PIPELET_NEXT;
52        } else {
53            _logger.debug(_context, "App server directory " + directory + " is empty.");
54            // sample Logging call into debug handler
55            pdict.sampleLogger = _logger;
56            return PIPELET_ERROR;
57        }
58    } catch (e) {
59        var exception: sampleException = new sampleException(_context, "Error occurred calling get files from directory", e);
60        _logger.error(_context, "Error occurred calling  get files from directory" + exception);
61        pdict.sampleError = exception;
62
63        return PIPELET_ERROR;
64    }
65}
66
67// UTIL.ds ?
68
69function descending(var1: Object, var2): Number {
70    if (var1 == var2) return 0
71    else if (var1 < var2) return 1
72    else return -1;
73}
74
75function getSortedFileList(directory: File, patternString: String, sortDirection: String): SortedSet {
76    var fileList: SortedSet = ((sortDirection == "DESCENDING") ? new SortedSet(descending) : new SortedSet());
77    return fileList.addAll(getFileList(directory, patternString)) ? fileList : null;
78}
79
80function getFileList(directory: File, patternString: String): ArrayList {
81    //@TODO : remove dir work around
82    Logger.debug("Get file list from app server directory {0}.", directory);
83
84
85
86    var fileList : ArrayList = new ArrayList();
87
88
89 var content : Array = directory.list();
90 if (content.length > 0)
91 {
92  if (!empty(patternString))
93   Logger.debug("Apply pattern {0}", patternString);
94  var pattern : RegExp = !empty(patternString) ? new RegExp(patternString) : null;
95  / /
96    var pattern = patternString;
97    for (var i: Number = 0; i < content.length; i++) {
98        var file: String = content[i];
99        if ((pattern == null) || file.match(pattern)) {
100            fileList.add(file);
101            Logger.debug("File / directory {0} added to list.", file);
102        } else
103            Logger.debug("File / directory {0} skipped.", file);
104    }
105} else {
106    //@TODO : remove dir work around
107    Logger.debug("App server directory {0} is empty.", directory);
108}
109
110return fileList;
111}