_FileWriteToLine Problem

  • Hi,
    entweder hab ich grad nen Brett vorm Kopf oder ich komm einfach nicht auf die Lösung.
    Eigentlich möchte ich nur in einem Script die _FileWriteToLine funktion nutzen, aber es klappt einfach nicht.
    Ich habe die Funktion bereits in anderen Scripts erfolgreich verwendet, aber bei diesem komme ich nicht weiter.

    Was macht das Script: Es liest die Daten aus einer .php Datei in eine Liste, da kann man diese bearbeiten und die Funktion _writeData soll diese wieder in die Datei einfügen.
    Ich habe mir mal eine MSG Box eingebaut, die mir @error, den Inhalt von $i und die Anzahl der Zeilen ausgibt.
    @error ist 1, also: File has less lines than $iLine
    $i ist 26 und die Zeilenzahl 54
    Demnach verstehe ich das Problem jetzt nicht so ganz ?(
    Ich bin für jede Hilfe dankbar ;)

    [autoit]

    #include <File.au3>
    #include <Array.au3>
    #include <MsgBoxConstants.au3>
    #include "Forms\gui.isf"
    ; -- Created with ISN Form Studio 2 for ISN AutoIt Studio -- ;
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #Include <GuiButton.au3>
    #include <ListViewConstants.au3>
    #include <GuiListView.au3>
    #include <FTPEx.au3>
    Global $aArray
    Global $start
    Global $ende
    $gui = GUICreate("Update Events",722,523,-1,-1,-1,-1)
    $btn_hoch = GUICtrlCreateButton("Hoch",588,401,100,30,-1,-1)
    $btn_runter = GUICtrlCreateButton("Runter",588,450,100,30,-1,-1)
    Global $liste = GUICtrlCreatelistview("Titel |Text ",40,190,534,309,-1,512)
    $inp_titel = GUICtrlCreateInput("",80,57,290,21,-1,512)
    GUICtrlCreateLabel("Titel:",50,60,26,15,-1,-1)
    GUICtrlSetBkColor(-1,"-2")
    GUICtrlCreateLabel("Text:",50,100,24,15,-1,-1)
    GUICtrlSetBkColor(-1,"-2")
    GUICtrlSetStyle($liste, $LVS_REPORT, $LVS_EX_FULLROWSELECT)
    $inp_text = GUICtrlCreateInput("",80,97,289,20,-1,512)
    $btn_add = GUICtrlCreateButton("Hinzufügen",420,75,100,30,-1,-1)
    $btn_upload = GUICtrlCreateButton("Upload",590,140,100,30,-1,-1)
    $btn_del = GUICtrlCreateButton("Löschen",589,320,100,30,-1,-1)
    GUISetState(@SW_SHOW,$gui)

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

    _importData ()
    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    Case $btn_add
    GUICtrlCreateListViewItem(GUICtrlRead($inp_titel) & "|" & GUICtrlRead($inp_text), $liste)
    Case $btn_del
    _GUICtrlListView_DeleteItemsSelected($liste)
    Case $btn_runter
    _GUICtrlListView_MoveItems($liste,1)
    Case $btn_hoch
    _GUICtrlListView_MoveItems($liste,-1)
    Case $btn_upload
    _writeData ()

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

    EndSwitch
    Sleep(10)
    WEnd

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

    Func _importData()
    _FileReadToArray(@ScriptDir& "\events.php", $aArray)
    If @error Then
    MsgBox(0, "", "Fehler beim einlesen der Datei. Fehler: " & @error)
    Exit
    EndIf
    ; Start und Ende finden
    $start = _ArraySearch($aArray, "<!--Start-->",0, 0, 0, 1)
    If @error Then
    MsgBox(0, "", "Fehler beim der Suche nach dem Startpunk. Fehler: " & @error)
    Exit
    EndIf
    $ende = _ArraySearch($aArray, "<!--Ende-->",0, 0, 0, 1)
    If @error Then
    MsgBox(0, "", "Fehler beim der Suche nach dem Endpunk. Fehler: " & @error)
    Exit
    EndIf
    For $i = $start To $ende
    If stringinstr($aArray[$i], '<p class="pcont2">') Then
    $tmp = StringTrimLeft ($aArray[$i], 18)
    $tmp = StringTrimRight($tmp, 10)
    $tmp2 = StringTrimRight($aArray[$i+1], 6)
    GUICtrlCreateListViewItem($tmp & "|" & $tmp2, $liste)
    Endif
    Next
    EndFunc

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

    Func _writeData ()
    Local $asplit
    Local $file2 = "events.php"
    $delanzahl = ($ende) - ($start+1)
    For $i = 1 to $delanzahl
    _FileWriteToLine($file2, $start+1, "", 1)
    If @error Then
    MsgBox(0, "", "Fehler beim löschen der Zeilen in der Datei. Fehler: " & @error)
    Exit
    EndIf
    Next
    Local $itemc = _GUICtrlListView_GetItemCount ($liste)
    $start2 = $start+1
    For $i = 1 To $itemc
    $tmp = _GUICtrlListView_GetItemTextString ($liste, $i-1)
    $asplit = StringSplit($tmp, "|")
    _FileWriteToLine($file2, $start2, '<p class="pcont2">' & $asplit[1] & '</p><br />', 0)
    If @error Then
    MsgBox(0, "", "Fehler beim schreiben in die Datei. Fehler: " & @error)
    Exit
    EndIf
    $start2 +=1
    _FileWriteToLine($file2, $start2, $asplit[2] & '<br />', 0)
    If @error Then
    MsgBox(0, "", "Fehler beim schreiben in die Datei. Fehler: " & @error)
    Exit
    EndIf
    $start2 +=1
    _FileWriteToLine($file2, $start2, '<hr class="hr1" /> ', 0)
    If @error Then
    MsgBox(0, "", "Fehler beim schreiben in die Datei. Fehler: " & @error)
    Exit
    EndIf
    $start2 +=1
    Next

    EndFunc
    ;===============================================================================
    ; Function Name: _GUICtrlListView_MoveItems()
    ; Description: Moves Up or Down selected item(s) in ListView.
    ;
    ; Parameter(s): $hListView - ControlID or Handle of ListView control.
    ; $iDirection - Define in what direction item(s) will move:
    ; -1 - Move Up.
    ; 1 - Move Down.
    ;
    ; Requirement(s): AutoIt 3.3.0.0
    ;
    ; Return Value(s): On seccess - Move selected item(s) Up/Down and return 1.
    ; On failure - Return "" (empty string) and set @error as following:
    ; 1 - No selected item(s).
    ; 2 - $iDirection is wrong value (not 1 and not -1).
    ; 3 - Item(s) can not be moved, reached last/first item.
    ;
    ; Note(s): * If you select like 15-20 (or more) items, moving them can take a while (second or two).
    ;
    ; Author(s): G.Sandler a.k.a CreatoR
    ;===============================================================================
    Func _GUICtrlListView_MoveItems($hListView, $iDirection)
    Local $aSelected_Indices = _GUICtrlListView_GetSelectedIndices($hListView, 1)

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

    If UBound($aSelected_Indices) < 2 Then Return SetError(1, 0, "")
    If $iDirection <> 1 And $iDirection <> -1 Then Return SetError(2, 0, "")

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

    Local $iTotal_Items = _GUICtrlListView_GetItemCount($hListView)
    Local $iTotal_Columns = _GUICtrlListView_GetColumnCount($hListView)

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

    Local $iUbound = UBound($aSelected_Indices)-1, $iNum = 1, $iStep = 1

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

    Local $iCurrent_Index, $iUpDown_Index, $sCurrent_ItemText, $sUpDown_ItemText
    Local $iCurrent_Index, $iCurrent_CheckedState, $iUpDown_CheckedState
    Local $iImage_Current_Index, $iImage_UpDown_Index

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

    If ($iDirection = -1 And $aSelected_Indices[1] = 0) Or _
    ($iDirection = 1 And $aSelected_Indices[$iUbound] = $iTotal_Items-1) Then Return SetError(3, 0, "")

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

    ControlListView($hListView, "", "", "SelectClear")

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

    If $iDirection = 1 Then
    $iNum = $iUbound
    $iUbound = 1
    $iStep = -1
    EndIf

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

    For $i = $iNum To $iUbound Step $iStep
    $iCurrent_Index = $aSelected_Indices[$i]
    $iUpDown_Index = $aSelected_Indices[$i]+1
    If $iDirection = -1 Then $iUpDown_Index = $aSelected_Indices[$i]-1

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

    $iCurrent_CheckedState = _GUICtrlListView_GetItemChecked($hListView, $iCurrent_Index)
    $iUpDown_CheckedState = _GUICtrlListView_GetItemChecked($hListView, $iUpDown_Index)

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

    _GUICtrlListView_SetItemSelected($hListView, $iUpDown_Index)

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

    For $j = 0 To $iTotal_Columns-1
    $sCurrent_ItemText = _GUICtrlListView_GetItemText($hListView, $iCurrent_Index, $j)
    $sUpDown_ItemText = _GUICtrlListView_GetItemText($hListView, $iUpDown_Index, $j)

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

    If _GUICtrlListView_GetImageList($hListView, 1) <> 0 Then
    $iImage_Current_Index = _GUICtrlListView_GetItemImage($hListView, $iCurrent_Index, $j)
    $iImage_UpDown_Index = _GUICtrlListView_GetItemImage($hListView, $iUpDown_Index, $j)

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

    _GUICtrlListView_SetItemImage($hListView, $iCurrent_Index, $iImage_UpDown_Index, $j)
    _GUICtrlListView_SetItemImage($hListView, $iUpDown_Index, $iImage_Current_Index, $j)
    EndIf

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

    _GUICtrlListView_SetItemText($hListView, $iUpDown_Index, $sCurrent_ItemText, $j)
    _GUICtrlListView_SetItemText($hListView, $iCurrent_Index, $sUpDown_ItemText, $j)
    Next

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

    _GUICtrlListView_SetItemChecked($hListView, $iUpDown_Index, $iCurrent_CheckedState)
    _GUICtrlListView_SetItemChecked($hListView, $iCurrent_Index, $iUpDown_CheckedState)

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

    _GUICtrlListView_SetItemSelected($hListView, $iUpDown_Index, 0)
    Next

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

    For $i = 1 To UBound($aSelected_Indices)-1
    $iUpDown_Index = $aSelected_Indices[$i]+1
    If $iDirection = -1 Then $iUpDown_Index = $aSelected_Indices[$i]-1
    _GUICtrlListView_SetItemSelected($hListView, $iUpDown_Index)
    Next

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

    Return 1
    EndFunc

    [/autoit]

    events.php

    3 Mal editiert, zuletzt von Neklor (14. Februar 2014 um 00:47)

  • Die Funktion _FileWriteToLine löscht alle Zeilen von $start bis $ende wenn du einen leeren String zum einfügen angibst. Die Zeilenanzahl nimmt also ab während das Script die _write Funktion ausführt. Irgendwann ist dann $i größer als die Zeilenanzahl der Datei.
    Interessanterweise tritt der Fehler bei mir aber mit deinen zur Verfügung gestellten Dateien nicht auf, wenn ich einfach nach dem öffnen des Programms auf "Upload" drücke.

  • In der Funktion _writeData() probiere mal diese Änderung:

    Spoiler anzeigen
    [autoit]

    $delanzahl = ($ende-1) - ($start+1)
    For $i = 1 to $delanzahl
    _FileWriteToLine($file2, $start+1, "", 1) ;so sollte es funktionieren
    MsgBox(0, "Test", @error & " " & $i & " " & _FileCountLines($file2))
    Next

    [/autoit]
  • Vielen Dank, jetzt klappt es auch mit dem Löschen der Zeilen ;)

    Zu dem Problem mit dem seltsamen Fehler bei FileWriteToLine: Das Problem lag offensichtlich and der AutoIt version 3.3.8.1. Ich habe jetzt aktualisiert und der Fehler tritt nicht mehr auf. :D

    Seltsamerweise erhalte ich jetzt aber Fehler, wenn ich mein Script mit der Beta Version ausführe, die ich auch aktualisert habe. Mit der normalen Version geht alles :S

    Das Script oben habe ich aktualisiert und ohne Leerzeichen im Austauschbaren Teil in der php Datei, funktioniert es dann auch.