【问题标题】:Find and Replace string in a .txt file with VBscript使用 VBscript 在 .txt 文件中查找和替换字符串
【发布时间】:2015-04-05 16:01:28
【问题描述】:

我正在尝试弄清楚如何使用 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”的代码。我遇到的困难是将第一行中的“Myer”更改为“Mike”。希望这有助于澄清一些事情......我对此非常陌生,所以不确定我应该使用什么语言来描述这个问题。

【问题讨论】:

  • 请展示您到目前为止所做的尝试,我们可能会帮助您找出您做错了什么。
  • 我对我的问题进行了一些修改,希望能有所帮助。

标签: replace vbscript find notepad


【解决方案1】:

对 .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

使用该演示来确定解决实际问题所需的内容。

【讨论】:

    【解决方案2】:

    我也很新,所以我没有得到代码中其他答案的作用,但我在最后一个答案中找到了关于“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
    

    【讨论】:

      猜你喜欢
      • 2011-05-25
      • 2020-07-04
      • 2013-02-19
      • 2020-08-12
      • 1970-01-01
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多