gpt4 book ai didi

vbscript - 使用vbs复制和替换txt文件中的简单文本

转载 作者:行者123 更新时间:2023-12-05 04:15:53 25 4
gpt4 key购买 nike

当运行下面的脚本时,我看到 Error Line 2 Char 1 Invalid procedure call or argument 请有人建议:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Users\newtons\Desktop\Text.txt",ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "Jim ", "James ")

Set objFile = objFSO.OpenTextFile("C:\Users\newtons\Desktop\Text.txt",ForWriting)
objFile.WriteLine strNewText
objFile.Close

最佳答案

像那样尝试:

Option Explicit
Const ForReading = 1
Const ForWriting = 2
Dim objFSO,objFile,strText,strNewText
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Users\newtons\Desktop\Text.txt",ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "Jim", "James")

Set objFile = objFSO.OpenTextFile("C:\Users\newtons\Desktop\Text.txt",ForWriting)
objFile.WriteLine strNewText
objFile.Close
set objFSO = Nothing
set objFile =

编辑:在这种情况下,您应该使用像这样的 VBScript 正则表达式:

Option Explicit
Const ForReading = 1
Const ForWriting = 2
Dim objFSO,objFile,strText,strNewText,objRegEx
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Users\newtons\Desktop\Text.txt",ForReading)
strText = objFile.ReadAll
objFile.Close
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True
objRegEx.IgnoreCase = True
objRegEx.Pattern = "jim"
strNewText = objRegEx.Replace(strText,"James")
Set objFile = objFSO.OpenTextFile("C:\Users\newtons\Desktop\Text.txt",ForWriting)
objFile.WriteLine strNewText
objFile.Close
set objFSO = Nothing
set objFile = Nothing

关于vbscript - 使用vbs复制和替换txt文件中的简单文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30393376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com