StringReplace über Config-Datei steuern

  • Hallo zusammen,

    ich bin jetzt ein Tool am programmieren, mit dem ich die Dateiname umbenennen kann.
    So weit bin ich schon gekommen:

    Spoiler anzeigen
    [autoit]

    #Region ### START Koda GUI section ### Form=
    Local $Form1 = GUICreate("Rename", 390, 70, -1, -1)
    Local $Label1 = GUICtrlCreateLabel("Verzeichnis:", 10, 12, 60, 12)
    Local $Input1 = GUICtrlCreateInput("", 80, 10, 280, 20)
    Local $Button1 = GUICtrlCreateButton("...", 364, 10, 20, 20, 0)
    Local $Label2 = GUICtrlCreateLabel("", 10, 47, 200, 16)
    Local $Button2 = GUICtrlCreateButton("Ersetze", 220, 45, 80, 20, 0)
    GUICtrlSetState($Button2, $GUI_DEFBUTTON)
    Local $Button3 = GUICtrlCreateButton("Ende", 304, 45, 80, 20, 0)

    [/autoit] [autoit][/autoit] [autoit]

    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $Button2
    GUICtrlSetState($Button2, $GUI_DISABLE)
    GUICtrlSetState($Button3, $GUI_DISABLE)
    $search = FileFindFirstFile(GUICtrlRead($Input1) & "\*.*")
    If $search = -1 Then
    MsgBox(262160, "Fehler", "Keine Dateien gefunden !!!")
    Exit
    EndIf
    While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
    $newfile = StringReplace($file, " ", "_")
    $newfile = StringReplace($newfile, "§", "p")
    $newfile = StringReplace($newfile, "ü", "ue")
    $newfile = StringReplace($newfile, "Ü", "Ue")
    $newfile = StringReplace($newfile, "ö", "oe")
    $newfile = StringReplace($newfile, "Ö", "Oe")
    $newfile = StringReplace($newfile, "ä", "ae")
    $newfile = StringReplace($newfile, "Ä", "Ae")
    $newfile = StringReplace($newfile, "ß", "ss")
    GUICtrlSetData($Label2, $newfile)
    FileMove(GUICtrlRead($Input1) & '\' & $file, GUICtrlRead($Input1) & '\' & $newfile, 0)
    WEnd
    FileClose($search)
    GUICtrlSetState($Button2, $GUI_ENABLE)
    GUICtrlSetState($Button3, $GUI_ENABLE)
    Case $Button1
    $l = FileSelectFolder("Verzeichnisauswahl:", @ScriptDir, 1)
    GUICtrlSetData($Input1, $l)
    Case $Button3
    Exit
    Case $GUI_EVENT_CLOSE
    Exit

    [/autoit] [autoit][/autoit] [autoit]

    EndSwitch
    WEnd

    [/autoit]


    Jetzt möchte ich die StringReplace-Anweisungen über ein Config-Datei steuern.
    Hat jemand eine Idee wie ich das angehen kann?

    Die INI sieht so aus:

    [autoit]

    [Replace]
    =_
    §=p
    ü=ue
    Ü=Ue
    ö=oe
    Ö=Oe
    ä=ae
    Ä=Ae
    ß=ss

    [/autoit]


    und wird vom Script auch gefunden.

    Einmal editiert, zuletzt von xoma (5. Juli 2007 um 16:52)

  • Hallo zusammen,

    danke für die Antworten gute Ideen.
    Folgendes habe umgesetzt:

    Spoiler anzeigen
    [autoit]

    #include <GUIConstants.au3>
    #include <File.au3>
    #include <Array.au3>

    [/autoit] [autoit][/autoit] [autoit]

    #NoTrayIcon
    dim $newfile, $file

    [/autoit] [autoit][/autoit] [autoit]

    #Region ### START Koda GUI section ### Form=
    Local $Form1 = GUICreate("Sonderzeichen in Dateinamen ersetzen.", 390, 70, -1, -1)
    Local $Label1 = GUICtrlCreateLabel("XML-Dir:", 10, 12, 40, 12)
    Local $Input1 = GUICtrlCreateInput("", 60, 10, 300, 20)
    GUICtrlSetData($Input1, '')
    Local $Button1 = GUICtrlCreateButton("...", 364, 10, 20, 20, 0)
    Local $Label2 = GUICtrlCreateLabel("", 10, 47, 200, 16)
    Local $Button2 = GUICtrlCreateButton("Ersetze", 220, 45, 80, 20, 0)
    GUICtrlSetState($Button2, $GUI_DEFBUTTON)
    Local $Button3 = GUICtrlCreateButton("Ende", 304, 45, 80, 20, 0)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $Button2
    GUICtrlSetState($Button2, $GUI_DISABLE)
    GUICtrlSetState($Button3, $GUI_DISABLE)
    $dir = _FileListToArray (GUICtrlRead($Input1), '*.*')
    $var = IniReadSection(@ScriptDir & "\rename.ini", "Replace")
    While UBound($dir) > 0
    $file = _ArrayPop($dir)
    $newfile = $file

    ;~ Hier funktioniert es nicht ???
    For $i = 1 To $var[0][0]
    $newfile = StringReplace($newfile, $var[$i][0], $var[$i][1])
    GUICtrlSetData($Label2, $newfile)
    Next

    [/autoit] [autoit][/autoit] [autoit]

    ;~ Hier geht es !!!
    ;~ $newfile = StringReplace($newfile, " ", "_")
    ;~ $newfile = StringReplace($newfile, "§", "p")
    ;~ $newfile = StringReplace($newfile, "ü", "ue")
    ;~ $newfile = StringReplace($newfile, "Ü", "Ue")
    ;~ $newfile = StringReplace($newfile, "ö", "oe")
    ;~ $newfile = StringReplace($newfile, "Ö", "Oe")
    ;~ $newfile = StringReplace($newfile, "ä", "ae")
    ;~ $newfile = StringReplace($newfile, "Ä", "Ae")
    ;~ $newfile = StringReplace($newfile, "ß", "ss")

    If UBound ($dir) = 0 Then
    GUICtrlSetData($Label2, 'Fertig !!!')
    Else
    GUICtrlSetData($Label2, $newfile)
    EndIf
    FileMove(GUICtrlRead($Input1) & '\' & $file, GUICtrlRead($Input1) & '\' & $newfile, 0)
    WEnd
    GUICtrlSetState($Button2, $GUI_ENABLE)
    GUICtrlSetState($Button3, $GUI_ENABLE)
    Case $Button1
    $l = FileSelectFolder("Wählen Sie ein Verzeichnis aus:", @ScriptDir, 2, GUICtrlRead($Input1))
    If $l <> '' Then
    GUICtrlSetData($Input1, $l)
    EndIf
    Case $Button3
    Exit
    Case $GUI_EVENT_CLOSE
    Exit

    [/autoit] [autoit][/autoit] [autoit]

    EndSwitch
    WEnd

    [/autoit]

    Wenn ich mit der For-Schleife über Array versuche läuft er durch, macht aber nichts.
    Wo habe ich den Fehler ?(

  • Hi,

    ich glaube den Fehler gefunden zuhaben aber ist habe keine Ahnung wie ich den beheben soll.

    Problem:

    [autoit]

    While UBound($dir) > 0
    $file = _ArrayPop($dir)
    $newfile = $file
    ;~ hier ist newfile mit dem Dateinamen belegt

    ;~ Hier funktioniert es nicht ???
    For $i = 1 To $var[0][0]
    ;~ hier ist newfile leer
    $newfile = StringReplace($newfile, $var[$i][0], $var[$i][1])
    GUICtrlSetData($Label2, $newfile)
    Next

    [/autoit]


    Die Variable $newfile hat in der For-Schleife keinen Wert, vor der Schleife ist diese aber belegt.
    Wie kommt das ?( Was tun ?(

    Einmal editiert, zuletzt von xoma (5. Juli 2007 um 17:22)

  • das ist sehr lieb von dir.
    Ich würde aber auch gerne verstehen wo mein Fehler liegt.

  • ich suche schon die ganze Zeit aber vor lauter Bäume sehe ich den Wald nicht mehr.
    Ich bleibe natürlich dran.

  • teste dies mal ... damit du wieder aus dem Wald herausfindest :D

    Spoiler anzeigen
    [autoit]

    #include <GUIConstants.au3>
    #include <File.au3>
    #include <Array.au3>

    #NoTrayIcon
    dim $newfile, $file

    #Region ### START Koda GUI section ### Form=
    Local $Form1 = GUICreate("Sonderzeichen in Dateinamen ersetzen.", 390, 70, -1, -1)
    Local $Label1 = GUICtrlCreateLabel("XML-Dir:", 10, 12, 40, 12)
    Local $Input1 = GUICtrlCreateInput("", 60, 10, 300, 20)
    GUICtrlSetData($Input1, '')
    Local $Button1 = GUICtrlCreateButton("...", 364, 10, 20, 20, 0)
    Local $Label2 = GUICtrlCreateLabel("", 10, 47, 200, 16)
    Local $Button2 = GUICtrlCreateButton("Ersetze", 220, 45, 80, 20, 0)
    GUICtrlSetState($Button2, $GUI_DEFBUTTON)
    Local $Button3 = GUICtrlCreateButton("Ende", 304, 45, 80, 20, 0)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $Button2
    GUICtrlSetState($Button2, $GUI_DISABLE)
    GUICtrlSetState($Button3, $GUI_DISABLE)
    $dir = _FileListToArray (GUICtrlRead($Input1), '*.*')
    $var = IniReadSection(@ScriptDir & "\rename.ini", "Replace")
    For $z = 1 to UBound($dir) -1
    $newfile = $dir[$z]
    For $i = 1 To UBound($var) -1
    If StringInStr($newfile, $var[$i][0]) Then $newfile = StringReplace($newfile, $var[$i][0], $var[$i][1])
    GUICtrlSetData($Label2, $newfile)
    Next
    If $z = UBound ($dir) -1 Then
    GUICtrlSetData($Label2, 'Fertig !!!')
    Else
    GUICtrlSetData($Label2, $newfile)
    EndIf
    FileMove(GUICtrlRead($Input1) & '\' & $dir[$z], GUICtrlRead($Input1) & '\' & $newfile, 0)
    Next

    GUICtrlSetState($Button2, $GUI_ENABLE)
    GUICtrlSetState($Button3, $GUI_ENABLE)
    Case $Button1
    $l = FileSelectFolder("Wählen Sie ein Verzeichnis aus:", @ScriptDir, 2, GUICtrlRead($Input1))
    If $l <> '' Then
    GUICtrlSetData($Input1, $l)
    EndIf
    Case $Button3
    Exit
    Case $GUI_EVENT_CLOSE
    Exit

    EndSwitch
    WEnd

    [/autoit]
  • Hi,

    DANKE es funkt.
    Jetzt gibt es eine Kleinigkeit: Ich möchte die Leerzeichen mit dem Unterstrich ersetzen.
    Das habe ich in der INI auch drin, aber das Leerzeichen wird nicht ersetzt.
    Woran kann es liegen?

  • Hm, also warum es so ist kann ich dir nicht erklären, denke halt, dass er Leerzeichen überliest (vielleicht ?()

    Habs so gelöst.

    Spoiler anzeigen
    [autoit]

    #include <GUIConstants.au3>
    #include <File.au3>
    #include <Array.au3>

    #NoTrayIcon
    dim $newfile, $file

    #Region ### START Koda GUI section ### Form=
    Local $Form1 = GUICreate("Sonderzeichen in Dateinamen ersetzen.", 390, 70, -1, -1)
    Local $Label1 = GUICtrlCreateLabel("XML-Dir:", 10, 12, 40, 12)
    Local $Input1 = GUICtrlCreateInput("", 60, 10, 300, 20)
    GUICtrlSetData($Input1, '')
    Local $Button1 = GUICtrlCreateButton("...", 364, 10, 20, 20, 0)
    Local $Label2 = GUICtrlCreateLabel("", 10, 47, 200, 16)
    Local $Button2 = GUICtrlCreateButton("Ersetze", 220, 45, 80, 20, 0)
    GUICtrlSetState($Button2, $GUI_DEFBUTTON)
    Local $Button3 = GUICtrlCreateButton("Ende", 304, 45, 80, 20, 0)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $Button2
    GUICtrlSetState($Button2, $GUI_DISABLE)
    GUICtrlSetState($Button3, $GUI_DISABLE)
    $dir = _FileListToArray (GUICtrlRead($Input1), '*.*')
    $var = IniReadSection(@ScriptDir & "\rename.ini", "Replace")
    For $z = 1 to UBound($dir) -1
    $newfile = $dir[$z]
    For $i = 1 To UBound($var) -1
    If StringInStr($var[$i][0], '"') Then $var[$i][0] = StringReplace($var[$i][0], '"', '')
    If StringInStr($newfile, $var[$i][0]) Then $newfile = StringReplace($newfile, $var[$i][0], $var[$i][1])
    GUICtrlSetData($Label2, $newfile)
    Next
    If $z = UBound ($dir) -1 Then
    GUICtrlSetData($Label2, 'Fertig !!!')
    Else
    GUICtrlSetData($Label2, $newfile)
    EndIf
    FileMove(GUICtrlRead($Input1) & '\' & $dir[$z], GUICtrlRead($Input1) & '\' & $newfile, 0)
    Next

    GUICtrlSetState($Button2, $GUI_ENABLE)
    GUICtrlSetState($Button3, $GUI_ENABLE)
    Case $Button1
    $l = FileSelectFolder("Wählen Sie ein Verzeichnis aus:", @ScriptDir, 2, GUICtrlRead($Input1))
    If $l <> '' Then
    GUICtrlSetData($Input1, $l)
    EndIf
    Case $Button3
    Exit
    Case $GUI_EVENT_CLOSE
    Exit

    EndSwitch
    WEnd

    [/autoit]

    In der ini muß du aus dem Leerzeichen dies machen

    [autoit]

    " "

    [/autoit]