Jump to content

Deleting a line of text


Recommended Posts

I'm trying to write of script that will Open a text file and then go through it looking for a certain value for that line. If it finds that value then I need to it delete that line out of the text file and continue through the file until the end. I have some code that seems to do everything...but I can't figure out how to delete the lines.

I'm attaching the code I've tried so far. I maybe doing this the wrong way....any ideas?

$file = FileOpen("c:\file.txt", 1)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $result = StringInStr($line, "PHYSICIAN",0)
If $result <> 0 then    
    StringReplace($line," ","-")
    StringStripWS($line,8) oÝ÷ ØÚ-²êÞ­¶hêÞ¶¬Ì!z·ßÙ¢wî±çZºÚ"µÍB[YÙ[[PÛÜÙJ ÌÍÙ[JB
Edited by Agent Orange
Link to comment
Share on other sites

Well you have to write to the file again :shocked:...

In your case I would suggest looking at this example

#include<Array.au3>
#include<File.au3>
Dim $a_Test
; Read file into array
_FileReadToArray("test.txt",$a_Test)
; reverse records
_ArrayReverse($a_Test,1)
; write reversed array to file
_FileWriteFromArray("test2.txt",$a_Test,1)

The "problem" is that you cant read and write to a file.. you open a file to read, then you close, then you open it again and write...

Cheers

Start here if you are new Valuater's AutoIT 1-2-3Looking for an UDF - Look hereDo you need to do it twice - Autoit

Link to comment
Share on other sites

Read the file to an array with _FileReadToArray(). Then step backwards through the array deleting lines that contain your target string. Use _ArrayDelete() to delete the entries in the array. Walking the array in reverse keeps any deleted entries from changing the next one to be done. When you're done, write it back with _FileWriteFromArray().

:shocked:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...