ConsoleWrite(_FileNewNameEx(@ScriptName, @ScriptDir))
Func _FileNewNameEx($sFileName, $sDir, $iExt_Mod = -1, $sDelim1 = -1, $sDelim2 = -1)
If Not StringInStr(FileGetAttrib($sDir), "D") Then Return $sFileName
If Not FileExists($sDir & "\" & $sFileName) Then Return $sFileName
$sDir = StringRegExpReplace($sDir, "\\ *$", "")
Local $sName = StringRegExpReplace($sFileName, "\.[^.]*$", "")
If $iExt_Mod <> -1 Then $sName = StringRegExpReplace($sFileName, "\.[^.]*(\.[^.]*$|$)", "")
If $sDelim1 = -1 Then $sDelim1 = " ("
If $sDelim2 = -1 Then $sDelim2 = ")"
Local $sDelim1_RE = StringRegExpReplace($sDelim1, '([][{}()|.?+*\\^])', '\\\1')
Local $sDelim2_RE = StringRegExpReplace($sDelim2, '([][{}()|.?+*\\^])', '\\\1')
Local $iDelim_Num = StringRegExpReplace($sName, ".*" & $sDelim1_RE & "(.*)" & $sDelim2_RE & ".*$", "\1")
If @extended = 0 Then $iDelim_Num = 0
$iDelim_Num = Number($iDelim_Num)
Local $sExtn = StringRegExpReplace($sFileName, "^.*\.", ".")
If @extended = 0 Then $sExtn = ""
Local $iCount = $iDelim_Num + 1, $sRet_FileName = $sFileName
While FileExists($sDir & "\" & $sRet_FileName)
If $iDelim_Num > 0 Then
$sRet_FileName = StringRegExpReplace($sRet_FileName, _
"(.*)" & $sDelim1_RE & $iDelim_Num & $sDelim2_RE, "\1" & $sDelim1 & $iCount & $sDelim2)
$iDelim_Num += 1
Else
If $sDelim1 <> -1 And $sDelim2 <> -1 Then
$sRet_FileName = $sName & $sDelim1 & $iCount & $sDelim2 & $sExtn
ElseIf $sDelim1 <> -1 And $sDelim2 = -1 Then
$sRet_FileName = $sName & $sDelim1 & $iCount & $sExtn
EndIf
EndIf
$iCount += 1
WEnd
Return $sRet_FileName
EndFunc