Send to Folder -> Rename file if exists

Hi

Most Scan softwares workflows, have the possibility to rename the output pdf file in a folder, if another file already exists in the folder with the same name.

The flows simply rename the pdf file _001.pdf, _002.pdf, _003.pdf etc., so no file is overwritten.

The plugin “Send To Folder” in the workflow can only overwrite the file or append existing file.

Do’s anybody have a simple script to do the task. The script should check the output folder for matching filename, and then return the extension for the new file like _001, _002 etc,

Then the script could be used just before the “Send To Folder”

Thanks in advance
Klaus

Here’s a piece of code I wrote a while back that does what you’re looking for. I tested it quickly and it appears to work as expected.

CONST_FOLDER = "C:/Tests/Output";
CONST_FILENAME = "MyFile.pdf";

var fName = generateFileName(CONST_FOLDER, CONST_FILENAME);
Watch.SetVariable("fName",fName);

function generateFileName(folder,file){
  var oFSO = new ActiveXObject("Scripting.FileSystemObject");
  var oFolder = oFSO.GetFolder(folder);

  var nameComponents = file.toLowerCase().split(".");
  var fNameNoExt = nameComponents[0];
  var extension = nameComponents[1];
  var arr = [];
  var fileFound = false;

  var e = new Enumerator(oFolder.Files);
  for (var i=0;!e.atEnd();e.moveNext()){
    var fNoExt = e.item(i).name.split(".")[0].toLowerCase();
    fileFound = fileFound || fNoExt===fNameNoExt;
    arr.push(fNoExt);
  }

  if(!fileFound)
    return fNameNoExt+"."+extension;
  else {
    var family = arr.filter(function(f){
      return f.substr(0,fNameNoExt.length)===fNameNoExt;
    });
    if(family.length===1) {
      return fNameNoExt+"_0."+extension;
    } else {
      var last = family[family.length-1].split("_")[1];
      last++;
      return fNameNoExt+"_"+last+"."+extension;
    }
  }
}

If stores the new file name (with a _xxx suffix if need be) in the process variable named fName. You can then use that variable name in your Send To Folder task. You’ll probably have to customize it a bit for your needs, but that should be fairly easy.

1 Like

Tried to copy you script completly, is something missing like a function (f) ?
It gives me an error on this line:
var family = arr.filter(function(f){

Have you set the scripting language to Enhanced JScript? And did you copy all the code?

That helped just had it on Jscript…

Thank