Jump to content

Need help deleting a line


Go to solution Solved by sahsanu,

Recommended Posts

Posted

Hi I need help with a command

I want to delete first line fom a notepad

I need only the command

Func DeleteLine()

If $text = $text Then

Here come te function to delete the first line from notepad(don't want to make the line blank I want to delete it because I want the secound line to become first line)

EndIf

EndFunc

I need only to delete the first line if this function is called

Thx in advance

Posted (edited)

Read the file with fileread(), you can get the first line by using stringleft and stringinstr:

$text = stringtrimleft( $text , stringinstr( $text , @cr ) )

and then do with the string what you will. I'm not sure if you need to search for @cr or @lf, try it outĀ 

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

  • Solution
Posted

You could also use arrays and rewrite the file from the second line, something like this:

#include <File.au3>
$textfile = "textfile.txt"
Local $aRecords
If Not _FileReadToArray($textfile, $aRecords) Then
    ConsoleWrite("Error reading file to Array:" & @error)
    Exit
EndIf
If $aRecords[0] = 1 Then
    ConsoleWrite("Mmmmm there is just 1 line on file... exiting" & @CRLF)
    Exit
EndIf
Local $hFile = FileOpen($textfile, 2) ; 2 = overwrite
    _FileWriteFromArray($hFile, $aRecords, 2)
FileClose($hFile)
Posted

The example of this _DeleteLine function deletes the first line in Notepad, or other single lines if specified.

Opt("WinTitleMatchMode", -2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase

If WinExists(" - Notepad") = 0 Then Run("notepad.exe")
WinActivate(" - Notepad", "")
Local $hWin = WinWaitActive(" - Notepad", "")
ControlSetText(" - Notepad", "", "Edit1", "line 1" & @CRLF & "line 2" & @CRLF & "line 3" & @CRLF & "line 4")

MsgBox(16, "Message", "First line of notepad ia about to deleted.", 4, $hWin)

_DeleteLine()

MsgBox(48, "**** Message ****", "First line of notepad has been deleted.", 4, $hWin)

;Tidy up
WinClose("[Handle:" & $hWin & "]", "")


; Default deletes first line in Notepad.
Func _DeleteLine($iLine = 1)
    If WinExists(" - Notepad") Then
        $sText = WinGetText(" - Notepad", "")
        $sText = StringRegExpReplace($sText, "^((\V+\v+){" & ($iLine - 1) & "})(\V+\v+)", "\1")
        ControlSetText(" - Notepad", "", "Edit1", $sText)
    EndIf
    Return
EndFunc   ;==>_DeleteLine
Posted (edited)

$string = ControlGetText("[CLASS:Notepad]", "", "[CLASS:Edit; INSTANCE:1]")
$line = 3
$string = StringReplace( $string, StringMid( $string, StringInStr( $string, @CRLF, 0, $line-1 )+1, (StringInStr( $string, @CRLF, 0, $line)-StringInStr( $string, @CRLF, 0, $line-1)) ) , "")
ControlSetText("[CLASS:Notepad]", "", "[CLASS:Edit; INSTANCE:1]", $string)

Amazing what you can so in so short a script :D

or as a func

Func StringRemoveNthLine($string,$n)
    Return StringReplace( $string, StringMid( $string, StringInStr( $string, @CRLF, 0, $n-1 )+1, (StringInStr( $string, @CRLF, 0, $n)-StringInStr( $string, @CRLF, 0, $n-1)) ) , "")
EndFunc

@Malkey Nice! I was just thinking "Boy I bet I could do this with Regular expressions" and noticed that's what you did. Yours is probably the most efficient one

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...