RenameFile.ds

Note: This code is provided as a sample. It isn't a supported part of the SiteGenesis application.
/**
 * Moves a file to another directory.
 *
 * @input SourceFileName  : String   		the file to move
 * @input TargetFileName  : String   		the file to move
 * @input SourceDirectory : dw.io.File   		the processing directory where the file is located
 * @input TargetDirectory : dw.io.File   		the target processing directory
 * @input sampleLogger    : Object Sample logger	input for Sample logger
 * @input Status  	  : dw.system.Status 	The stautus of the DW import pipelet
 * @output TargetFile	  : dw.io.File
 * @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.io);



importScript("bc_sample:library/impex/libImpExConstants.ds");
// 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("RenameFile.ds");
    var _logger: Log = Log.getLogger(pdict);



    var sourceFileName: String = pdict.SourceFileName;
    var targetFileName: String = pdict.TargetFileName;
    var sourceDirectory: String = pdict.SourceDirectory;
    var targetDirectory: String = pdict.TargetDirectory;
    var status: String = pdict.Status;

    var sourceFile: File = new File(sourceDirectory, sourceFileName);
    var targetFile: File = new File(targetDirectory, targetFileName);

    if (status != null) {
        _logger.error(_context, "Status :" + status.message + " : End Status");
    }


    if (!sourceFile.exists()) {
        _logger.error(_context, "Source file '" + sourceFile.name + "' in path '" + sourceFile.path + "' cannot be found");
        return PIPELET_ERROR;
    }
    if (targetFile.exists()) {
        _logger.warn(_context, "Target file '" + targetFile.name + "' in path '" + targetFile.path + "' already exists. Overwriting it.");
    }

    _logger.info(_context, "Moving file " + sourceFile.name + " to " + targetFile.name);

    try {
        sourceFile.renameTo(targetFile);
        pdict.TargetFile = targetFile;



    } catch (e) {
        var exception: sampleException = new sampleException(_context, "Failed moving file " + sourceFileName, e);
        _logger.error(_context, "Failed moving file " + sourceFileName + exception);
        pdict.sampleError = exception;

        return PIPELET_ERROR;
    }

    return PIPELET_NEXT;
}