 |

04-26-2003, 12:44 PM
|
|
Newcomer
|
|
Join Date: Apr 2003
Location: dont call me boris!
Posts: 18
|
|
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---
|
|

04-26-2003, 12:49 PM
|
 |
Senior Contributor
* Expert *
|
|
Join Date: Sep 2002
Location: Karlsruhe, Germany
Posts: 1,319
|
|
|
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)
|
|

04-26-2003, 02:58 PM
|
|
Newcomer
|
|
Join Date: Apr 2003
Location: dont call me boris!
Posts: 18
|
|
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
|
|

04-26-2003, 03:04 PM
|
 |
Google Hound
Retired Moderator * Guru *
|
|
Join Date: Nov 2001
Location: Arizona, USA
Posts: 12,378
|
|
|
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
|

04-26-2003, 03:24 PM
|
|
Newcomer
|
|
Join Date: Apr 2003
Location: dont call me boris!
Posts: 18
|
|
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
|
|

04-26-2003, 03:35 PM
|
 |
Political Coder
Retired Moderator * Guru *
|
|
Join Date: Mar 2001
Location: London, England
Posts: 8,037
|
|
|
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.
|
|

04-26-2003, 03:45 PM
|
|
Newcomer
|
|
Join Date: Apr 2003
Location: dont call me boris!
Posts: 18
|
|
|
ok cheers guys, thanks for the help.
should have it done in about an hour. ta v. much
|
|

04-26-2003, 04:39 PM
|
|
Newcomer
|
|
Join Date: Apr 2003
Location: dont call me boris!
Posts: 18
|
|
|
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
|
|

04-26-2003, 04:43 PM
|
 |
Political Coder
Retired Moderator * Guru *
|
|
Join Date: Mar 2001
Location: London, England
Posts: 8,037
|
|
Code:
Kill candidatedetailsfile
Name reportdetailsfile As candidatedetailsfile
|
|

04-26-2003, 04:59 PM
|
|
Newcomer
|
|
Join Date: Apr 2003
Location: dont call me boris!
Posts: 18
|
|
|
all i can say is i owe u guys.
cheers
matt
|
|

04-26-2003, 10:48 PM
|
|
Junior Contributor
|
|
Join Date: Apr 2003
Location: San Diego, CA
Posts: 301
|
|
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.
|
|

04-27-2003, 06:59 AM
|
|
Newcomer
|
|
Join Date: Apr 2003
Location: dont call me boris!
Posts: 18
|
|
|
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!
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|