Code : Tout sélectionner
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <SendMessage.au3>
;#Include <Array.au3>
Global Const $EM_GETSEL = 0xB0
Global $array[3][4], $list, $treat, $regex, $workingdir = @scriptdir, $times; = 1
Global $logfile = $workingdir & "\save.log", $font = "Times New Roman"
;HotkeySet("a", "_getsel")
$gui = GUICreate("Renommer fichiers par lots", 760, 205, -1, 50)
$folder = GUICtrlCreateInput($workingdir, 15, 10, 285, 20)
$btn_open = GUICtrlCreateButton("...", 305, 10, 25, 20)
GUICtrlCreateLabel("ext.", 15, 45, 20, 17)
$input_ext = GUICtrlCreateInput("*", 40, 40, 40, 22)
$btn_regex = GUICtrlCreateButton("RegExp ON", 100, 40, 90, 22)
$btn_getsel = GUICtrlCreateButton("Get selection >>", 215, 40, 115, 22)
GUICtrlCreateLabel("Remplacer", 25, 70, 55, 17)
GUICtrlCreateLabel("par", 235, 70, 25, 17)
$array[0][0] = GUICtrlCreateInput("", 15, 88, 210, 23)
$array[0][1] = GUICtrlCreateInput("", 230, 88, 100, 23)
$array[1][0] = GUICtrlCreateInput("", 15, 114, 200, 23)
$array[1][1] = GUICtrlCreateInput("", 220, 114, 110, 23)
$array[2][0] = GUICtrlCreateInput("", 15, 140, 190, 23)
$array[2][1] = GUICtrlCreateInput("", 210, 140, 120, 23)
For $i = 0 to 2
GUICtrlSetFont($array[$i][0], 12, 400, 0, $font)
GUICtrlSetFont($array[$i][1], 12, 400, 0, $font)
Next
$btn_test = GUICtrlCreateButton("Tester", 15, 175, 55, 22)
$log_box = GUICtrlCreateCheckbox("Log", 80, 178, 40, 20)
;GUICtrlSetState(-1, $GUI_CHECKED)
$btn_go = GUICtrlCreateButton("Remplacer", 125, 175, 70, 22)
$btn_undo = GUICtrlCreateButton("Undo", 205, 175, 50, 22)
$btn_reset = GUICtrlCreateButton("Actualiser", 265, 175, 65, 22)
$edit = GUICtrlCreateEdit("", 340, 10, 410, 175)
GUICtrlSetFont($edit, 12, 400, 0, $font)
GUICtrlCreateLabel("Modifier ou supprimer le nom du fichier dans l'edit pour l'exclure", 370, 186, 350, 17, 1)
GUICtrlSetState(-1, $GUI_FOCUS)
GUISetState(@SW_SHOWNA)
GUIRegisterMsg($WM_COMMAND, "_Space")
_Reset()
While 1
$msg = GUIGetMsg()
Switch $msg
Case $GUI_EVENT_CLOSE
Exit
Case $btn_open
$var = FileSelectFolder("Choose", "")
If not @error Then $workingdir = $var
GuiCtrlSetData($folder, $workingdir)
$logfile = $workingdir & "\save.log"
_Reset()
Case $btn_test
_Rename(0)
; GUICtrlSetState($btn_test, $GUI_DISABLE)
Case $btn_go
_Rename(1)
; GUICtrlSetState($btn_go, $GUI_DISABLE)
Case $btn_reset
_Reset()
GUICtrlSetState($btn_test, $GUI_ENABLE)
GUICtrlSetState($btn_go, $GUI_ENABLE)
$treat = ""
Case $btn_regex
If $regex = 0 Then
GUICtrlSetData($btn_regex, "RegExp OFF")
GUICtrlSetData($array[0][0], "((\s|_)*[\[\(\{].+?[\]\)\}](\s|_)*)")
Else
GUICtrlSetData($btn_regex, "RegExp ON")
GUICtrlSetData($array[0][0], "")
EndIf
$regex = not $regex
Case $btn_undo
_UnDo()
Case $btn_getsel
_Getsel()
EndSwitch
WEnd
;=============================================================
Func _Getsel()
Local $content = GuiCtrlRead($edit)
Local $sel = _GUICtrlEdit_GetSel(GuiCtrlGetHandle($edit))
Local $get = StringMid($content, $sel[0]+1, $sel[1]-$sel[0])
For $i = 0 to 2
If GuiCtrlRead($array[$i][0]) = "" Then
GuiCtrlSetData($array[$i][0], $get)
Exitloop
EndIf
Next
EndFunc
Func _Space($hWnd, $Msg, $wParam, $lParam) ;WM_COMMAND
#forceref $hWnd, $Msg, $lParam
For $i = 0 to 2
Switch BitAND($wParam, 0x0000FFFF)
Case $array[$i][1]
GuiCtrlSetData($array[$i][1], StringReplace(GuiCtrlRead($array[$i][1]), " ", "¶"))
EndSwitch
Next
Return 'GUI_RUNDEFMSG'
EndFunc
Func _Rename($replace)
Local $new, $txt = "", $save = ""
For $i = 0 to 2
$array[$i][2] = GuiCtrlRead($array[$i][0])
$array[$i][3] = StringReplace(GuiCtrlRead($array[$i][1]), "¶", " ")
Next
If $treat = "" Then
$treat = StringRegExp(GuiCtrlRead($edit), '(?-i)[^\r\n]+', 3)
If not IsArray($treat) Then Return
EndIf
For $i = 0 to UBound($treat)-1
If FileExists($workingdir & "\" & $treat[$i]) Then
$new = $treat[$i]
For $j = 0 to 2
If $array[$j][2] <> "" Then
If $regex Then
$new = StringRegExpReplace($new, $array[$j][2], $array[$j][3])
Else
$new = StringReplace($new, $array[$j][2], $array[$j][3], 0, 1)
EndIf
EndIf
Next
If $replace Then
$save &= $treat[$i] & "#" & $new & @crlf
;; FileMove($workingdir & "\" & $treat[$i], $workingdir & "\" & $new)
RunWait(@ComSpec & ' /C REN "' & $treat[$i] & '" "' & $new & '"', $workingdir, @SW_HIDE)
EndIf
$txt &= $new & @crlf
EndIf
Next
_Refresh($workingdir)
WinActivate($gui)
GuiCtrlSetData($edit, $txt)
If $replace Then
If BitAnd(GUICtrlRead($log_box), $GUI_CHECKED) = $GUI_CHECKED Then
$times += 1
GUICtrlSetState($btn_undo, $GUI_ENABLE)
GUICtrlSetData($btn_undo, "Undo " & $times)
Local $file = FileOpen($logfile, 1)
FileWrite($file, $times & @crlf)
FileWrite($file, $save)
FileClose($file)
EndIf
$treat = ""
EndIf
EndFunc
Func _Refresh($folder)
Local $hwnd = WinGetHandle(StringRegExpReplace($folder, '.+\\(.+)', "$1"))
If $hwnd = "" Then Return
;DllCall("user32.dll", "long", "SendMessage", "hwnd", $hwnd, "int", 0x111, "int", 28931, "int", 0)
DllCall("user32.dll", "long", "SendMessage", "hwnd", $hwnd, "int", 0x111, "int", 41504, "int", 0)
EndFunc
Func _Refresh2($folder)
Local $win = StringRegExpReplace($folder , '.+\\(.+)', "$1")
Local $oShell = ObjCreate("shell.application")
Local $oShellWindows = $oShell.windows
If IsObj($oShellWindows) Then
For $Window In $oShellWindows
If $Window.LocationName == $win Then $Window.Refresh
Next
EndIf
EndFunc
Func _UnDo()
Local $txt = FileRead($logfile), $oldtxt = ""
Local $lines = StringRegExp($txt, '(?-i)[^\r\n]+', 3)
For $i1 = 0 to UBound($lines)-1
If $lines[$i1] = $times Then Exitloop
$oldtxt &= $lines[$i1] & @crlf
Next
For $i2 = $i1+1 to UBound($lines)-1
$items = StringSplit($lines[$i2], "#")
$oldname = $items[1]
$newname = $items[2]
;; FileMove($workingdir & "\" & $newname, $workingdir & "\" & $oldname)
RunWait(@ComSpec & ' /C REN "' & $newname & '" "' & $oldname & '"', $workingdir, @SW_HIDE)
Next
_Refresh($workingdir)
If $oldtxt = "" Then
FileDelete($logfile)
GUICtrlSetState($btn_undo, $GUI_DISABLE)
Else
Local $file = FileOpen($logfile, 2)
FileWrite($file, $oldtxt)
FileClose($file)
EndIf
_Reset()
EndFunc
Func _Reset()
Local $ext = GuiCtrlRead($input_ext)
If $ext = "" Then $ext = "*"
Local $list = _FileListToArray($workingdir, "*." & $ext, 1)
If @error Then Return
If $list <> "" Then
Local $txt = ""
For $i = 1 to $list[0]
If $list[$i]<>"save.log" Then $txt &= $list[$i] & @crlf
Next
GuiCtrlSetData($edit, $txt)
EndIf
For $i = 0 to 2
GUICtrlSetData($array[$i][0], "")
GUICtrlSetData($array[$i][1], "")
Next
If FileExists($logfile) Then
Local $txt = FileRead($logfile)
Local $res = StringRegExp($txt, '(?m)(^\d{1,2})\s', 3)
$times = $res[Ubound($res)-1]
GUICtrlSetData($btn_undo, "Undo " & $times)
Else
GUICtrlSetData($btn_undo, "Undo ")
GUICtrlSetState($btn_undo, $GUI_DISABLE)
$times = 0
EndIf
;msgbox(0,"", $times)
EndFunc
Func _FileListToArray($sPath, $sFilter = "*", $iFlag = 0)
Local $hSearch, $sFile, $sFileList, $sDelim = "|"
$sPath = StringRegExpReplace($sPath, "[\\/]+\z", "") & "\" ; ensure single trailing backslash
If Not FileExists($sPath) Then Return SetError(1, 1, "")
If StringRegExp($sFilter, "[\\/:><\|]|(?s)\A\s*\z") Then Return SetError(2, 2, "")
If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 3, "")
$hSearch = FileFindFirstFile($sPath & $sFilter)
If @error Then Return SetError(4, 4, "")
While 1
$sFile = FileFindNextFile($hSearch)
If @error Then ExitLoop
If ($iFlag + @extended = 2) Then ContinueLoop
$sFileList &= $sDelim & $sFile
WEnd
FileClose($hSearch)
If Not $sFileList Then Return SetError(4, 4, "")
Return StringSplit(StringTrimLeft($sFileList, 1), "|")
EndFunc;==>_FileListToArray
Func _GUICtrlEdit_GetSel($hWnd)
Local $aSel[2]
Local $wparam = DllStructCreate("uint Start")
Local $lparam = DllStructCreate("uint End")
_SendMessage($hWnd, $EM_GETSEL, DllStructGetPtr($wparam), DllStructGetPtr($lparam), 0, "ptr", "ptr")
$aSel[0] = DllStructGetData($wparam, "Start")
$aSel[1] = DllStructGetData($lparam, "End")
Return $aSel
EndFunc ;==>_GUICtrlEdit_GetSel