By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,827 Members | 2,277 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,827 IT Pros & Developers. It's quick & easy.

Find a string in a text file, replace it with space and save

P: n/a
Vic
Hi,
With VBA code from MSAccess, how to search for a string in another
text file, and replace it with space and then save it under the same
name? Your help is greatly appreciated.
Rgds
Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
li*****@uq.edu.au (Vic) wrote in message news:<f4**************************@posting.google. com>...
Hi,
With VBA code from MSAccess, how to search for a string in another
text file, and replace it with space and then save it under the same
name? Your help is greatly appreciated.
Rgds


A way to do it. I am sure there are others.

dim fso as object
dim txtStr as object
dim strHolder as string
dim strFileToFix as string

strFileToFix = "PATHTO FILE"

set fso = wscript.createobject("scripting.filesystemobject")

set txtStr = fso.opentextfile(strFileToFix,1,false,0)

strHolder = txtstr.readall

txtStr.close

set txtStr = nothing

strHolder = replace(strHolder,"string to find",space(0),1,-1,vbtextcompare)

set txtStr = fso.createtextfile(strFileToFix,true,false)

txtStr.write(strHolder)

txtStr.close

set txtStr = nothing
Nov 13 '05 #2

P: n/a
Thanks a lot. Is there is a way to search the file one line by one line?
It's actually a series of records, in each line, i need to find all d
with a comma behind and replace it with space. Then write it out.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #3

P: n/a
Vic Li <li*****@uq.edu.au> wrote in message news:<41**********************@news.newsgroups.ws> ...
Thanks a lot. Is there is a way to search the file one line by one line?
It's actually a series of records, in each line, i need to find all d
with a comma behind and replace it with space. Then write it out.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Try this:

dim fso as object
dim txtStr as object
dim strHolder as string
dim strFileToFix as string
dim strTemp

strFileToFix = "PATHTO FILE"

set fso = wscript.createobject("scripting.filesystemobject")

set txtStr = fso.opentextfile(strFileToFix,1,false,0)

do until txtStr.atendofstream

strTemp = txtStr.readline

strTemp = replace(strTemp,"string to
find",space(1),1,-1,vbtextcompare)

strHolder = strHolder & strTemp & vbcrlf 'not sure you need vbcrlf or
not i think you do

next

txtStr.close

set txtStr = nothing

set txtStr = fso.createtextfile(strFileToFix,true,false)

txtStr.write(strHolder)

txtStr.close

set txtStr = nothing

Just a side note. I would suggest you pick up a copty of
VBScript in a Nutshell published by O'Reilly. The first
edition is OK and is just a tremendous resource. No real need to get
the second edition if you want to be cheap. Dirt cheap used on the
internet.

Finally there was a bug in the first post should be space(1) not
space(0).
Nov 13 '05 #4

P: n/a
Hi all,
Thanks a lot. Works well now!! Great!!
Rgds,

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.