Jump to content

delete lines in text file


Recommended Posts

i know that theres a way to replace strings in files

but is there a way to DELETE lines containing "some text...." where "..." can be anything

Hi gcue, try this

$filename = "testdel.txt"
$text = FileRead($filename)
$replace = "some text"
$text = StringRegExpReplace($text,"\h*\Q" & $replace & "\E.*\cM\cJ","")
$hf = FileOpen($filename,2)
FileWrite($hf,$text)

EDIT:Corrected so does not leave an empty line.

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

try this:

#Include <File.au3>
Global $success = False
$file_name = FileOpenDialog("Select file", @ScriptDir, "All files (*.*)", 1+4)
$line_text_input = InputBox("Line's text", "Line must contain following text:", "line contains this text")
$file_count_lines = _FileCountLines($file_name)
for $i = 0 to $file_count_lines
    $Lines_text_output = FileReadLine($file_name, $i)
    if StringInStr($Lines_text_output, $line_text_input) then
        _FileWriteToLine($file_name, $i, "", 1)
        $success = True
        ExitLoop
    EndIf
Next
if $success = True Then
    MsgBox(0, "Success", "Line has been deleted")
Else
    MsgBox(0, "Failure", "Line wasn't found")
EndIf
Link to comment
Share on other sites

hey martin =)

that works but it still leaves a blank line.

Hi gcue, try this

$filename = "testdel.txt"
$text = FileRead($filename)
$text = StringRegExpReplace($text,"\v\h*\Qsome text\E.*","")
$hf = FileOpen($filename,2)
FileWrite($hf,$text)
Link to comment
Share on other sites

sandin that works great.. im trying to repeat it for all instances and predefine the line to replace

heres what i have..

not sure how i can loop it so it will replace all instances

#Include <File.au3>

Global $success = False

$filename = "C:\CRS\Tools\Account_Info\gxm_novell.txt"

$line_text_input = "Subject:"

$file_count_lines = _FileCountLines($filename)

for $i = 0 to $file_count_lines

$Lines_text_output = FileReadLine($filename, $i)

if StringInStr($Lines_text_output, $line_text_input) then

_FileWriteToLine($filename, $i, "", 1)

$success = True

ExitLoop

EndIf

Next

ShellExecute($filename)

try this:

#Include <File.au3>
Global $success = False
$file_name = FileOpenDialog("Select file", @ScriptDir, "All files (*.*)", 1+4)
$line_text_input = InputBox("Line's text", "Line must contain following text:", "line contains this text")
$file_count_lines = _FileCountLines($file_name)
for $i = 0 to $file_count_lines
    $Lines_text_output = FileReadLine($file_name, $i)
    if StringInStr($Lines_text_output, $line_text_input) then
        _FileWriteToLine($file_name, $i, "", 1)
        $success = True
        ExitLoop
    EndIf
Next
if $success = True Then
    MsgBox(0, "Success", "Line has been deleted")
Else
    MsgBox(0, "Failure", "Line wasn't found")
EndIf
Link to comment
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
 Share

  • Recently Browsing   0 members

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