如何使用 vbscript 将 GUID 写入.ini文件?
guid = CreateGUID()
WriteIni "testsection", "ID", guid, "H:test123.ini"
Function CreateGUID()
CreateGUID = ""
Set objTypeLib = CreateObject("Scriptlet.TypeLib")
Wscript.Echo "GUID: " & objTypeLib.Guid
CreateGUID = objTypeLib.Guid
End Function
上面的代码无法正常工作,它将其他一些字符附加到.ini文件的 GUID 中。例如:
将 GUID 写入test123.ini
后:
[testsection]
ID=EAE11CC4-F60E-4B21-8AA8-E0CE2ADE5521 5
这里添加了额外的字符(例如:5)。它必须像这样更新文件:
[testsection]
ID=EAE11CC4-F60E-4B21-8AA8-E0CE2ADE5521
Scriptlet.TypeLib
对象中的一个已知错误。Guid
属性的长度应为 38 个字符,但返回的长度应为 40,末尾有 2 个垃圾字符。您需要修剪Guid
值以避免此问题:
Function CreateGUID()
CreateGUID = ""
Set objTypeLib = CreateObject("Scriptlet.TypeLib")
CreateGUID = Left(TypeLib.Guid, 38) ' With curly braces: {47BC69BD-06A5-4617-B730-B644DBCD40A9}
' CreateGUID = Mid(TypeLib.Guid, 2, 36) ' Without curly braces: 47BC69BD-06A5-4617-B730-B644DBCD40A9
End Function
你正在使它变得比它需要的更复杂。我建议将INI文件读入字典字典,以便像这样的文件:
[Section1]
value1="foo"
value2="bar"
[Section2]
valueX="baz"
变成这样的数据结构:
+-Section1
| +-value1 -> "foo"
| `-value2 -> "bar"
`-Section2
`-valueX -> "baz"
然后,只需将 GUID 添加到给定部分,如下所示:
section = "Section1"
If Not ini.Exists(section) Then
ini.Add section, CreateObject("Scripting.Dictionary")
End If
ini(section)("ID") = CreateObject("Scriptlet.TypeLib").GUID
并将数据写回如下文件:
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("C:pathtoyour.ini", 2)
For Each section In ini.Keys
f.WriteLine "[" & section & "]"
For Each value In ini(section).Keys
f.WriteLine value & "=" & Chr(34) & ini(section)(value) & Chr(34)
Next
f.WriteLine
Next
f.Close
不久前,我编写了一个过程(ParseIni
)用于将INI文件解析为此类数据结构。