lib/fsExt.js
function checkExistsName(filePath, isDir, callback, count){ // callback(err, fullPath)
    if(typeof callback !== 'function') throw new Error('Wrong arguments');
    var pathInfo = resolve(filePath), newFullPath, oldName;
    count = count || 1;
    
    fs.exists(pathInfo.fullPath, function(exists){
        if(!exists) callback(null, pathInfo.fullPath);
        else { // generate new file or dir name, and try again
            oldName = count>1 ? '('+(count-1)+')' : '';
            if(isDir) {
                newFullPath = pathInfo.fullPath.replace(new RegExp(oldName.escape()+'$'),'') + '(' +count+ ')';
            }
            else {
                oldName = oldName + '.' + pathInfo.extName;
                newFullPath = pathInfo.fullPath.replace(new RegExp(oldName.escape()+'$'), '(' +count+ ').' + pathInfo.extName);
            }
            checkExistsName(newFullPath, isDir, callback, count+1);
        }
    });
}
function checkExistsName(filePath, isDir, callback, count){ // callback(err, fullPath)
    if(typeof callback !== 'function') throw new Error('Wrong arguments');
    var pathInfo = resolve(filePath), newFullPath, oldName;
    count = count || 1;
    
    fs.exists(pathInfo.fullPath, function(exists){
        if(!exists) callback(null, pathInfo.fullPath);
        else { // generate new file or dir name, and try again
            oldName = count>1 ? '('+(count-1)+')' : '';
            if(isDir) {
                newFullPath = pathInfo.fullPath.replace(new RegExp(oldName.escape()+'$'),'') + '(' +count+ ')';
            }
            else {
                oldName = oldName + '.' + pathInfo.extName;
                newFullPath = pathInfo.fullPath.replace(new RegExp(oldName.escape()+'$'), '(' +count+ ').' + pathInfo.extName);
            }
            checkExistsName(newFullPath, isDir, callback, count+1);
        }
    });
}
function checkExistsName ( filePath , isDir , callback , count ) { // callback(err, fullPath) if ( typeof callback !== 'function' ) throw new Error ( 'Wrong arguments' ) ; var pathInfo = resolve ( filePath ) , newFullPath , oldName ; count = count || 1 ; fs . exists ( pathInfo . fullPath , function ( exists ) { if ( ! exists ) callback ( null , pathInfo . fullPath ) ; else { // generate new file or dir name, and try again oldName = count > 1 ? '(' + ( count - 1 ) + ')' : '' ; if ( isDir ) { newFullPath = pathInfo . fullPath . replace ( new RegExp ( oldName . escape ( ) + '$' ) , '' ) + '(' + count + ')' ; } else { oldName = oldName + '.' + pathInfo . extName ; newFullPath = pathInfo . fullPath . replace ( new RegExp ( oldName . escape ( ) + '$' ) , '(' + count + ').' + pathInfo . extName ) ; } checkExistsName ( newFullPath , isDir , callback , count + 1 ) ; } } ) ; }
check if file or path already exists, and append number "_(1)"
@param {String} filePath full path
@param {Boolean} isDir
@param {Function} callback
@param {Number} count
check if file or path already exists and append number _ ( 1 )