Find and Replace string in a .txt file with VBscript

霸气de小男生 提交于 2021-02-16 14:21:13

问题


I am trying to figure out how to use vbscript to:
1 - open a .csv file as a .txt file
2 - search for a certain string of text that is located randomly throughout the text 3 - replace that string with a different string.

I have found an article that helped me learn how to replace an entire line in a .txt document, but so far have had no luck finding anything about replacing just certain characters within the line.

Thanks!

Here is the code I am using currently:

Const ForReading = 1
Const ForWriting = 2

'Setting up our objects and focusing on the text file.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Users\Documents\Script Practice\TextFiles-2-4-15-Folder\ReadandWrite\Textlook.txt", ForReading)


Do Until objFile.AtEndOfStream

    strLine = objFile.ReadLine


    If strLine = "Myer" Then
        strLine = "Mike"
    End If

    strContents = strContents & strLine & vbCrLf

Loop



objFile.Close


Set objFile = objFSO.OpenTextFile("C:\Users\Documents\Script Practice\TextFiles-2-4-15-Folder\ReadandWrite\Textlook.txt", ForWriting)


objFile.Write(strContents)
objFile.Close

The text file it references says:
Ken Myer
Fabrikam

Pilar Ackerman
Wingtip Toys

Jeff Hay
Fabrikam

Ellen Adams
Northwind Traders

Myer

(End of text file). So essentially, I have gotten the code to successfully change the "Myer" that is on its own line to "Mike". What I am having a hard time with is changing the "Myer" in the first line to "Mike". Hopefully this helps clarify things a bit...I'm extremely new at this so not sure of the language I should be using to describe the problem.


回答1:


Use Replace on the file's content obtained by .ReadAll() and .Write the result back. In code:

Option Explicit

Dim goFS  : Set goFS  = Createobject("Scripting.FileSystemObject")
Dim goWAU : Set goWAU = WScript.Arguments.Unnamed

WScript.Quit main()

Function main()
  main = 1 ' assume error
  If 3 = goWAU.Count Then
     If goFS.FileExists(goWAU(0)) Then
        Dim s : s = goFS.OpenTextFile(goWAU(0)).ReadAll()
        If 0 < Instr(s, goWAU(1)) Then
           goFS.CreateTextFile(goWAU(0)).Write Replace(s, goWAU(1), goWAU(2))
           WScript.Echo "done"
           main = 0
        Else
           WScript.Echo goWAU(1), "not found"
        End If
     Else
        WScript.Echo goWAU(0), "does not exist"
     End If
  Else
     WScript.Echo "need 3 args: fspec, find, replacement"
  End If
End Function

output:

copy con 28350055.csv
1,2,3
4,5,6
^Z

cscript 28350055.vbs 28350055.csv 5 4711
done

type 28350055.csv
1,2,3
4,4711,6

cscript 28350055.vbs 28350055.csv 5 4711
5 not found

cscript 28350055.vbs 28350055.cs 5 4711
28350055.cs does not exist

Use that demo to determine what is needed to solve your real world problem.



来源:https://stackoverflow.com/questions/28350055/find-and-replace-string-in-a-txt-file-with-vbscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!