index.js
function _getSafeFileName(filename, directory) {
        var deferred = q.defer();
    
        fs.readdir(directory, function(err, files) {
    
            if(err) {
              // This means the directory doesn't exist
              if (err.errno === 34 && err.code === 'ENOENT') {
                deferred.resolve(filename);
              }
              else {
                deferred.reject(err);
              }
            }
            else {
              var index = files.indexOf(filename);
              if (index > -1) { 
    
                // Get filename and extension (if it has one)
                var name, extension;
                var splitString = filename.split('.');
    
                // Has extension
                if (splitString.length > 1) {
                  extension = splitString.pop(); 
                  name = splitString.join('.');
    
                  // Wait! Is this a hidden file?
                  if (!name) {
                    name = '.' + extension;
                    extension = '';
                  }
                }
                // No extension
                else {
                  name = filename;
                  extension = '';
                }
    
                // Get the copy number appended to the name and increment, if any
                var matches = name.match(/\((\d+)\)$/);
                if (matches) {
                  var copyNumber = Number(matches[1]) + 1;
                  name = name.replace(/\((\d+)\)$/, '(' + copyNumber + ')'); 
                }
                else {
                  name += '(1)';
                }
    
                // Assemble new filename
                if (extension) {
                  filename = name + '.' + extension;
                }
                else {
                  filename = name;
                }
    			deferred.resolve(_getSafeFileName(filename, directory));
              }
    		  else {
              	deferred.resolve(filename);
    		  }
            }       
          });
    
        return deferred.promise;
      }
function _getSafeFileName(filename, directory) {
        var deferred = q.defer();
    
        fs.readdir(directory, function(err, files) {
    
            if(err) {
              // This means the directory doesn't exist
              if (err.errno === 34 && err.code === 'ENOENT') {
                deferred.resolve(filename);
              }
              else {
                deferred.reject(err);
              }
            }
            else {
              var index = files.indexOf(filename);
              if (index > -1) { 
    
                // Get filename and extension (if it has one)
                var name, extension;
                var splitString = filename.split('.');
    
                // Has extension
                if (splitString.length > 1) {
                  extension = splitString.pop(); 
                  name = splitString.join('.');
    
                  // Wait! Is this a hidden file?
                  if (!name) {
                    name = '.' + extension;
                    extension = '';
                  }
                }
                // No extension
                else {
                  name = filename;
                  extension = '';
                }
    
                // Get the copy number appended to the name and increment, if any
                var matches = name.match(/\((\d+)\)$/);
                if (matches) {
                  var copyNumber = Number(matches[1]) + 1;
                  name = name.replace(/\((\d+)\)$/, '(' + copyNumber + ')'); 
                }
                else {
                  name += '(1)';
                }
    
                // Assemble new filename
                if (extension) {
                  filename = name + '.' + extension;
                }
                else {
                  filename = name;
                }
    			deferred.resolve(_getSafeFileName(filename, directory));
              }
    		  else {
              	deferred.resolve(filename);
    		  }
            }       
          });
    
        return deferred.promise;
      }
function _getSafeFileName ( filename , directory ) { var deferred = q . defer ( ) ; fs . readdir ( directory , function ( err , files ) { if ( err ) { // This means the directory doesn't exist if ( err . errno === 34 && err . code === 'ENOENT' ) { deferred . resolve ( filename ) ; } else { deferred . reject ( err ) ; } } else { var index = files . indexOf ( filename ) ; if ( index > - 1 ) { // Get filename and extension (if it has one) var name , extension ; var splitString = filename . split ( '.' ) ; // Has extension if ( splitString . length > 1 ) { extension = splitString . pop ( ) ; name = splitString . join ( '.' ) ; // Wait! Is this a hidden file? if ( ! name ) { name = '.' + extension ; extension = '' ; } } // No extension else { name = filename ; extension = '' ; } // Get the copy number appended to the name and increment, if any var matches = name . match ( / \((\d+)\)$ / ) ; if ( matches ) { var copyNumber = Number ( matches [ 1 ] ) + 1 ; name = name . replace ( / \((\d+)\)$ / , '(' + copyNumber + ')' ) ; } else { name += '(1)' ; } // Assemble new filename if ( extension ) { filename = name + '.' + extension ; } else { filename = name ; } deferred . resolve ( _getSafeFileName ( filename , directory ) ) ; } else { deferred . resolve ( filename ) ; } } } ) ; return deferred . promise ; }
Append a copy number to a filename if a
file by that same name already exists

@param string
@param string

@return promise
Append a copy number to a filename if a file by that same name already exists