使用VBscript在.txt文件中查找和替换字符串

3
我正在尝试使用VBScript实现以下操作:
1. 将.csv文件作为.txt文件打开
2. 搜索文本中随机出现的某个特定字符串
3. 用不同的字符串替换该字符串。
我已经找到一篇文章,帮助我学会了如何替换.txt文档中的整行内容,但是目前还没有找到任何关于在行内替换特定字符的方法。
谢谢!
以下是我当前正在使用的代码:
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

它引用的文本文件内容如下:
Ken Myer
Fabrikam
Pilar Ackerman
Wingtip Toys
Jeff Hay
Fabrikam
Ellen Adams
Northwind Traders
Myer
(文本文件结束)因此,基本上我已经成功地将单独一行的“Myer”更改为“Mike”。我遇到的困难是将第一行中的“Myer”更改为“Mike”。希望这有助于澄清问题......我刚刚接触这个,所以不确定应该使用什么语言来描述问题。

请展示一下你已经尝试过的内容,也许我们可以帮助你找出你做错了什么。 - rory.ap
我在我的问题中添加了一些编辑,希望能有所帮助。 - M199463
2个回答

3
使用.Replace()方法对通过.ReadAll()获得的文件内容进行替换,然后使用.Write()将结果写回。代码如下:
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

使用该演示程序确定解决您的现实世界问题所需的内容。


0

我也很新,所以我没有理解其他答案在代码中的作用,但是我在最后一个答案中找到了 "Replace" 并在您的代码中使用它来做您需要的事情,结果是这样的:

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


    strLine = Replace(strLine,"Myer","Mike")

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

    strContents = strContents & strLine & vbCrLf

Loop



objFile.Close


Set objFile = objFSO.OpenTextFile("F:\BIBLIOTECAS\Archivos\TEST.txt", ForWriting)


objFile.Write(strContents)
objFile.Close

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接