cancel
Showing results for 
Search instead for 
Did you mean: 

Moving when a file already exists with that name

dallinns
Champ on-the-rise
Champ on-the-rise
I have a webscript set to move a file to a different folder. If, for example, I have a file named "dog.jpg" and I move it into a different folder that already has a file named "dog.jpg" then the script will just time out. I tried to catch the error and then do something else, but it just seems to time out rather than return false when there is a problem with the .move function.

my script looks like this:

destNode = search.findNode("workspace://SpacesStore/"+url.templateArgs.destNodeId);

nodeToMove = search.findNode("workspace://SpacesStore/"+url.templateArgs.nodeToMoveId);

var success = nodeToMove.move(destNode);

if(success){
   nodeToMove.save();
}else{

   var originalName = nodeToMove.name;
   for(var i = 1; !success; i++){
      nodeToMove.name = originalName + '('+i+')';
      nodeToMove.save();
      success = nodeToMove.move(destNode);
   }
   nodeToMove.save();
}

Does anyone know how I can handle this situation? How can I force a rename when there is already an existing file with that name?
5 REPLIES 5

mrogers
Star Contributor
Star Contributor
Check whether the destination path exists first.    I think that will get you around your immediate problem.

May also be worth raising an enhancement JIRA so this can be considered.

dallinns
Champ on-the-rise
Champ on-the-rise
Ok, so now I am trying this:

destNode = search.findNode("workspace://SpacesStore/"+url.templateArgs.destNodeId);

nodeToMove = search.findNode("workspace://SpacesStore/"+url.templateArgs.nodeToMoveId);

var childrenAtDest = destNode.children;

var alreadyExists = false;
var counter = 1;
var originalName = nodeToMove.name;
var nameToCheck = originalName;

model.whatever = Array();
model.original = originalName;

do{
   for(var j = 0; j < childrenAtDest.length; j++){
      
      if(childrenAtDest[j].name == nameToCheck){
         
         alreadyExists = true;
         
      }
      model.whatever.push(childrenAtDest[j].name + " vs " + nameToCheck + ": " + alreadyExists);
   }

   if(alreadyExists){
      nameToCheck = originalName+'('+counter+')';
      counter++;
   }else if(originalName != nameToCheck){
      nodeToMove.name = nameToCheck;
      nodeToMove.save();
   }
}while(alreadyExists);

nodeToMove.move(destNode);
and i'm outputting the results and looking at the names but the if statement that compares them never evaluates to true. Why is that?

Hi,
You can refer, how alfresco is handling the duplication file uploads, in upload.post.js file; You can follow the similar approach in your code.
Below the snippet for the same.



/**
          * Existing file handling.
          */
         
/ Upload component was configured to find a new unique name for clashing filenames
               var counter = 1,
                  tmpFilename,
                  dotIndex;

               while (existingFile !== null)
               {
                  dotIndex = filename.lastIndexOf(".");
                  if (dotIndex == 0)
                  {
                     // File didn't have a proper 'name' instead it had just a suffix and started with a ".", create "1.txt"
                     tmpFilename = counter + filename;
                  }
                  else if (dotIndex > 0)
                  {
                     // Filename contained ".", create "filename-1.txt"
                     tmpFilename = filename.substring(0, dotIndex) + "-" + counter + filename.substring(dotIndex);
                  }
                  else
                  {
                     // Filename didn't contain a dot at all, create "filename-1"
                     tmpFilename = filename + "-" + counter;
                  }
                  existingFile = destNode.childByNamePath(tmpFilename);
                  counter++;
               }
               filename = tmpFilename;
        


adnya
Champ in-the-making
Champ in-the-making
Hi, I have copied and added above script in alfresco.
And trying to apply it on folder via rule. But it is giving error in Catalina log file like
ReferenceError: "url" is not defined. (workspace://SpacesStore/0b0aec0b-8856-42af-8ace-8a9460426c4b#1)

Can anyone help me out from this? How i can set url?

muralidharand
Star Contributor
Star Contributor
When you're running the script via folder rule, you will not get url context in the script.
I believe, you can use <strong> document </strong> variable to get the document / folder node reference.
Please let me know, if this is not helping you.