469,898 Members | 1,537 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

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

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
4 17024
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
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
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
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.

Similar topics

25 posts views Thread by Neo Geshel | last post: by
5 posts views Thread by peter | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.