Script to copy documents to a relevant folder

cancel
Showing results for 
Search instead for 
Did you mean: 
anuradha1
Active Member II

Script to copy documents to a relevant folder

Jump to solution

Hi, I am using Alfresco Community 5.2 and i need to create a script to copy document to a relevant folder. 

copy-to-rel-folder.js
 

var createdDate = document.properties["cm:created"];

var dd = createdDate.getDate();
var mm = createdDate.getMonth()+1; //January is 0!

var yyyy = createdDate.getFullYear();
if(dd<10){
dd='0'+dd;
}
if(mm<10){
mm='0'+mm;
}
var today = dd + "-" + mm + "-" + yyyy;

var objDestFolder = companyhome.childByNamePath("Shared/SECRETARY/COMMISSION_MEETINGS/" + today);
document.copy(objDestFolder);

Ex: today = 20-03-2018 but I am having folders named 10-03-2018 ,18-04-2018 , 09-03-2017. I need to copy the document into 10-03-2018 folder. I need to select the folder using LIKE mm + "-" + "-" + "yyyy" (not considering the date).

Please help me.

Thank you.

1 Solution

Accepted Solutions
afaust
Master

Re: Script to copy documents to a relevant folder

Jump to solution

Simply get the name path until the "COMMISSION_MEETINGS" folder and then iterate over iters chidlren, checking the folder names against your pattern using endsWith / regex checks. Or you may perform an FTS query using the "search" root scope object looking into the path and doing a wildcard match on the folder names.

View solution in original post

6 Replies
afaust
Master

Re: Script to copy documents to a relevant folder

Jump to solution

Simply get the name path until the "COMMISSION_MEETINGS" folder and then iterate over iters chidlren, checking the folder names against your pattern using endsWith / regex checks. Or you may perform an FTS query using the "search" root scope object looking into the path and doing a wildcard match on the folder names.

anuradha1
Active Member II

Re: Script to copy documents to a relevant folder

Jump to solution

Thanks Axel.
Could you please give me a sample code how to do it. I have no idea about how to change my code to achieve this. 

afaust
Master

Re: Script to copy documents to a relevant folder

Jump to solution

How did you write the code you already have? It shouldn't need a sample to make two or three small adjustments to the code if you have written it yourself. I recommend you make yourself familiar with the ScriptNode API if you are unsure about which properties / functions to call. Specifically you should only need to use the childFileFolders() function and the name property to deal with the contents of the COMMISSION_MEETINGS folder and filter by name..

mehe
Senior Member II

Re: Script to copy documents to a relevant folder

Jump to solution

just change the way you filled your "today" variable

anuradha1
Active Member II

Re: Script to copy documents to a relevant folder

Jump to solution

I'll try this way & will inform if i success in this.
Thank you.

anuradha1
Active Member II

Re: Script to copy documents to a relevant folder

Jump to solution

I have achieved that task using childFileFolders() method.

Thanks all for helping me.

My JS code is below, in case if someone needs.

//Get document created date
var createdDate = document.properties["cm:created"];

//Get date, month & year seperately from the createdDate
var dd = createdDate.getDate();
var mm = createdDate.getMonth()+1; //January is 0!
var yyyy = createdDate.getFullYear();

//Date & month formatting
if(dd<10){
dd='0'+dd;
}
if(mm<10){
mm='0'+mm;
}

//Get access to the parent folder
var objDestFolder = companyhome.childByNamePath("Shared/public");

//Get all childrens in the parent folder into nodes array
var nodes = objDestFolder.childFileFolders();
var x = nodes.length;
nodes.sort(function(a, b){return a - b});

//Chack whether folder name(date) > created date
for (i = 0; i < x; i++) {
var ch = objDestFolder.children[i].name;
var ch1 = objDestFolder.children[i].properties["cm:title"];
var res = ch1.split(": ");
var fDate = res[1];
var res1 = fDate.split("/");
var fDD = res1[0];
var fMM = res1[1];
var fYYYY = res1[2];
if(yyyy == fYYYY)
{
if(mm == fMM)
{
if(dd <= fDD)
{
//Document move to the relevant folder
var folderDes = companyhome.childByNamePath("Shared/public/" + ch);
document.move(folderDes);

}
}
}

}