Create folder via Datamapper Postprocessor script

Hello,

I want to export my datamodel as a csv file via script in the Datamapper Postprocessor. My script works fine, but if the target folder (where the export file will be stored) does not exist the script fails.

I tried to use an ActiveXObject (FileSystemObject) to create the folder. But obviously ActiveX is not supported.

How can I create a folder dynamically in a Datamapper script? I can not use Workflow in this case.

Thanks,
Thomas

Use the execute() method, as in the following example:

var myFolder = "C:\\test\\newFolder";
var tmpFile = createTmpFile();

var writer = openTextWriter(tmpFile.getPath());
writer.write("Hello World!");
writer.newLine();
writer.close();

execute("cmd /c md "+myFolder);

tmpFile.move(myFolder+"\\MyNewFile.txt");

The “cmd /c” command launches an instance of the Windows command line and instructs it to execute a single command (through the /c switch). That command is the internal md command, used to create a folder. If the folder already exists, the md command does nothing, so you can use it safely every time you run the post processor.

1 Like

Hi Phil,

thank you for your great answer! It works fine.

Thomas

Hi Phil,
Back again.
Working fine when running the datamapper on the server, but when the REST API tried to run the execute command (path string stored in JobInfo1), no directory is created. API works fine if a directory exists though.
Known issue, or just me again?
Cheers,
Tim

Works fine on my end. Are you trying to save a file to a network share? If so, is that network share available to the Connect Server at runtime?

Try saving to a local folder on the server (e.g. C:\\Tests\\MyTest) and see if that works any better.

Hi Phil,

Thanks for getting back to me.
Nope, not on a network share –
I’m sending a variable into JobInfo1, and writing out a text file using the following:

var origFn = automation.properties.OriginalFilename;
// OriginalFilename passed to datamapper from REST API json payload
if(origFn.indexOf(".")!== -1){
var fnNoExt = origFn.substring(0, origFn.lastIndexOf('.'));
}
else {
var fnNoExt = origFn
}
var workingPath = automation.jobInfo.JobInfo1;
// JobInfo1 passed to datamapper from REST API json payload

createdDirectory = workingPath+fnNoExt

// create directory if it doesn't exist
execute("cmd /c md "+workingPath);

var fileOut = openTextWriter(workingPath+fnNoExt+".dpf");

// ***** If column names need to be added as the first line in the output file,
// ***** comment out the next three lines (after adjusting the Labels
 var Labels = 'Letter Code'+'\t'+'Courier ID'+'\t'+'Date'+'\t'+'Post Code';
 fileOut.write(Labels);
 fileOut.newLine();

//Go through all records to extract the fields value
var str="";
for (var i=0; i < data.records.length;i++){

letterCode = data.records[i].fields.Letter_Code;
courierID = data.records[i].fields.CourierID;
date = data.records[i].fields.Date;
postCode = data.records[i].fields.Add_5;

// concatenate the fields value with tabs to have TSV line (needed for .dpf)
str = letterCode + '\t' + courierID + '\t' + date + '\t' + postCode;

fileOut.write(str);
fileOut.newLine();
}
//Close file
fileOut.close();

All works fine if I send JobInfo1 a path that already exists eg. D:/aiw/aiw1/spool/default/DataOut
However, as soon as the execute command is uncommented again, no directory is created – doesn’t seem to matter if I use variables to initially code the path or hardcode a string.
As I said, works in debug on the server, but not through the API even with admin rights token.
All the best,

Tim

Use backslashes (\) instead of slashes (/). The execute command is apparently finicky about it.

Also, either make sure the path you pass in Jobinfo1 ends with one of those slashes, or that you insert a slash in between the folder value and the filename when you create the file (e.g. openTextWriter(workingPath+"\"+fnNoExt+".dpf");), otherwise you’ll end up with a file named D:\aiw\aiw1\spool\default\DataOutMY_ORIGINAL_FILE.dpf instead of the intended D:\aiw\aiw1\spool\default\DataOut\MY_ORIGINAL_FILE.dpf

Thanks Phil, will give it a go…

Fabulous - yep was the backslashes.
Had to cast the path-like object as a string (python) and Connect was happy.
Thanks ever so much for your time,
T