 
					
				
		
03-12-2018 05:38 AM
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.
03-12-2018 08:41 AM
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.
03-12-2018 08:41 AM
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.
 
					
				
		
03-13-2018 12:31 AM
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. 
03-13-2018 01:12 PM
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..
 
					
				
		
03-14-2018 12:41 AM
I'll try this way & will inform if i success in this.
Thank you.
03-13-2018 03:44 PM
just change the way you filled your "today" variable
 
					
				
		
03-19-2018 02:34 AM
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);
 
 }
 }
 }
 
}
 
					
				
				
			
		
Explore our Alfresco products with the links below. Use labels to filter content by product module.