7

I am searching for a VBScript that does a search and replace in files (e.g. 1.txt 2.xml). I have file "1.txt" that inside there is the word "temporary" and I want to change it to "permanent". Because I get this file a lot I need a script for it.

Every time that I try to write a script that contains open a txt file and the command replace, it doesn't.

I found a script that change this file with another file and does the change inside, but this is not what I am looking for.

4
  • It is customary to thank the user who supplies the correct answer, by marking their answer as the accepted answer. Commented Jan 4, 2010 at 19:54
  • I have tested the script using a file with the .config extension containing the example text you provided in your comment, and I had no problems. I'm not sure how you are using the script, but in the case of "" when you type the command at the command line do not include the "" simply type Find_And_Replace.vbs "C:\1.txt" "temporary" "permanent". NOT Find_And_Replace.vbs "C:\1.txt" ""temporary"" "permanent". Commented Jan 11, 2010 at 13:02
  • You can also try changing the first line in the FindAndReplace function "Set inputFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(strFilename, 1)" With this "Set inputFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(strFilename, 1,true,0)". If that does not work try changing this this line "outputFile.Write Replace(strInputFile, strFind, strReplace)" With this "outputFile.Write Replace(strInputFile, strFind, strReplace,1,-1,1)" Commented Jan 11, 2010 at 13:20
  • Keep in mind that the Replace function returns the replaced text, it doesn't modify the variable passed into it. Commented Mar 22, 2010 at 20:42

2 Answers 2

7

Try this

If WScript.Arguments.Count <> 3 then
  WScript.Echo "usage: Find_And_replace.vbs filename word_to_find replace_with "
  WScript.Quit
end If

FindAndReplace WScript.Arguments.Item(0), WScript.Arguments.Item(1), WScript.Arguments.Item(2)
WScript.Echo "Operation Complete"

function FindAndReplace(strFilename, strFind, strReplace)
    Set inputFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(strFilename, 1)
    strInputFile = inputFile.ReadAll
    inputFile.Close
    Set inputFile = Nothing
    Set outputFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(strFilename,2,true)
    outputFile.Write Replace(strInputFile, strFind, strReplace)
    outputFile.Close
    Set outputFile = Nothing
end function 

Save this in a file called Find_And_Replace.vbs, it can then be used at the command line like this.

[C:\]> Find_And_Replace.vbs "C:\1.txt" "temporary" "permanent"

*This method is case sensitive "This" != "this"

If you don't want to read the entire file into memory, you could use a temp file like this.

If WScript.Arguments.Count <> 3 then
  WScript.Echo "usage: Find_And_replace.vbs filename word_to_find replace_with "
  WScript.Quit
end If

FindAndReplace WScript.Arguments.Item(0), WScript.Arguments.Item(1), WScript.Arguments.Item(2)
WScript.Echo "Operation Complete"

function FindAndReplace(strFile, strFind, strReplace)
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objInputFile = objFSO.OpenTextFile(strFile,1)
    strTempDir = objFSO.GetSpecialFolder(2)
    Set objTempFile = objFSO.OpenTextFile(strTempDir & "\temp.txt",2,true)
    do until objInputFile.AtEndOfStream
        objTempFile.WriteLine(Replace(objInputFile.ReadLine, strFind, strReplace))
    loop
    objInputFile.Close
    Set objInputFile = Nothing
    objTempFile.Close
    Set objTempFile = Nothing
    objFSO.DeleteFile strFile, true
    objFSO.MoveFile strTempDir & "\temp.txt", strFile
    Set objFSO = Nothing
end function 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot it's very beautiful and creative, this is what i was looking for. Happy new year.
Hi, I have sometimes problem with the script, the files that i have a problem with are including "", at the file before the word that i try to replace (i.e "this flight number is "temporary"". and also files that ended .config I don't get any error (Operation Complete) like successfully, so it's very difficult to know what's the problem. Any idea why or how to fix it? Regards
2

You can try this version which doesn't slurp the whole file into memory:

Set objFS = CreateObject("Scripting.FileSystemObject")
strFile=WScript.Arguments.Item(0)
strOld=WScript.Arguments.Item(1)
strNew=WScript.Arguments.Item(2)
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
 strLine = objFile.ReadLine 
        if Instr(strLine,strOld)> 0 Then
          strLine=Replace(strLine,strOld,strNew)
        End If
 WScript.Echo strLine
Loop

Usage:

c:\test> cscript //nologo find_replace.vbs file oldtext newtext

1 Comment

This does not change the file. The reason I "slurp" the whole file into memory, is so I can write changes back to the same file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.