Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search and replace FSO error

Status
Not open for further replies.

benniesanders

Programmer
Jan 20, 2002
199
US
Greetings, I am *trying* to use the following code to search for and replace a phone number in a gazillion files:
Code:
<% 
  Dim objFSO, objFolder 
  Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 
  Set objFolder = objFSO.GetFolder("d:\inetpub\[URL unfurl="true"]wwwroot\myfolder")[/URL] 
  
  Dim objFile 


  For Each objFile in objFolder.Files 
    ' Do something to the file

  Dim objTextStream 
  Dim strContent 
   
  Set objTextStream = objFile.OpenAsTextStream(ForReading) 
    strContent = objTextStream.ReadAll() 
  Call objTextStream.Close() 
   
  Set objTextStream = Nothing
   
  strContent = Replace(strContent,"old phone number", "new phone number") 
   
  Set objTextStream = objFile.OpenAsTextStream(ForWriting) 
    Call objTextStream.Write(strContent) 
  Call objTextStream.Close() 
   
  Set objTextStream = Nothing 
  Next'objFile  
%>
however, when I run this, I am getting the following error:

Invalid procedure call or argument on line 18

Line 18 is:

Set objTextStream = objFile.OpenAsTextStream(ForReading)

I am not smart enough to figure this out myself. I would appreciate any thoughts or better ideas or is this even the correct way to do this... Many thanks in advance for your help.
 
try this:
Code:
...
...
Set objTextStream = objFile.OpenAsTextStream(1)
...
...
Set objTextStream = objFile.OpenAsTextStream(2)
...
...

-DNG
 
Thanks for that. At least it's moved past that. Now I'm getting this error:

Input past end of file

on this line: strContent = objTextStream.ReadAll()

Waddya think?
 
Nevermind. I found another code snippet and it's working properly and not nearly as complicated:
Code:
Dim txt
Dim fso
Dim MyFile
Dim myfilename

Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.OpenTextFile("d:\inetpub\[URL unfurl="true"]wwwroot\"[/URL] & myfilename, 1)
    txt = MyFile.ReadAll
    MyFile.Close
Set MyFile = Nothing

txt = replace(txt, "old phone number", "new phone number")

'then write again into the text file.
Set MyFile = fso.OpenTextFile("d:\inetpub\[URL unfurl="true"]wwwroot\"[/URL] & myfilename, 2, True)
    MyFile.WriteLine(txt)
MyFile.Close
Set MyFile = Nothing 
Set fso = nothing

I can't thank you enough for taking the time to help me. Many thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top