如何使用 vbscript 在 .reg 文件中查找和替换字符串 [英] how to find and replace a string in .reg files using vbscript
问题描述
我正在尝试打开一个 .reg 文件并用另一个替换一个特定的字符串.我正在使用下面的代码(replace.vbs)并且我收到一个无效的程序调用或参数"错误并且 .reg 文件是运行此代码后变空.此代码适用于文本文件,但不适用于 .reg.请帮助我找出我出错的地方.
I am trying to open a .reg file and replace a particular string with another.I am using the below code(replace.vbs) and i am getting a "Invalid procedural call or argument" error and the .reg file is getting empty once i run this code.This code works fine for text files but not for .reg.please help me to figure out where I am going wrong.
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 变空了.提前致谢.
I run this in cmd CSCRIPT replace.vbs "this.reg" "KEY" "1234"
and this.reg is getting empty.Thanks in advance.
推荐答案
你可以使用这样的东西来自动检测编码并相应地打开文件:
You could use something like this to auto-detect the encoding and open the file accordingly:
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")
这篇关于如何使用 vbscript 在 .reg 文件中查找和替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!