Search code examples
javascriptalfresco

Renaming files in an Alfresco script


I am struggling to write a script in Alfresco to rename file extension.

The file is saved as filename.bin. I am using content rules to say when filename equals *bin rename to *pdf.

I am struggling a bit with the script and would appreciate any help.

My script is as below:

// change the name of this document
document.properties.name = document.properties.name+".pdf";
// add a new property string
document.properties["cm:locale"] = mylocalenode;
// save the property modifications
document.save();

but doesn't seem to get me anywhere.


Solution

  • The script as written would take a document named "filename.bin" and rename it to "filename.bin.pdf". It would then set a property called "cm:locale" equal to the value of mylocalenode, which appears to be undefined in this snippet. I don't know what you are going for with the cm:locale so I will ignore that and give you a script that will search for the document named filename.bin and change its name.

    If you would rather iterate over the children in a folder, you should be able to look at the Alfresco JavaScript API to figure out how to modify the snippet below to do that.

    var results = search.luceneSearch("@cm\\:name:filename.bin");
    var doc = results[0]; // assumes there is only one result, which may not be what you want
    var oldName = doc.properties.name;
    var newName = oldName.replace('.bin', '.pdf');
    doc.properties.name = newName;
    doc.save();