Hello all,
What I'm trying to accomplish is moving a user selected file to a user selected directory - if the selected file already exists in that directory I want to rename that file.

For example: if user selects file 'abc.txt' i want to loop thru the selected directory and check for 'abc.txt'. If it exists copy and rename the file 'abc [1].txt' and if 'abc [1].txt' already exists name it 'abc [2].txt', etc.

The code below works once until I get one duplicated copy 'abc [1].txt' after that it just overwrites that duplicated copy because I don't know how to check for the square brackets([x]).

Code:
function duplicate_file(file,dir) // file = 'abc.txt'
{
  // split file name and extension
  var name = file.split('.');
	
  // check for dupes in parent directory
  var str=name[0];

  // ?????????
  var patt = new RegExp('\\{'+str+'\\}', 'g');

  var dup_array = [];
  var d = dir.getDirectoryListing();
  var iterator = 1;

  for (var l=0;l<d.length;l++)
  {
    if(d[l].match(patt))
    {
      dup_array.push(d[l]);
      iterator++;
    }
  }	
  // put it all back together
  var dup = name[0] + ' [' + iterator + '].' + name[1];
  return dup;
}
Any ideas?? Thx!!!