使用 VBscript 在 .txt 文件中查找和替换字符串 [英] Find and Replace string in a .txt file with VBscript
问题描述
我正在尝试弄清楚如何使用 vbscript 来:
1 - 将 .csv 文件作为 .txt 文件打开
2 - 搜索随机位于整个文本中的特定文本字符串3 - 用不同的字符串替换该字符串.
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.
我发现一篇文章帮助我学习了如何替换 .txt 文档中的整行,但到目前为止还没有找到关于仅替换该行中某些字符的任何内容.
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.
谢谢!
这是我目前使用的代码:
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:UsersDocumentsScript PracticeTextFiles-2-4-15-FolderReadandWriteTextlook.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:UsersDocumentsScript PracticeTextFiles-2-4-15-FolderReadandWriteTextlook.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"更改为Mike"的代码.我遇到的困难是将第一行中的Myer"更改为Mike".希望这有助于澄清一些事情......我对此非常陌生,所以不确定我应该使用什么语言来描述这个问题.
(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.
推荐答案
对.ReadAll()获取的文件内容使用Replace,并将结果写回.在代码中:
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
输出:
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.
这篇关于使用 VBscript 在 .txt 文件中查找和替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!