我正在尝试打开.reg文件并用另一个替换特定的字符串。我使用下面的代码(replace.vbs),我得到一个"无效的程序调用或参数"一旦我运行此代码,错误和.reg文件变空。这段代码适用于文本文件但不适用于.reg.please帮助我弄清楚我哪里出错了。
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.WriteLine strNewText
objFile.Close
我在cmd CSCRIPT replace.vbs "this.reg" "KEY" "1234"
中执行此操作,而this.reg正在变空。谢谢。
答案 0 :(得分:1)
你可以使用类似的东西来自动检测编码并相应地打开文件:
Function ReadFile(filename)
Set fso = CreateObject("Scripting.FileSystemObject")
bom = ""
Set f = fso.OpenTextFile(filename)
Do Until f.AtEndOfStream Or bom = "ÿþ" Or bom = "þÿ" Or Len(bom) >= 3
bom = bom & f.Read(1)
Loop
f.Close
Select Case bom
Case "ÿþ", "þÿ" 'UTF-16 text
Set f = fso.OpenTextFile(filename, 1, False, -1)
ReadFile = f.ReadAll
f.Close
Case "" 'UTF-8 text
Set stream = CreateObject("ADODB.Stream")
stream.Open
stream.Type = 2
stream.Charset = "utf-8"
stream.LoadFromFile filename
ReadFile = stream.ReadText
stream.Close
Case Else 'ASCII text
Set f = fso.OpenTextFile(filename, 1, False, 0)
ReadFile = f.ReadAll
f.Close
End Select
End Function
reg = ReadFile("C:\path\to\your.reg")