|
-
November 1st, 2004, 01:58 AM
#7
Re: search,read,write & replace in a textfile
Here is the fastest way I have found to do what you want:
Code:
Dim A$, S$()
'pull content in all at once. care should be taken
'to ensure not to try to read in too large a file
'note that Binary will create the file if it does not exist
Open "C:\test.txt" For Binary Access Read As #1
A = Space$(LOF(1))
Get #1, , A
Close #1
'split contents to an array
S = Split(A, vbCrLf)
'edit contents here
'you can use the Filter() function to filter out certain strings very quickly
'for example to remove any line containing "hello", you can use
'S = Filter(S, "hello", 0, 1)
'join the array to a single string
A = Join(S, vbCrLf)
'you can also open for Binary Access Write and use Put #1, , A, but this method
'does not resize the file if the string is shorter, so you need to use Kill to delete
'the original beforehand, or write to a new file. The use of ";" after the string
'prevents a vbCrLf at the end of the file.
Open "C:\test.txt" For Output As #1
Print #1, A;
Close #1
This post may also be of interest to you:
http://www.codeguru.com/forum/showth...537#post975537
Please remember to rate the posts and threads that you find useful.
How can something be both new and improved at the same time?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|