Sign in to follow this  
Followers 0
Dangerous

File line delete

4 posts in this topic

#1 ·  Posted (edited)

Hi guys i have a probelm with deleting a line in a file, so here is my script:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>
#include <file.au3>
#include <array.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Site Blocker", 398, 351, 192, 124)
$List1 = GUICtrlCreateList("", 8, 48, 161, 253)
$Input1 = GUICtrlCreateInput("", 8, 16, 161, 21)
$List2 = GUICtrlCreateList("", 200, 48, 161, 253)
$BegLine = _LineNumsOfSearchStr(@ScriptDir & "Bock.txt", "127.0.0.1")
$FileLNumber = _FileCountLines(@ScriptDir & "Bock.txt")
For $i = $BegLine to $FileLNumber Step +1
$AddLine = FileReadLine(@ScriptDir & "Bock.txt", $i)
GUICtrlSetData($List2, $AddLine & "|")
Next
$Input2 = GUICtrlCreateInput("", 200, 16, 161, 21)
$Button1 = GUICtrlCreateButton("Block", 40, 312, 75, 25)
$Button2 = GUICtrlCreateButton("DeBlock", 240, 312, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $Button1
   $SiteText = GUICtrlRead($Input1)
   If $SiteText <> "" Then
    GUICtrlSetData($List1, $SiteText & "|")
    $LineNumber = _FileCountLines(@ScriptDir & "Bock.txt")
    _FileWriteToLine(@ScriptDir & "Bock.txt", $LineNumber,"127.0.0.1" & " " & $SiteText)
    GUICtrlSetData($Input1, "")
   Else
    Sleep(1000)
   EndIf
  Case $Button2
   $DeSite = GUICtrlRead($List2)
   _GUICtrlListBox_DeleteString($List2, _GUICtrlListBox_GetCaretIndex($List2))
   $LineDeleteNumber = _LineNumsOfSearchStr(@ScriptDir & "Bock.txt", $DeSite, True)
   ;_FileWriteToLine(@ScriptDir & "Bock.txt", $LineDeleteNumber, "", 1)
  Case $GUI_EVENT_CLOSE
   Exit
EndSwitch
WEnd

Func _LineNumsOfSearchStr($sFileName, $searchString, $bDeleteLine = False)
    Local $location, $aCurrentLineNum, $iCurrentLineNum, $sFile, $iOccur = 1, $sRes = ""
    If FileExists($sFileName) = 0 Then Return 1
        $sFile = FileRead($sFileName)
        $location = StringInStr($sFile, $searchString, 0, $iOccur) ; Find the $iOccur occurrence of the "substring"
        If $location > 0 Then
            $aCurrentLineNum = StringRegExp(StringRegExpReplace($sFile, "(?s)(.{" & $location & "})(.*)$", "1"), "(?s)(v+)", 3) ;Find line number
            $iCurrentLineNum = UBound($aCurrentLineNum) + 1 ; Line number
            If $bDeleteLine Then
                _FileWriteToLine($sFileName, $iCurrentLineNum, "", 1) ; Remove found line from file.
            Else
                $iOccur += 1
            EndIf
        Else
        EndIf
        Sleep(10)
    ;ShellExecute($sFileName)
    Return $iCurrentLineNum
EndFunc   ;==>_LineNumsOfSearchStr

any idea why this won't work?:

Case $Button2
   $DeSite = GUICtrlRead($List2)
   _GUICtrlListBox_DeleteString($List2, _GUICtrlListBox_GetCaretIndex($List2))
   $LineDeleteNumber = _LineNumsOfSearchStr(@ScriptDir & "Bock.txt", $DeSite, True)
   ;_FileWriteToLine(@ScriptDir & "Bock.txt", $LineDeleteNumber, "", 1)

Thanks

Edited by Dangerous

Share this post


Link to post
Share on other sites



bump

Share this post


Link to post
Share on other sites

I just checked. It does work o_O

Everything works fine in your script...

Delete ";" at the start of the line:

;_FileWriteToLine(@ScriptDir & "Bock.txt", $LineDeleteNumber, "", 1)
and everything will work...

[indent=3][/indent]

Share this post


Link to post
Share on other sites

I just checked. It does work o_O

Everything works fine in your script...

Delete ";" at the start of the line:

;_FileWriteToLine(@ScriptDir & "\Bock.txt", $LineDeleteNumber, "", 1)
and everything will work...

Oh... what a fool I am i could not notice I had it in comment, thanks ;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0