Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and replace string in my text with VBScript

Tags:

vbscript

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.

like image 448
Tzahi Avatar asked Dec 29 '09 15:12

Tzahi


People also ask

How can I find and replace text in a text file?

Open the text file in Notepad. Click Edit on the menu bar, then select Replace in the Edit menu. Once in the Search and Replace window, enter the text you want to find and the text you want to use as a replacement. See our using search and replace and advanced options section for further information and help.

How do I replace a string in terminal?

Replace String in a File with the `sed` Command '-i' option is used to modify the content of the original file with the replacement string if the search string exists in the file. 's' indicates the substitute command. 'search_string' contains the string value that will be searched in the file for replacement.


2 Answers

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 
like image 79
Tester101 Avatar answered Nov 15 '22 10:11

Tester101


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
like image 28
ghostdog74 Avatar answered Nov 15 '22 09:11

ghostdog74