user1552474
user1552474

Reputation: 31

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.

Upvotes: 2

Views: 3089

Answers (2)

Krutik Jayswal
Krutik Jayswal

Reputation: 3175

Adding more in this,as you are using content-rule.There is no need to search document using lucene/solr service .We can directly access document object,it refers to document on which your rule is being executed.

So the code will be like below.

var oldName = document.properties.name;
var newName = oldName.replace('.bin', '.pdf');
document.properties.name = newName;
document.save();

Upvotes: 0

Jeff Potts
Jeff Potts

Reputation: 10538

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();

Upvotes: 6

Related Questions