Kleines Problem mit Array....

  • Also guten Tag erst mal...

    Da mein Kollege gerne mal ein bisschen was an Musik runterlädt und sich dabei zu tode nervt weil die Dateinamen immer Bodenstriche oder anderes unbrauchbares Zeug drin haben, hat er mich gefragt, ob ich ihm ein Programm zur korrektur dieser Dateinamen schreiben könnte...

    Dann erst mal der Code:

    Spoiler anzeigen
    [autoit]

    #Include <GuiListBox.au3>

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

    Dim $oFSO = ObjCreate('Scripting.FileSystemObject')
    Dim $search_files[1000]

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

    Opt("GUIOnEventMode", 1)

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

    $GUI_main = GUICreate("Chanah - by StengelProductions", 560, 500)
    $group_files = GUICtrlCreateGroup("Dateien", 20, 15, 300, 460)

    $list_files = _GUICtrlListBox_Create($GUI_main, "", 40, 45, 260, 400, $LBS_EXTENDEDSEL)
    $add_files = GUICtrlCreateButton("Hinzufügen", 40, 440, 110, 25)
    GUICtrlSetOnEvent(-1, "_add")
    $del_files = GUICtrlCreateButton("Liste Löschen", 190, 440, 110, 25)
    GUICtrlSetOnEvent(-1, "_del")

    $group_functions = GUICtrlCreateGroup("Aktionen", 340, 15, 200, 255)

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

    $bs_funktion = GUICtrlCreateButton("Bodenstriche entfernen", 360, 45, 160, 25)
    GUICtrlSetOnEvent(-1, "_bodenstriche_del")
    $dash_funktion = GUICtrlCreateButton("Bindestriche entfernen", 360, 80, 160, 25)
    GUICtrlSetOnEvent(-1 , "_bindestriche_del")
    GUICtrlCreateLabel("Zu ersetzen:", 360, 115, 150)
    $input_funktion = GUICtrlCreateInput("", 360, 135, 160, 25)
    GUICtrlCreateLabel("Mit:", 360, 170, 160, 25)
    $input_funktion1 = GUICtrlCreateInput("", 360, 190, 160, 25)
    $text_funktion = GUICtrlCreateButton("Bestimme Textstelle entfernen", 360, 235, 160, 25)
    GUICtrlSetOnEvent(-1, "_bestimmte_textstelle_del")

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

    GUISetOnEvent($GUI_EVENT_CLOSE, "_terminate")

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

    GUISetState()

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

    While 1
    Sleep(100)
    WEnd

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

    Func _add()
    $files = FileOpenDialog("Dateien Auswählen", "::{450D8FBA-AD25-11D0-98A8-0800361B1103}", "All Files (*.*)", 7, "", $GUI_main)
    If Not @error Then
    $search_files = StringSplit($files, "|", 1)
    If @error = 1 Then
    For $a = 1 To $search_files[0]
    $oFile = $oFSO.GetFile($search_files[$a])
    _GUICtrlListBox_AddString($list_files, $oFile.Name)
    Next
    Else
    For $a = 2 To $search_files[0]
    $oFile = $oFSO.GetFile($search_files[$a])
    _GUICtrlListBox_AddString($list_files, $oFile.Name)
    Next
    EndIf
    EndIf
    EndFunc

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

    Func _del()
    _GUICtrlListBox_ResetContent($list_files)
    EndFunc

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

    Func _bodenstriche_del()
    _GUICtrlListBox_BeginUpdate($list_files)
    $count = _GUICtrlListBox_GetCount($list_files)

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

    For $a = $count -1 To 0 Step -1
    $text = _GUICtrlListBox_GetText($list_files, $a)
    If $count = 1 Then
    $oFile = $oFSO.GetFile($search_files[$a + 1])
    Else
    $oFile = $oFSO.GetFile($search_files[$a + 2])
    EndIf
    $new_text = StringReplace($text, "_", " ")
    $string = $oFile.Path
    $file_dir = $string
    _GUICtrlListBox_DeleteString($list_files, $a)
    _GUICtrlListBox_AddString($list_files, $new_text)
    Do
    $file_dir = StringTrimRight($file_dir, 1)
    Until StringRight($file_dir, 1) = "\"
    FileMove($oFile.Path, $file_dir & $new_text, 1)
    Next
    _GUICtrlListBox_EndUpdate($list_files)
    EndFunc

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

    Func _bindestriche_del()
    _GUICtrlListBox_BeginUpdate($list_files)
    $count = _GUICtrlListBox_GetCount($list_files)

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

    For $a = $count -1 To 0 Step -1
    $text = _GUICtrlListBox_GetText($list_files, $a)
    If $count = 1 Then
    $oFile = $oFSO.GetFile($search_files[$a + 1])
    Else
    $oFile = $oFSO.GetFile($search_files[$a + 2])
    EndIf
    $new_text = StringReplace($text, "-", " ")
    $string = $oFile.Path
    $file_dir = $string
    _GUICtrlListBox_DeleteString($list_files, $a)
    _GUICtrlListBox_AddString($list_files, $new_text)
    Do
    $file_dir = StringTrimRight($file_dir, 1)
    Until StringRight($file_dir, 1) = "\"
    FileMove($oFile.Path, $file_dir & $new_text, 1)
    Next
    _GUICtrlListBox_EndUpdate($list_files)
    EndFunc

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

    Func _bestimmte_textstelle_del()
    _GUICtrlListBox_BeginUpdate($list_files)
    $count = _GUICtrlListBox_GetCount($list_files)

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

    For $a = $count -1 To 0 Step -1
    $text = _GUICtrlListBox_GetText($list_files, $a)
    If $count = 1 Then
    $oFile = $oFSO.GetFile($search_files[$a + 1])
    Else
    $oFile = $oFSO.GetFile($search_files[$a + 2])
    EndIf
    $new_text = StringReplace($text, GUICtrlRead($input_funktion), GUICtrlRead($input_funktion1))
    $string = $oFile.Path
    $file_dir = $string
    _GUICtrlListBox_DeleteString($list_files, $a)
    _GUICtrlListBox_AddString($list_files, $new_text)
    Do
    $file_dir = StringTrimRight($file_dir, 1)
    Until StringRight($file_dir, 1) = "\"
    FileMove($oFile.Path, $file_dir & $new_text, 1)
    Next
    _GUICtrlListBox_EndUpdate($list_files)
    EndFunc

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

    Func _terminate()
    Exit
    EndFunc

    [/autoit]

    Bei der ersten Korrektur der Dateinamen funktioniert alles perfekt.
    Aber beim zweiten mal kommt die Fehlermeldung in der Console:

    E:\Scripts\Edit\Dateinamen Ändern\Chanah.au3 (70) : ==> The requested action with this object has failed.:
    $oFile = $oFSO.GetFile($search_files[$a + 2])
    $oFile = $oFSO.GetFile($search_files[$a + 2])^ ERROR

    Weiss vielleicht jemand, warum diese Fehlermeldung erst beim 2. oder 3. mal beim Ausführen der Funktion kommt?

    Ich hab schon stundenlang "herumgepröbelt" und bin dabei auf keine perfekt funktionierende Lösung gekommen...

    Hoffe Ihr könnt mir helfen..

    MfG, Der Unwissende

    Einmal editiert, zuletzt von Unwissender (11. Juli 2008 um 15:30)

  • Moin,

    Du hast folgendes gemacht:
    1: Lade Dateien und Pfad in ein Array
    2: Ändere was und schreibe die Datei im Filesystem um

    Und jetzt kommt der kleine aber böse Fehler ;) :
    Die Datei die vorher z.B. "Lied1_01.mp3" war heißt nun im Filesystem z.B. "Lied101.mp3", aber in deinem $search_files Array lautet der Name immer noch "Lied1_01.mp3", Du musst die Bezeichnung da auch anpassen.

    Ich habe mal die eine Funktion angepasst, habe das Array Element umbenannt und _GUICtrlListBox_DeleteString($list_files, $a) sowie _GUICtrlListBox_AddString($list_files, $new_text) durch _GUICtrlListBox_ReplaceString($list_files, $a,$new_text) ersetzt:

    Beispiel
    [autoit]


    Func _bestimmte_textstelle_del()
    _GUICtrlListBox_BeginUpdate($list_files)
    $count = _GUICtrlListBox_GetCount($list_files)

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

    For $a = $count -1 To 0 Step -1
    $text = _GUICtrlListBox_GetText($list_files, $a)
    $new_text = StringReplace($text, GUICtrlRead($input_funktion), GUICtrlRead($input_funktion1))
    If $count = 1 Then
    $oFile = $oFSO.GetFile($search_files[$a + 1])
    $search_files[$a + 1] = $new_text
    Else
    $oFile = $oFSO.GetFile($search_files[$a + 2])
    $search_files[$a + 2] = $new_text
    EndIf
    $string = $oFile.Path
    $file_dir = $string
    _GUICtrlListBox_ReplaceString($list_files, $a,$new_text)
    Do
    $file_dir = StringTrimRight($file_dir, 1)
    Until StringRight($file_dir, 1) = "\"
    FileMove($oFile.Path, $file_dir & $new_text, 1)
    Next
    _GUICtrlListBox_EndUpdate($list_files)
    EndFunc

    [/autoit]

    Ich hab das Script noch etwas verkürzt, ist vielleicht übersichtlicher.

    Überarbeitete Version
    [autoit]


    #include <GuiListBox.au3>

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

    Dim $oFSO = ObjCreate('Scripting.FileSystemObject')
    Dim $search_files[1000]

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

    Opt("GUIOnEventMode", 1)

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

    $GUI_main = GUICreate("Chanah - by StengelProductions", 560, 500)
    $group_files = GUICtrlCreateGroup("Dateien", 20, 15, 300, 460)

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

    $list_files = _GUICtrlListBox_Create($GUI_main, "", 40, 45, 260, 400, $LBS_EXTENDEDSEL)
    $add_files = GUICtrlCreateButton("Hinzufügen", 40, 440, 110, 25)
    GUICtrlSetOnEvent(-1, "_add")
    $del_files = GUICtrlCreateButton("Liste Löschen", 190, 440, 110, 25)
    GUICtrlSetOnEvent(-1, "_del")

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

    $group_functions = GUICtrlCreateGroup("Aktionen", 340, 15, 200, 255)

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

    $bs_funktion = GUICtrlCreateButton("Bodenstriche entfernen", 360, 45, 160, 25)
    GUICtrlSetOnEvent(-1, "_RenameFiles")
    $dash_funktion = GUICtrlCreateButton("Bindestriche entfernen", 360, 80, 160, 25)
    GUICtrlSetOnEvent(-1, "_RenameFiles")
    GUICtrlCreateLabel("Zu ersetzen:", 360, 115, 150)
    $input_funktion = GUICtrlCreateInput("", 360, 135, 160, 25)
    GUICtrlCreateLabel("Mit:", 360, 170, 160, 25)
    $input_funktion1 = GUICtrlCreateInput("", 360, 190, 160, 25)
    $text_funktion = GUICtrlCreateButton("Bestimme Textstelle entfernen", 360, 235, 160, 25)
    GUICtrlSetOnEvent(-1, "_RenameFiles")

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

    GUISetOnEvent($GUI_EVENT_CLOSE, "_terminate")

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

    GUISetState()

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

    While 1
    Sleep(100)
    WEnd

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

    Func _add()
    $files = FileOpenDialog("Dateien Auswählen", "::{450D8FBA-AD25-11D0-98A8-0800361B1103}", "All Files (*.*)", 7, "", $GUI_main)
    If Not @error Then
    $search_files = StringSplit($files, "|", 1)
    If @error = 1 Then
    For $a = 1 To $search_files[0]
    $oFile = $oFSO.GetFile($search_files[$a])
    _GUICtrlListBox_AddString($list_files, $oFile.Name)
    Next
    Else
    For $a = 2 To $search_files[0]
    $oFile = $oFSO.GetFile($search_files[$a])
    _GUICtrlListBox_AddString($list_files, $oFile.Name)
    Next
    EndIf
    EndIf
    EndFunc ;==>_add

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

    Func _RenameFiles()
    Dim $searchFor, $replaceWith
    Switch @GUI_CtrlId
    Case $bs_funktion
    $searchFor = '_'
    $replaceWith = ' '
    Case $dash_funktion
    $searchFor = '-'
    $replaceWith = ' '
    Case $text_funktion
    $searchFor = GUICtrlRead($input_funktion)
    $replaceWith = GUICtrlRead($input_funktion1)
    EndSwitch
    _GUICtrlListBox_BeginUpdate($list_files)
    $count = _GUICtrlListBox_GetCount($list_files)

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

    For $a = $count - 1 To 0 Step -1
    $text = _GUICtrlListBox_GetText($list_files, $a)
    $new_text = StringReplace($text, $searchFor, $replaceWith)
    If $count = 1 Then
    $oFile = $oFSO.GetFile($search_files[$a + 1])
    $search_files[$a + 1] = $new_text
    Else
    $oFile = $oFSO.GetFile($search_files[$a + 2])
    $search_files[$a + 2] = $new_text
    EndIf
    $string = $oFile.Path
    $file_dir = $string
    _GUICtrlListBox_ReplaceString($list_files, $a, $new_text)
    Do
    $file_dir = StringTrimRight($file_dir, 1)
    Until StringRight($file_dir, 1) = "\"
    FileMove($oFile.Path, $file_dir & $new_text, 1)
    Next
    _GUICtrlListBox_EndUpdate($list_files)
    EndFunc ;==>_RenameFiles

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

    Func _del()
    _GUICtrlListBox_ResetContent($list_files)
    EndFunc ;==>_del

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

    Func _terminate()
    Exit
    EndFunc ;==>_terminate

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

    Mfg
    Jens (McPoldy)

    Twitter: jkroeger

    Denn die Dinge, die wir erst lernen müssen, bevor wir sie tun, lernen wir beim Tun.(Aristoteles)

    2 Mal editiert, zuletzt von McPoldy (11. Juli 2008 um 10:35)