Replace 用VBscript查找并替换.txt文件中的字符串

Replace 用VBscript查找并替换.txt文件中的字符串,replace,vbscript,find,notepad,Replace,Vbscript,Find,Notepad,我正在尝试找出如何使用vbscript: 1-将.csv文件作为.txt文件打开 2-搜索在整个文本中随机定位的特定文本字符串 3-将该字符串替换为其他字符串 我找到了一篇文章,帮助我学习如何替换.txt文档中的整行,但到目前为止,还没有找到任何关于仅替换行中某些字符的内容 谢谢 以下是我当前使用的代码: Const ForReading = 1 Const ForWriting = 2 'Setting up our objects and focusing on the text file

我正在尝试找出如何使用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
它引用的文本文件显示:
肯·迈尔
法布里卡姆

皮拉尔·阿克曼
翼尖玩具

杰夫·海伊
法布里卡姆

艾伦·亚当斯
北风商人

迈尔


(文本文件结尾)。因此,本质上,我已经获得了成功地将行中的“Myer”更改为“Mike”的代码。我现在很难接受的是把第一行的“迈尔”改成“迈克”。希望这有助于澄清一些问题……我对此非常陌生,因此不确定应该使用什么语言来描述问题。

对.ReadAll()获得的文件内容使用Replace,然后将结果写回去。代码:

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