﻿#include <Array.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <WindowsConstants.au3>
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Version=beta
#AutoIt3Wrapper_Icon=cerebrum_.ico
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Res_Language=1031
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****


#RequireAdmin
#NoTrayIcon

$wintitle = "automatum reparare oraculum"
$version = "v 1.0.0.0"
$name = "© 2012 -Bazii-"
$date = ""
$time_ = ""

If WinExists($wintitle) Then
	WinActivate($wintitle, "")
	Exit
EndIf

#Region ### START Koda GUI section ### Form=c:\users\bobles\documents\!text-ersetzer\form1.kxf
$Version1 = GUICreate($wintitle, 613, 496, -1, -1, BitOR($GUI_SS_DEFAULT_GUI,$WS_SIZEBOX,$WS_THICKFRAME))
$StatusBar1 = _GUICtrlStatusBar_Create($Version1)
$Edit1 = GUICtrlCreateEdit("", 0, 88, 609, 153)
GUICtrlSetData(-1, "(Suchtext)")
$Edit2 = GUICtrlCreateEdit("", 0, 280, 609, 153)
GUICtrlSetData(-1, "(Zieltext)")
$Button1 = GUICtrlCreateButton("Beenden", 536, 8, 73, 33)
$Button2 = GUICtrlCreateButton("Sicherung", 456, 8, 73, 33)
$Button3 = GUICtrlCreateButton("Start", 350, 9, 97, 33)
;$Tab1 = GUICtrlCreateTab(0, 64, 81, 25)
;$TabSheet1 = GUICtrlCreateTabItem("Freie Version")
GUICtrlCreateTabItem("")
$Label1 = GUICtrlCreateLabel("Text-Regenerator", 8, 10, 243, 28)
GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")
$Label2 = GUICtrlCreateLabel($name, 25, 448, 225, 17)
$Label3 = GUICtrlCreateLabel($version, 550, 448, 74, 17)
$TE_txt = GUICtrlCreateCheckbox(" txt", 80, 65, 40, 20)
$TE_html = GUICtrlCreateCheckbox(" html", 120, 65, 40, 20)
$TE_htm = GUICtrlCreateCheckbox(" htm", 170, 65, 40, 20)
$TE_php = GUICtrlCreateCheckbox(" php", 220, 65, 40, 20)
$TE_srt = GUICtrlCreateCheckbox(" srt", 270, 65, 40, 20)
$TE_ini = GUICtrlCreateCheckbox(" ini", 320, 65, 40, 20)
$TE_inf = GUICtrlCreateCheckbox(" inf", 370, 65, 40, 20)
$TE_reg = GUICtrlCreateCheckbox(" reg", 420, 65, 40, 20)
$TE_xml = GUICtrlCreateCheckbox(" xml", 470, 65, 40, 20)
$TE_bat = GUICtrlCreateCheckbox(" bat", 520, 65, 40, 20)
$TE_cfg = GUICtrlCreateCheckbox(" cfg", 570, 65, 40, 20)

$button4 = GUICtrlCreateButton("Quell Ordner", 500, 435, 34, 34, $BS_ICON)
GUICtrlSetImage(-1, "shell32.dll", 4)
$Input1 = GUICtrlCreateInput("Kein Pfad angegeben!", 210, 440, 220, 20)

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

#Region --- CodeWizard generated code Start ---
;MsgBox features: Title=Yes, Text=Yes, Buttons=OK, Icon=Info, Modality=Task Modal, Miscellaneous=Top-most attribute
MsgBox(270400,"Information","Texte werden ohne Rückfrage ersetzt. Weiter auf eigene Gefahr!")
#EndRegion --- CodeWizard generated code End ---


$Erweiterung = ""
Global $Wert1, $Wert2, $path, $sRootPath

SteuerungZeit()

Func SteuerungZeit()
	Global $date = @YEAR & "-" & @MON & "-" & @MDAY
	Global $time_ = @HOUR & "." & @MIN & "." & @SEC
EndFunc   ;==>SteuerungZeit

Func BerechnenvonWerten()
	$Wert1 = GUICtrlRead($Edit1, 1)
	$Wert2 = GUICtrlRead($Edit2, 1)
EndFunc   ;==>BerechnenvonWerten


While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			Exit
		Case $Button1
			Exit
		Case $Edit1
			BerechnenvonWerten()
		Case $Edit2
			BerechnenvonWerten()
		Case $Button3
			Beginn()
		Case $Button2
			Sicherung()
		Case $Button4
			pfad()

	EndSwitch
WEnd


Func Sicherung()
	$path = @ScriptDir & "\Sicherung_" & $date & "_" & $time_
	FileCopy(@ScriptDir & "\*.srt", $path & "\*.*", 9)
	FileCopy(@ScriptDir & "\*.ini", $path & "\*.*", 9)
	FileCopy(@ScriptDir & "\*.7z", $path & "\*.*", 9)
	FileCopy(@ScriptDir & "\*.txt", $path & "\*.*", 9)
	FileCopy(@ScriptDir & "\*.html", $path & "\*.*", 9)
	FileCopy(@ScriptDir & "\*.htm", $path & "\*.*", 9)
	FileCopy(@ScriptDir & "\*.php", $path & "\*.*", 9)
	FileCopy(@ScriptDir & "\*.srt", $path & "\*.*", 9)
	FileCopy(@ScriptDir & "\*.ini", $path & "\*.*", 9)
	FileCopy(@ScriptDir & "\*.inf", $path & "\*.*", 9)
	FileCopy(@ScriptDir & "\*.reg", $path & "\*.*", 9)
	FileCopy(@ScriptDir & "\*.xml", $path & "\*.*", 9)
	FileCopy(@ScriptDir & "\*.cfg", $path & "\*.*", 9)

	_GUICtrlStatusBar_SetText($StatusBar1, " Sicherung erfolgreich erstellt", 0)
	SteuerungZeit()
EndFunc   ;==>Sicherung

Func Beginn()
If (GUICtrlRead($TE_txt) ) = $GUI_CHECKED Then Call("Start", "txt")
If (GUICtrlRead($TE_html) ) = $GUI_CHECKED Then Call("Start", "html")
If (GUICtrlRead($TE_htm) ) = $GUI_CHECKED Then Call("Start", "htm")
If (GUICtrlRead($TE_php) ) = $GUI_CHECKED Then Call("Start", "php")
If (GUICtrlRead($TE_srt) ) = $GUI_CHECKED Then Call("Start", "srt")
If (GUICtrlRead($TE_ini) ) = $GUI_CHECKED Then Call("Start", "ini")
If (GUICtrlRead($TE_inf) ) = $GUI_CHECKED Then Call("Start", "inf")
If (GUICtrlRead($TE_reg) ) = $GUI_CHECKED Then Call("Start", "reg")
If (GUICtrlRead($TE_xml) ) = $GUI_CHECKED Then Call("Start", "xml")
If (GUICtrlRead($TE_bat) ) = $GUI_CHECKED Then Call("Start", "bat")
If (GUICtrlRead($TE_cfg) ) = $GUI_CHECKED Then Call("Start", "cfg")
EndFunc   ;==>Beginn

Func Start($Erweiterung)
	BerechnenvonWerten()
	$sPath = GUICtrlRead($Input1)
	If $sPath = "Kein Pfad angegeben!" Then
		$sPath = @ScriptDir
		GUICtrlSetData($Input1, $sPath)
	Else
	EndIf
	MsgBox(270400,"Information",$sPath)
	$array = _GetFilesFolder_Rekursiv($sPath, $Erweiterung)
	For $i = 1 To $array[0]
		umbenennen($array[$i])
	Next
EndFunc   ;==>Start

Func umbenennen($Suche_file)

	$path = @ScriptDir & "\Sicherung\alt_" & $date & "_" & $time_
	$LeseFile = FileRead($Suche_file)
	$controll_String = StringInStr($LeseFile, $Wert1)
	If $controll_String > 0 Then
		_GUICtrlStatusBar_SetText($StatusBar1, " " & $Suche_file, 0)
		FileCopy(@ScriptDir & "\" & $Suche_file, $path & "\*.*", 9)
		$LeseFile = StringReplace($LeseFile, $Wert1, $Wert2, "", 1)
		$Datei_Start = FileOpen($Suche_file, 258)
		FileWrite($Datei_Start, $LeseFile)
		FileClose($Datei_Start)
	EndIf
	_GUICtrlStatusBar_SetText($StatusBar1, "Der Text wurde erfolgreich ausgetauscht", 0)
	SteuerungZeit()
EndFunc   ;==>umbenennen

$ret = _GetFilesFolder_Rekursiv('.', 'au3',0,1)
If IsArray($ret) Then
    _ArrayDisplay($ret, 'Rekursiv alle au3-Dateien')
Else
    ConsoleWrite($ret & @CRLF)
EndIf

;==================================================================================================
; Function Name:   _GetFilesFolder_Rekursiv($sPath [, $sExt='*' [, $iDir=-1 [, $iRetType=0 ,[$sDelim='0']]]])
; Description:     Rekursive Auflistung von Dateien und/oder Ordnern
; Parameter(s):    $sPath     der Basispfad für die Auflistung ('.' -aktueller Pfad, '..' -Parentpfad)
;                  $sExt      Erweiterung für Dateiauswahl '*' oder -1 für alle (Standard)
;                  $iDir      -1 Dateien+Ordner(Standard), 0 nur Dateien, 1 nur Ordner
;      optional:   $iRetType  0 gibt Array, 1 gibt String zurück
;      optional:   $sDelim    legt Trennzeichen für Stringrückgabe fest
;                             0 -@CRLF (Standard)  1 -@CR  2 -@LF  3 -';'  4 -'|'
; Return Value(s): Array (Standard) od. String mit den gefundenen Pfaden der Dateien und/oder Ordner
;                  Array[0] enthält die Anzahl der gefundenen Dateien/Ordner
; Author(s):       BugFix (bugfix@autoit.de)
;==================================================================================================
Func _GetFilesFolder_Rekursiv($sPath, $sExt, $iDir=0, $iRetType=0, $sDelim='0')
    Global $oFSO = ObjCreate('Scripting.FileSystemObject')
    Global $strFiles = ''
    Switch $sDelim
        Case '1'
            $sDelim = @CR
        Case '2'
            $sDelim = @LF
        Case '3'
            $sDelim = ';'
        Case '4'
            $sDelim = '|'
        Case Else
            $sDelim = @CRLF
    EndSwitch
    If ($iRetType < 0) Or ($iRetType > 1) Then $iRetType = 0
    If $sExt = -1 Then $sExt = '*'
    If ($iDir < -1) Or ($iDir > 1) Then $iDir = -1
    _ShowSubFolders($oFSO.GetFolder($sPath),$sExt,$iDir,$sDelim)
    If $iRetType = 0 Then
        Local $aOut
        $aOut = StringSplit(StringTrimRight($strFiles, StringLen($sDelim)), $sDelim, 1)
        If $aOut[1] = '' Then
            ReDim $aOut[1]
            $aOut[0] = 0
        EndIf
        Return $aOut
    Else
        Return StringTrimRight($strFiles, StringLen($sDelim))
    EndIf
EndFunc

Func _ShowSubFolders($Folder, $Ext='*', $Dir=-1, $Delim=@CRLF)
    If Not IsDeclared("strFiles") Then Global $strFiles = ''
    If ($Dir = -1) Or ($Dir = 0) Then
        For $file In $Folder.Files
            If $Ext <> '*' Then
                If StringRight($file.Name, StringLen($Ext)) = $Ext Then _
                    $strFiles &= $file.Path & $Delim
            Else
                $strFiles &= $file.Path & $Delim
            EndIf
        Next
    EndIf
    For $Subfolder In $Folder.SubFolders
        If ($Dir = -1) Or ($Dir = 1) Then $strFiles &= $Subfolder.Path & '\' & $Delim
        _ShowSubFolders($Subfolder, $Ext, $Dir, $Delim)
    Next
EndFunc

Func pfad()
	GUISETSTATE(@SW_HIDE)
	Global $pfad = FileSelectFolder("Wähle einen Ordner aus.", "::{00000000-1080-F9E5-6311-4162E05A6BEE}", 4)
	If FileExists($pfad) Then
		GUICtrlSetData($Input1, $pfad)
	EndIf
	GUISETSTATE(@SW_SHOW)
EndFunc   ;==>pfad