Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > File I/O and Registry > deleting specific lines from text files


Reply
 
Thread Tools Display Modes
  #1  
Old 04-26-2003, 12:44 PM
Sidewinder21 Sidewinder21 is offline
Newcomer
 
Join Date: Apr 2003
Location: dont call me boris!
Posts: 18
Unhappy deleting specific lines from text files


lo again,

what i need to do is search through a particularly short text file and upon finding a specific word at the beginning of a line, deleting the whole line entirely.
the specific word will be entered from a text box if that is of any help?

thanks a lot

sidewinder

---im a n00b, dont hurt me---
Reply With Quote
  #2  
Old 04-26-2003, 12:49 PM
Robse's Avatar
Robse Robse is offline
Senior Contributor

* Expert *
 
Join Date: Sep 2002
Location: Karlsruhe, Germany
Posts: 1,319
Default

Rather than deleting the line and thus modifying the file, think of doing
it in these steps:

1. Open file and read contents into a string variable
2. Remove the line from the string
3. Overwrite the old file (Or better, write new file, delete old file, rename new file)
__________________
Posting Guidelines MSDN-VB API List Use [vb]...[/vb] tags for code!
Reply With Quote
  #3  
Old 04-26-2003, 02:58 PM
Sidewinder21 Sidewinder21 is offline
Newcomer
 
Join Date: Apr 2003
Location: dont call me boris!
Posts: 18
Default

thats what i had in the first place, i just didnt use it properly
when i loaded the form it would put both whole text file into the listboxes, whereas i only wanted it to put one which was the modified one into them.
silly me.

cheers 4 the help
Reply With Quote
  #4  
Old 04-26-2003, 03:04 PM
loquin's Avatar
loquin loquin is offline
Google Hound

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Arizona, USA
Posts: 12,378
Default

You cannot read a text file and write to the same open text file. You can open it for input operations, or output(output or append) operations, but not as both.

So, you must read a line from the input file, check the line - if OK, write the line to the output file. Repeat until input file is empty.

Then, kill the old file & rename the new file with the old file name.
__________________
Lou
"I have my standards. They may be low, but I have them!" ~ Bette Middler
"It's a book about a Spanish guy called Manual. You should read it." ~ Dilbert
"To understand recursion, you must first understand recursion." ~ unknown
Reply With Quote
  #5  
Old 04-26-2003, 03:24 PM
Sidewinder21 Sidewinder21 is offline
Newcomer
 
Join Date: Apr 2003
Location: dont call me boris!
Posts: 18
Default

ok, ive just had a look at my program, and what i need it to do, and.... low and behold, it still isnt quite right

i have one text file, which has these contents:


"paterson , john","4356","58157","28181",43,"E","##","X","26/04/2003"
"cringle , chris","5467","58157","28181",54,"D","##","X","26/04/2003"
"cringle , chris","5467","58157","28181","54","D","65","D","26/04/2003"

(it could have any number of people in it)

the bottom record is where i have edited chris cringles (it was the 1st name that came into my head) data as the default for everybody in those fields is "##" and "X"

what i am trying to do is delete the record where his data holds "##" and "X"

-----------

if any more info. is needed, or anyone could give me a proper hand with it, my email should be somewhere in my profile, if not it is The_sidewinder__@hotmail.com

cheers.
matt
Reply With Quote
  #6  
Old 04-26-2003, 03:35 PM
Squirm's Avatar
Squirm Squirm is offline
Political Coder

Retired Moderator
* Guru *
 
Join Date: Mar 2001
Location: London, England
Posts: 8,037
Default

As has been said above, you need to completely rebuild the file, copying each
line in turn, except the lines you wish to omit from the output file.
__________________
Search the forums | Use [vb][/vb] tags | Still IRCing
Reply With Quote
  #7  
Old 04-26-2003, 03:45 PM
Sidewinder21 Sidewinder21 is offline
Newcomer
 
Join Date: Apr 2003
Location: dont call me boris!
Posts: 18
Default

ok cheers guys, thanks for the help.
should have it done in about an hour. ta v. much
Reply With Quote
  #8  
Old 04-26-2003, 04:39 PM
Sidewinder21 Sidewinder21 is offline
Newcomer
 
Join Date: Apr 2003
Location: dont call me boris!
Posts: 18
Default

ok ive got it all sorted but i still need ur help on the file killing/renaming

ive got the 2 files called
candidate details
report details


the report details are the new ones so i need them to be changed to candidate details and the candidate details need to be scrapped.
how is this done?

as i say n00b here so sorry for taking up all da space with easy questions

ta.
matt
Reply With Quote
  #9  
Old 04-26-2003, 04:43 PM
Squirm's Avatar
Squirm Squirm is offline
Political Coder

Retired Moderator
* Guru *
 
Join Date: Mar 2001
Location: London, England
Posts: 8,037
Default

Code:
Kill candidatedetailsfile Name reportdetailsfile As candidatedetailsfile
__________________
Search the forums | Use [vb][/vb] tags | Still IRCing
Reply With Quote
  #10  
Old 04-26-2003, 04:59 PM
Sidewinder21 Sidewinder21 is offline
Newcomer
 
Join Date: Apr 2003
Location: dont call me boris!
Posts: 18
Default

all i can say is i owe u guys.
cheers
matt
Reply With Quote
  #11  
Old 04-26-2003, 10:48 PM
unclebill unclebill is offline
Junior Contributor
 
Join Date: Apr 2003
Location: San Diego, CA
Posts: 301
Default

Quote:
Originally Posted by Sidewinder21
...i have one text file, which has these contents:


"paterson , john","4356","58157","28181",43,"E","##","X","26/04/2003"
"cringle , chris","5467","58157","28181",54,"D","##","X","26/04/2003"
"cringle , chris","5467","58157","28181","54","D","65","D","26/04/2003"

(it could have any number of people in it)
...if any more info. is needed, or anyone could give me a proper hand with it, my email should be somewhere in my profile, if not it is

cheers.
matt

If this is a good representation of what the file is designed to contain, I'd recommend changing it to a random acces file. The only thing that will differ significantly is that all of the names will be in equal length fields but I think the advantages outweigh that minor limitation. And while you are manipulating the name within VB, you can truncate spaces using the LTRIM$/RTRIM$ function.

The big advantage is you only need to read the data record you desire, modify it and write it back to the file replacing the existing data.

No need to deal with the copying record by record, modifying the desired one and rewriting a new file with the modified contents, then deleting the original file and renaming the new one to the old name.

It takes a bit of effort to set it up initially but makes the programming much easier in my estimation to support the file manipulations.

I just did a program that uses this method. At one site there are only a bit over 1,000 records (76 bytes per record) but the other site has over 4,000 records.

The only process that gets cumbersome with a random access file is deleting (actually removing) a record.
Reply With Quote
  #12  
Old 04-27-2003, 06:59 AM
Sidewinder21 Sidewinder21 is offline
Newcomer
 
Join Date: Apr 2003
Location: dont call me boris!
Posts: 18
Default

thanks for the idea bill, but the method already stated i have used and it works fine, this is only a small program that i have to do for my course and it has to be in tomorrow.
so changing it now would be a wasted effort as we are not needed to know random acess files yet.

i may need to ask more about them at a later date so be warned, i will be back!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading Text Files and Progress Bar Ashon General 13 05-05-2004 10:55 AM
Deleting specific text in a listbox bibiteinfo General 2 04-22-2003 08:44 PM
deleting two lines in a text file jlbryant General 1 02-27-2003 12:55 PM
Deleting lines from text file... Ice_Frog General 35 06-27-2002 05:49 PM
Required files burningodzilla General 11 09-12-2001 04:51 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->