Eamonn McEvoy
Eamonn McEvoy

Reputation: 8986

Find and replace multiple file in vbs

I am trying to implement a find and replace for all files in a folder using a vbs script, here is what I have so far

Dim fso,folder,files,oldFileContents,newFileContents,FIND,REPLACE

FIND = "textToBeReplaced"
REPLACE = "localhost"

Set fso = CreateObject("Scripting.FileSystemObject")

Set folder = fso.GetFolder("HTML")
Set files = folder.Files

For each item In files
    oldFileContents = GetFile(item.Path)
    newFileContents = replace(oldFileContents,FIND,REPLACE,1,-1,1)
    WriteFile FileName,newFileContents
Next

but when I try to run it I get and error, "Type Mismatch: 'GetFile'", what am I doing wrong?

Upvotes: 1

Views: 4489

Answers (2)

Skytunnel
Skytunnel

Reputation: 1083

You missed out the fso. on

oldFileContents = fso.GetFile(item.Path)

and

fso.WriteFile FileName,newFileContents

EDIT: as per discussion below, please note this answer was only meant to show where your error was occurring. It was assumed that your intent was to develop your code further once your got past this error, which if so, I’m sure you’ve already seen Ekkehard has provided some very helpful guidance on his answer.

Upvotes: 1

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

The problem should be solved with code like:

' Constants Mr Gates forgot (but cf. vbTextCompare)
  Const ForReading = 1
  Const ForWriting = 2
' Configuration constants  
  Const csFind     = "pdf"
  Const csRepl     = "puf"
' Dim & init for vars needed on *this* level   
  Dim oFS   : Set oFS = CreateObject("Scripting.FileSystemObject")
  Dim sTDir : sTDir   = oFS.GetAbsolutePathName("..\data\test")
  Dim oFile
  For Each oFile In oFS.GetFolder(sTDir).Files
      WScript.Echo "looking at", oFile.Name
      ' Dim & init for vars needed on *this* level   
      Dim sContent : sContent = goFS.GetFile(oFile.Path)
      ' For Skytunnels and other air-coders
      WScript.Echo "content is not", sContent
      ' you got oFile, so use it; no need for .GetFile()
      sContent = oFile.OpenAsTextStream(ForReading).ReadAll()
      WScript.Echo "qed! content is", sContent
      ' Replace(expression, find, replacewith[, start[, count[, compare]]])
      ' don't use magic numbers; vbTextCompare is even pre-defined
      sContent = Replace(sContent, csFind, csRepl, 1, -1, vbTextCompare)
      WScript.Echo "new content", sContent
      oFile.OpenAsTextStream(ForWriting).Write sContent
      sContent = oFile.OpenAsTextStream(ForReading).ReadAll()
      WScript.Echo "new content straight from file", sContent
      WScript.Echo "------------------"
  Next

output:

...
------------------
looking at 0000000000012345.20120302.pdf
content is not E:\trials\SoTrials\answers\9117277\data\test\0000000000012345.20120302.pdf
qed! content is This is the content of 0000000000012345.20120302.pdf

new content This is the content of 0000000000012345.20120302.puf

new content straight from file This is the content of 0000000000012345.20120302.puf

Important points:

  1. Don't use a Dim-all-vars-ever-used-line at the top of your script
  2. Avoid creation of unnecessary vars (folder, files, *contents), use the vars you have properly (item==oFile)
  3. .GetFile() returns a File object, not the file's content

Upvotes: 3

Related Questions