AnsweredAssumed Answered

Moving when a file already exists with that name

Question asked by dallinns on Nov 28, 2011
Latest reply on Mar 17, 2015 by muralidharand
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?

Outcomes