Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Copy Only Certain Lines in Text File and Delete File? 1

Status
Not open for further replies.

KellyStee

Technical User
Jul 30, 2001
106
US
In Access, I need to replace the first line of a text file. It seems the only way I can do this is to add the line I want to be the replacement (the new line) to a NEW file, copy all of the lines in the original file to the new file, and re-name the new file as the original file. This leaves me with two things I can't resolve: how do you delete the temporary file and how do I delete the SECOND line in the "now original" file (which is the line I wanted to replace initially but it got copied when I copied the text from the original file into the new file).
Here's the code I'm currently working with:

Print #1, strInput
Open strFileName For Input As #2
Do While Not EOF(2) ' Loop until end of file.
Line Input #2, TextLine ' Read line into variable.
Print #1, TextLine
Loop
Close #1
Close #2
SourceFile = strNewFileName
DestinationFile = strFileName
FileCopy SourceFile, DestinationFile

Thanks in advance!!
Kelly
 
The kill(filename) command will delete the file for you.

Then depending on your situation for replacing or deleting a line of text try one of the following:

strFlag = "Text you are looking to exclude"
Do While Not EOF(2) ' Loop until end of file.
Line Input #2, TextLine ' Read line into variable.
If TextLine =strFlag then
'Do Nothing or Print new line of text
Else
Print #1, TextLine
End If
Loop


If you are always deleting a particular line you can also put in a line counter and skip over the print statement for just that line


intLineNum = 1
Do While Not EOF(2) ' Loop until end of file.
Line Input #2, TextLine ' Read line into variable.
If intLineNum = 2 then
'Do Nothing
Else
Print #1, TextLine
End If
intLineNum = intLineNum + 1
Loop


Hope this helps
 
bkclaw113,
Thanks so much!!! You solved both my issues and saved the day!!! :eek:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top