RenameFile.ds

Review the RenameFile.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 * Moves a file to another directory.
3 *
4 * @input SourceFileName  : String   		the file to move
5 * @input TargetFileName  : String   		the file to move
6 * @input SourceDirectory : dw.io.File   		the processing directory where the file is located
7 * @input TargetDirectory : dw.io.File   		the target processing directory
8 * @input sampleLogger    : Object Sample logger	input for Sample logger
9 * @input Status  	  : dw.system.Status 	The stautus of the DW import pipelet
10 * @output TargetFile	  : dw.io.File
11 * @output sampleLogger   : Object Sample logger	debug output for Sample logger
12 * @output sampleError 	  : Object Sample error	error output for Sample logger
13 */
14importPackage(dw.system);
15importPackage(dw.io);
16
17
18
19importScript("bc_sample:library/impex/libImpExConstants.ds");
20// Sample Logging ImportScript
21importScript("bc_sample:library/common/libContext.ds");
22importScript("bc_sample:library/utility/exception/libsampleException.ds");
23importScript("bc_sample:library/utility/logging/libLog.ds");
24
25
26
27function execute(pdict: PipelineDictionary): Number {
28    // Sample Logging var
29    var _context: String = new Context("RenameFile.ds");
30    var _logger: Log = Log.getLogger(pdict);
31
32
33
34    var sourceFileName: String = pdict.SourceFileName;
35    var targetFileName: String = pdict.TargetFileName;
36    var sourceDirectory: String = pdict.SourceDirectory;
37    var targetDirectory: String = pdict.TargetDirectory;
38    var status: String = pdict.Status;
39
40    var sourceFile: File = new File(sourceDirectory, sourceFileName);
41    var targetFile: File = new File(targetDirectory, targetFileName);
42
43    if (status != null) {
44        _logger.error(_context, "Status :" + status.message + " : End Status");
45    }
46
47
48    if (!sourceFile.exists()) {
49        _logger.error(_context, "Source file '" + sourceFile.name + "' in path '" + sourceFile.path + "' cannot be found");
50        return PIPELET_ERROR;
51    }
52    if (targetFile.exists()) {
53        _logger.warn(_context, "Target file '" + targetFile.name + "' in path '" + targetFile.path + "' already exists. Overwriting it.");
54    }
55
56    _logger.info(_context, "Moving file " + sourceFile.name + " to " + targetFile.name);
57
58    try {
59        sourceFile.renameTo(targetFile);
60        pdict.TargetFile = targetFile;
61
62
63
64    } catch (e) {
65        var exception: sampleException = new sampleException(_context, "Failed moving file " + sourceFileName, e);
66        _logger.error(_context, "Failed moving file " + sourceFileName + exception);
67        pdict.sampleError = exception;
68
69        return PIPELET_ERROR;
70    }
71
72    return PIPELET_NEXT;
73}