tags:

views:

1189

answers:

3

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.

A: 

Have a look at the "How Can I Find and Replace Text in a Text File?" tutorial.

Peter Schuetze
A: 

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 
Tester101
Thanks a lot it's very beautiful and creative, this is what i was looking for.Happy new year.
Tzahi
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 .configI 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
Tzahi
A: 

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
ghostdog74
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.
Tester101