G
Guest
Is there a way to search and replace a piece of text in a text file from an
Access module?
Any help is appreciated. thanks!
Access module?
Any help is appreciated. thanks!
The safe way to do this with pure VBA would be:
- Open the file for input
- Open a temp file for output
- InputLine
- Replace()
- Print #
- Loop until EOF
- Close both
- Kill the orginal
- Name the 2nd as the first.
Peter said:But God help you if the Search and/or Replacement strings may include
1 or more CRLFs!!
Bas Cost Budde said:All except for that God doesn't write VBA. At least I never see Him
post on these fora...
Seriously, the InputLine part would need revision in that case.
At worst, read the whole text file into a String variable in one gulp
(if it's not too big), and then just replace within that variable and
write it out again.
Peter R. Fletcher said:The "if it's not too big" is potentially a very big "if".
The textbook way to do this, assuming that you do need to handle
included CRLFs and/or other control characters, _and_ that you need to
handle files of arbitrary (i.e. large!) size, would probably be to
open the file as binary, read it in fixed-length chunks, and
"double-buffer" the input in such a way that matches would be
guaranteed to be found even if they fall across block margins. The
details of the most efficient way of doing this would depend on your
scanning algorithm.
Dirk said:Fortunately, text files don't tend to be arbitrarily large.
The "if it's not too big" is potentially a very big "if".