DelFile.ds

Note: This code is provided as a sample. It isn't a supported part of the SiteGenesis application.
/**
 * Delete the file or directory.
 *
 * @input SourceFileName  : String   the file to move
 * @input SourceDirectory : dw.io.File   the processing directory where the file is located
 * @input sampleLogger    : Object Sample logger	input for Sample logger
 * @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);



// 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("DelFile.ds");
    var _logger: Log = Log.getLogger(pdict);

    var sourceFileName: String = pdict.SourceFileName;
    var sourceDirectory: String = pdict.SourceDirectory;

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

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

    _logger.info(_context, "Delet file " + sourceFile);

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

    return PIPELET_NEXT;
}