这是我使用VB脚本编写的第一天。我找到了以下代码来搜索和替换文本文件中的文本,但是当我使用以下命令运行它时
cscript replace.vbs "test.txt" "Jim" "James"
我收到错误说
replace.vbs(6, 1) Microsoft VBScript runtime error: Object required: 'Scripting'
这是代码
Const ForReading = 1
Const ForWriting = 2
strFileName = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strNewText = Wscript.Arguments(2)
Set objFSO = CreateObject(Scripting.FileSystemObject)
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOldText, strNewText)
Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.Write strNewText 'WriteLine adds extra CRLF
objFile.Close
答案 0 :(得分:2)
使用
Set objFSO = CreateObject("Scripting.FileSystemObject")
(标记引号,CreateObject()需要一个字符串)