使用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:\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
它引用的文本文件说:
肯·迈尔
Fabrikam
The text file it references says:
Ken Myer
Fabrikam
Pilar Ackerman
翼尖玩具
Pilar Ackerman
Wingtip Toys
杰夫·海伊
Fabrikam
Jeff Hay
Fabrikam
艾伦·亚当斯(Ellen Adams)
罗斯文商人
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()获得的文件内容使用替换",然后将结果写回.在代码中:
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屋!