本帖最后由 yuanyannian 于 2014-10-19 14:07 编辑
这个有点麻烦,求助高手老师们。
原文件如下:是后缀为 .reg 的注册表文件。
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\pe-def\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders]
"AppData"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,\
4c,00,45,00,25,00,5c,00,41,00,70,00,70,00,6c,00,69,00,63,00,61,00,74,00,69,\
00,6f,00,6e,00,20,00,44,00,61,00,74,00,61,00,00,00
"Desktop"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,\
4c,00,45,00,25,00,5c,00,4c,68,62,97,00,00
"Favorites"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,\
4c,00,45,00,25,00,5c,00,46,00,61,00,76,00,6f,00,72,00,69,00,74,00,65,00,73,\
00,00,00
"NetHood"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,\
4c,00,45,00,25,00,5c,00,4e,00,65,00,74,00,48,00,6f,00,6f,00,64,00,00,00
" ersonal"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,\
4c,00,45,00,25,00,5c,00,4d,00,79,00,20,00,44,00,6f,00,63,00,75,00,6d,00,65,\
00,6e,00,74,00,73,00,00,00
"PrintHood"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,\
4c,00,45,00,25,00,5c,00,50,00,72,00,69,00,6e,00,74,00,48,00,6f,00,6f,00,64,\
00,00,00
"Programs"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,\
4c,00,45,00,25,00,5c,00,0c,30,00,5f,cb,59,0d,30,dc,83,55,53,5c,00,0b,7a,8f,\
5e,00,00
"Recent"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,4c,\
00,45,00,25,00,5c,00,52,00,65,00,63,00,65,00,6e,00,74,00,00,00
"SendTo"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,4c,\
00,45,00,25,00,5c,00,53,00,65,00,6e,00,64,00,54,00,6f,00,00,00
"Start Menu"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,\
00,4c,00,45,00,25,00,5c,00,0c,30,00,5f,cb,59,0d,30,dc,83,55,53,00,00
"Startup"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,\
4c,00,45,00,25,00,5c,00,0c,30,00,5f,cb,59,0d,30,dc,83,55,53,5c,00,0b,7a,8f,\
5e,5c,00,2f,54,a8,52,00,00
"Templates"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,\
4c,00,45,00,25,00,5c,00,54,00,65,00,6d,00,70,00,6c,00,61,00,74,00,65,00,73,\
00,00,00
"Cookies"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,\
4c,00,45,00,25,00,5c,00,43,00,6f,00,6f,00,6b,00,69,00,65,00,73,00,00,00
"My Pictures"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,\
00,4c,00,45,00,25,00,5c,00,4d,00,79,00,20,00,44,00,6f,00,63,00,75,00,6d,00,\
65,00,6e,00,74,00,73,00,5c,00,4d,00,79,00,20,00,50,00,69,00,63,00,74,00,75,\
00,72,00,65,00,73,00,00,00
"Local Settings"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,\
49,00,4c,00,45,00,25,00,5c,00,4c,00,6f,00,63,00,61,00,6c,00,20,00,53,00,65,\
00,74,00,74,00,69,00,6e,00,67,00,73,00,00,00
"Local AppData"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,\
49,00,4c,00,45,00,25,00,5c,00,4c,00,6f,00,63,00,61,00,6c,00,20,00,53,00,65,\
00,74,00,74,00,69,00,6e,00,67,00,73,00,5c,00,41,00,70,00,70,00,6c,00,69,00,\
63,00,61,00,74,00,69,00,6f,00,6e,00,20,00,44,00,61,00,74,00,61,00,00,00
"Cache"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,4c,\
00,45,00,25,00,5c,00,4c,00,6f,00,63,00,61,00,6c,00,20,00,53,00,65,00,74,00,\
74,00,69,00,6e,00,67,00,73,00,5c,00,54,00,65,00,6d,00,70,00,6f,00,72,00,61,\
00,72,00,79,00,20,00,49,00,6e,00,74,00,65,00,72,00,6e,00,65,00,74,00,20,00,\
46,00,69,00,6c,00,65,00,73,00,00,00
"History"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,\
4c,00,45,00,25,00,5c,00,48,00,69,00,73,00,74,00,6f,00,72,00,79,00,00,00
问题:1. 如果存在 "History" 、"Local AppData"、"NetHood"、 "Personal"、"PrintHood"、"Recent"、"Cookies"、"My Pictures"、"Local AppData" 键值,就删除所在的整个键值。
2. 替换 "Programs" 为 "Common Programs" ,"Start Menu",为"Common Start Menu","Desktop" 为 "Common Desktop","Startup" 为 "Common Startup","AppData" 为 "Common AppData","Templates" 为 "Common Templates","Favorites" 为 "Common Favorites",其余的都删除。
3. 替换每一个键值下的 hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,\
4c,00,45,00,25,00(很有规律,这段16 进制代码之后的内容不能更改)为 hex(2):25,00,41,00,4c,00,4c,00,55,00,53,00,45,00,52,00,53,00,50,00,52,00,\
4f,00,46,00,49,00,4c,00,45,00,25,00,可以先合并行再替换。
我的思路:是否可以先合并各个键值的行为一行,再替换更好?
要求:操作中保持 .reg 格式(即 unicode 文件格式)不变,hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,\
4c,00,45,00,25,00 之后的内容不能动。
先谢谢!
-----------------------------------------------------------------------------------------------------
感谢 apang 等老师,我的问题圆满解决。将最终代码贴上,以供需要的参考: | Dim msg1, msg2, fso, ws, oArgs, iPath, tPath, sLoca, sPName, tPName | | msg1 = "HojoHE.exe -Sdefault -ID:\a -TE:\a\b -L00000409" | | msg2 = "HojoUE.exe -SC:\def.reg -TD:\sft.reg" | | Set fso = CreateObject("Scripting.FileSystemObject") | | Set ws = CreateObject("WScript.Shell") | | Set oArgs = WScript.Arguments | | | | If oArgs.Count >= 4 Then | | If (Left(oArgs(0),2) = "-S") and (Left(oArgs(1),2) = "-I") and (Left(oArgs(2),2) = "-T") and (Left(oArgs(3),2) = "-L") Then | | iPath = Mid(oArgs(1), 3) & "\" | | tPath = Mid(oArgs(2), 3) & "\" | | sLoca = Mid(oArgs(3), 3) | | Call HojoHE() | | Else MsgBox "usage:" & vbLf & vbLf & msg1 | | End If | | ElseIf oArgs.Count = 2 Then | | If Left(oArgs(0),2) = "-S" and (Left(oArgs(1),2) = "-T") Then | | sPName = Mid(oArgs(0), 3) | | tPName = Mid(oArgs(1), 3) | | Call ChangeRegFile() | | Else MsgBox "usage:" & vbLf & vbLf & msg2 | | End If | | Else MsgBox "usage:" & vbLf & vbLf & msg1 & vbLf & "or" & vbLf & msg2 | | End If | | | | Function HojoHE() | | On Error Resume Next | | Dim ar, i | | If Not fso.FolderExists(tPath) Then fso.CreateFolder tPath | | Select Case LCase(Mid(oArgs(0), 3)) | | Case "default" | | fso.CopyFile iPath & "HIVEDEF.INF", tPath, true | | Call ProcessFile(tPath & "HIVEDEF.INF", "default") | | Case "software" | | ar = Array("HIVESFT","HIVECLS","HIVESXS","HIVCLS32","HIVSFT32","DMREG") | | For i = 0 to UBound(ar) | | fso.CopyFile iPath & ar(i) & ".INF", tPath, true | | Call ProcessFile(tPath & ar(i) & ".INF", "software") | | Next | | Case "setupreg.hiv" | | ar = Array("HIVESYS","INTL") | | For i = 0 to UBound(ar) | | fso.CopyFile iPath & ar(i) & ".INF", tPath, true | | Call ProcessFile(tPath & ar(i) & ".INF", "setup") | | Next | | Case Else MsgBox "The parameter isn't supported!" & vbLf & vbLf & "Must be 'default', or 'software', or 'setupreg.hiv'." | | WScript.Quit | | End Select | | End Function | | | | Function ChangeRegFile() | | Dim f, txt, re, m, s1, s2, s | | Set f = fso.OpenTextFile(sPName, 1, , -1) | | txt = f.ReadAll : f.Close | | Set re = New RegExp | | re.Pattern = "([\s\S]*?)(^"".+"" *=[\s\S]+?)(?=^"")" | | re.Global = true | | re.IgnoreCase = true | | re.MultiLine = true | | For Each m in re.Execute(txt & vbCrLf & """") | | s1 = m.SubMatches(0) | | s2 = ReReplace(m.SubMatches(1)) | | If m.SubMatches(1) <> s2 Then | | s = s & s1 & s2 | | Else s = s & s1 | | End If | | Next | | s1 = "25,00,41,00,4c,00,4c,00,55,00,53,00,45,00,52,00,53,00," | | re.Pattern = "(hex\(2\):)25,00,55,00,53,00,45,00,52,00," | | s = re.Replace(s, "$1" & s1) | | re.Pattern = "WB-default\\Software" | | s = re.Replace(s, "WB-software") | | fso.OpenTextFile(tPName, 2, true, -1).Write s | | End Function | | | | Function ProcessFile(infFile, hivFile) | | Dim f, s, lgInst, yn | | Set f = fso.OpenTextFile(infFile, 1, false, GetFileFormat(infFile)) | | s = f.ReadAll : f.Close | | s = ReplaceStr(s, "HKCU, *""", "HKLM,""WB-default\") | | s = ReplaceStr(s, "HKLM, *""SYSTEM\\CurrentControlSet", "HKLM,""WB-setup\ControlSet001") | | s = ReplaceStr(s, "HKLM, *""SYSTEM\\", "HKLM,""WB-setup\") | | s = ReplaceStr(s, "HKLM, *SYSTEM\\CurrentControlSet", "HKLM,WB-setup\ControlSet001") | | s = ReplaceStr(s, "HKLM, *SYSTEM\\", "HKLM,WB-setup\") | | s = ReplaceStr(s, "\\CryptSvc\\Security"",""Security"",0x00030003, *\\", "\CryptSvc\Security"",""Security"",0x00030003,00") | | s = ReplaceStr(s, "HKLM, *""SOFTWARE\\", "HKLM,""WB-software\") | | s = ReplaceStr(s, "HKLM, *SOFTWARE\\", "HKLM,WB-software\") | | s = ReplaceStr(s, "HKCR, *""", "HKLM,""WB-software\Classes\") | | s = ReplaceStr(s, "HKCR,\.", "HKLM,WB-software\Classes\.") | | If UCase(infFile) = UCase(tPath & "INTL.INF") Then | | s = ReplaceStr(s, "\[" & sLoca & "\]", "[DefaultInstall]") | | s = ReplaceStr(s, "CopyFile", ";CopyFile") | | lgInst = Split(ProssLocales(s), ",") | | s = ReplaceStr(s, "\[LG_INSTALL_(" & lgInst(0) & "|" & lgInst(1) & ")]", "[DefaultInstall]") | | ElseIf Left(s, 16) <> "[DefaultInstall]" Then | | s = "AddReg = AddReg.Upgrade" & vbCrLf & s | | s = "AddReg = AddReg.Fresh" & vbCrLf & s | | s = "AddReg = AddReg.RemoteBoot" & vbCrLf & s | | s = "AddReg = AddReg" & vbCrLf & s | | s = "[DefaultInstall]" & vbCrLf & s | | End If | | fso.OpenTextFile(infFile, 2, true, -1).Write s | | On Error Resume Next | | yn = ws.RegRead("HKEY_LOCAL_MACHINE\WB-" & hivFile & "\") | | If yn <> 0 Then | | infFile = fso.GetFile(infFile).ShortPath | | ws.Run "rundll32 syssetup,SetupInfObjectInstallAction DefaultInstall 132 " & infFile, , true | | Else MsgBox "Error, the WB-" & hivFile & " not found and exit." | | End If | | End Function | | | | Function GetFileFormat(ByVal infFile) | | Dim Bin | | with CreateObject("Adodb.Stream") | | .Type = 1 | | .Mode = 3 | | .Open | | .Position = 0 | | .Loadfromfile infFile | | Bin = .read(2) | | End with | | If AscB(MidB(Bin,1,1))=&HFF and AscB(MidB(Bin,2,1))=&HFE Then | | GetFileFormat = -1 | | Else GetFileFormat = 0 | | End If | | End Function | | | | Function ReplaceStr(ByVal s, pattern, s1) | | Dim re | | Set re = New RegExp | | re.Pattern = pattern | | re.Global = true | | re.IgnoreCase = true | | ReplaceStr = re.Replace(s, s1) | | End Function | | | | Function ProssLocales(ByVal s) | | Dim pattern1, pattern2, re, m | | pattern1 = "^ *\[Locales] *$" | | pattern2 = "^ *" & sLoca & " *=([^,]*,){2}([^,]*,[^,]*),.*$" | | Set re = New RegExp | | re.Pattern = pattern1 & "[\s\S]*?" & pattern2 | | re.IgnoreCase = true | | re.MultiLine = true | | For Each m in re.Execute(s) | | ProssLocales = m.SubMatches(1) | | Next | | End Function | | | | Function ReReplace(str) | | Dim re, p | | p = "Programs|Start Menu|Desktop|Startup|AppData|Templates|Favorites" | | Set re = New RegExp | | re.Pattern = """(" & p & ")""" | | re.IgnoreCase = true | | ReReplace = re.Replace(str, """Common $1""") | | End FunctionCOPY |
|