原文http://slore.blogbus.com/logs/52627038.html
Slore编写的这个reg文件转换为bat文件,是逐句转换的,不是通过批处理生成临时reg文件然后导入的方法,很不错。
我做了以下修改:
1、修改openfile函数,用SelectFile( )代替,原函数中Set objDialog = CreateObject("UserAccounts.CommonDialog")只支持XP,WIN7下没有选择文件的对话框,不方便,修改之后的VBS代码支持XP、WIN7、WIN7X64。Windows 8, Windows 8.1未测试,理论上可行。
2、选择reg文件之后,默认保存为与之同名的bat文件,避免两次弹出选择文件的对话框。
Slore的这个VBS确实很不错,在此表示感谢!代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 | '------------------------------------------------------------------------------ '名称:Reg2Bat_By Slore(生成同名bat文件,支持XP、WIN7、WIN7X64).vbs '功能:REG文件转换为同名的BAT、CMD文件。 '原文http://slore.blogbus.com/logs/52627038.html ' REG命令功能有限,仅支持常用类型。 ' (REG_SZ、REG_DWORD、REG_BINARY、 ' REG_EXPAND_SZ、REG_MULTI_SZ) ' ' By Slore 【修改by 代码飞扬】 ' 更新于:2017年2月16日 ' '如果对脚本有任何意见和建议,可发送相关信息至: ' slorelee@yahoo.com.cn ' '声明: '本人支持开源,代码未作任何加密,可自由转载,但请 '尊重他人劳动成果,转载请务必注明出处和原作者。 '------------------------------------------------------------------------------ '--------------------------- 'Reg2Bat By Slore '--------------------------- '命令行参数说明(不区分大小写) '/?、/h、/help 查看此帮助信息 '/i:RegFileName 指定要转换的注册表文件路径 '/o:BatFileName 指定转换后的批处理文件路径 '可选参数 '/S:Separator REG_MULTI_SZ 数据字符串中用作分隔符的字符 ' 仅限一个字符,默认"\0"用作分隔符 '/Q 安静模式,不弹出错误提示 '/NF 转换后REG命令无/F参数 '/NH 忽略注册表文件头检测 ' '例如: '简易模式:CScript Reg2Bat.vbs [/i:]slore.reg /S:轩 /Q ' 省略批处理文件路径,将输出为注册表文件同名文件。 '经典模式:CScript Reg2Bat.vbs slore.reg slore.bat /S:轩 /Q ' 其中注册表文件路径和批处理文件路径顺序不可调换。 '标准模式:CScript Reg2Bat.vbs /i:slore.reg /o:slore.bat /S:轩 /Q ' 其中/i:、/o:、/S:中的冒号不可省略,顺序可变。 '--------------------------- Const ForReading = 1 Const ForWriting = 2 Const ForAppending = 8 Const blnOverWrite = True Const adTypeBinary = 1 Const adModeReadWrite = 3 Const ANSI = 0 Const Unicode = - 1 Const REG_SEPARATOR = "\0" '默认分隔符 Const BatHead = "@echo off" '转换后的批处理文件头 Const BatFileExt = "bat" '批处理文件扩展名 Const IgnoreRegHead = False '忽略注册表文件头检测 Dim RegHexType(10) RegHexType(0) = "REG_NONE" :RegHexType(1) = "REG_SZ" RegHexType(2) = "REG_EXPAND_SZ" :RegHexType(3) = "REG_BINARY" RegHexType(4) = "REG_DWORD" :RegHexType(5) = "REG_DWORD_BIG_ENDIAN" RegHexType(6) = "REG_LINK" :RegHexType(7) = "REG_MULTI_SZ" RegHexType(8) = "REG_RESOURCE_LIST" :RegHexType(9) = "REG_FULL_RESOURCE_DESCRIPTOR" Dim RegSeptr Dim blnForce,blnSilent blnForce = True blnSilent = False Dim RegFile,BatFile Dim objFSO Set objFSO = CreateObject( "Scripting.FileSystemObject" ) '参数处理 With WSH.Arguments If .Count <> 0 Then If .Named.Exists( "?" ) Then ShowHelp If .Named.Exists( "h" ) Then ShowHelp If .Named.Exists( "help" ) Then ShowHelp If .Named.Exists( "i" ) Then RegFile = .Named.Item( "i" ) If .Named.Exists( "o" ) Then BatFile = .Named.Item( "o" ) If .Named.Exists( "s" ) Then RegSeptr = .Named.Item( "s" ) If .Named.Exists( "q" ) Then blnSilent = True If .Named.Exists( "nf" ) Then blnForce = False If .Named.Exists( "nh" ) Then IgnoreRegHead = True If .Unnamed.Count > 0 Then RegFile = .Unnamed.Item(0) If Not objFSO.FileExists(RegFile) Then WSH.Quit If .Unnamed.Count > 1 Then BatFile = .Unnamed.Item(1) Else BatFile = Left(RegFile,InstrRev(RegFile, "." )) & "bat" End If End If End If End With If Len(RegSeptr) = 0 Then RegSeptr = REG_SEPARATOR Else RegSeptr = Left(RegSeptr,1) End If '选择注册表文件 Dim strFile RegFile = SelectFile( ) If RegFile = "" Then ' RegFile = OpenFile(".","注册表文件(*.reg)|*.reg") RegFile= SelectFile( ) If RegFile = "" Then WSH.Quit 'Else ' BatFile = Left(RegFile,InstrRev(RegFile,".")) & BatFileExt End If If Not objFSO.FileExists(RegFile) Then WSH.Quit '选择批处理文件 'If BatFile = "" Then BatFile = OpenFile(".","批处理文件(*." & BatFileExt & ")|*." & BatFileExt) BatFile = Left(RegFile,InstrRev(RegFile, "." )) & BatFileExt If BatFile = "" Then WSH.Quit '获取注册表文件编码 Dim FileEncoding FileEncoding = GetEncoding(RegFile) If FileEncoding = "ANSI" Then OpenFormat = ANSI ElseIf FileEncoding = "Unicode" Then OpenFormat = Unicode Else If Not blnSilent Then MsgBox "注册表文件的编码不正确。" ,vbInformation, "文件编码:" & FileEncoding WSH.Quit End If '格式化注册表文件 Dim RegStr,RegLine Set objFile = objFSO.OpenTextFile(RegFile,ForReading, False ,OpenFormat) Do Until objFile.AtEndOfStream RegLine = MyTrim(objFile.ReadLine) If RegLine <> "" Then '清除空行 'If Left(RegLine,1) <> ";" Then RegStr = RegStr & RegLine & vbCrLf '清除注释行 RegStr = RegStr & RegLine & vbCrLf End If Loop objFile.Close '合并hex(?)类型多行数据 Dim hStr,hPos,RegChar hPos = InStr(1,RegStr,",\" & vbCrLf) Do While hPos > 0 RegChar = Mid(RegStr,hPos + 4,1) If InStr(1, "[@" "" ,RegChar) > 0 Then RegStr = Left(RegStr,hPos - 1) & Mid(RegStr,hPos + 2) ElseIf RegChar = ";" Then RemEnd = InStr(hPos + 5,RegStr,vbCrLf) If RemEnd = 0 Then RegStr = Left(RegStr,hPos - 1) Else RegStr = Left(RegStr,hPos) & Mid(RegStr,RemEnd + 2) End If Else RegStr = Left(RegStr,hPos) & Mid(RegStr,hPos + 4) End If hPos = InStr(hPos + 4,RegStr,",\" & vbCrLf) Loop '替换主键为缩写 RegStr = Replace(RegStr,vbCrLf & "[HKEY_LOCAL_MACHINE\",vbCrLf & " [HKLM\") RegStr = Replace(RegStr,vbCrLf & "[HKEY_CURRENT_USER\",vbCrLf & " [HKCU\") RegStr = Replace(RegStr,vbCrLf & "[HKEY_CLASSES_ROOT\",vbCrLf & " [HKCR\") RegStr = Replace(RegStr,vbCrLf & "[HKEY_USER\",vbCrLf & " [HKU\") RegStr = Replace(RegStr,vbCrLf & "[HKEY_CUREENT_CONFIG\",vbCrLf & " [HKCC\") RegStr = Replace(RegStr,vbCrLf & "[-HKEY_LOCAL_MACHINE\",vbCrLf & " [-HKLM\") RegStr = Replace(RegStr,vbCrLf & "[-HKEY_CURRENT_USER\",vbCrLf & " [-HKCU\") RegStr = Replace(RegStr,vbCrLf & "[-HKEY_CLASSES_ROOT\",vbCrLf & " [-HKCR\") RegStr = Replace(RegStr,vbCrLf & "[-HKEY_USER\",vbCrLf & " [-HKU\") RegStr = Replace(RegStr,vbCrLf & "[-HKEY_CUREENT_CONFIG\",vbCrLf & " [-HKCC\") '对格式化后的注册表文件进行转换 Dim RegLines,n RegLines = Split(RegStr,vbCrLf) n = UBound(RegLines) If Not IgnoreRegHead Then '检验文件头 If RegLines(0) <> "REGEDIT4" And _ RegLines(0) <> "Windows Registry Editor Version 5.00" Then If Not blnSilent Then MsgBox "不是合法注册表文件头" ,vbInformation, "错误" WSH.Quit End If End If Dim RegCommand,BatStr Dim i,RegKey,RegVal,RegType,RegData If blnForce Then sForce = " /f" Else sForce = "" Dim o '为1是删除,为0是添加 Dim vPos Dim HeadChar,LastHeadChar LastHeadChar = "" For i = 1 To n HeadChar = Left(RegLines(i),1) If HeadChar = "[" Then If LastHeadChar = "[" And o = 0 Then '添加项 BatStr = BatStr & RegCommand & RegKey & sForce & vbCrLf End If If Mid(RegLines(i),2,1) = "-" Then RegCommand = "reg delete " o = 1 Else RegCommand = "reg add " o = 0 End If RegKey = "" "" & Mid(RegLines(i),2 + o,Len(RegLines(i)) - 2 - o) & "" "" If o = 1 Then '删除项 BatStr = BatStr & RegCommand & RegKey & sForce & vbCrLf RegKey = "" End If ElseIf HeadChar = "@" And ( Not Len(RegKey)) Then RegLines(i) = Replace(RegLines(i), "\\" ,vbNullChar) RegVal = " /ve " GetTD Replace(Mid(RegLines(i),3),vbNullChar,"\"),RegType,RegData If RegData = "-" Then BatStr = BatStr & "reg delete " & RegKey & " /ve " & sForce & vbCrLf Else If RegType <> "REG_SZ" Then RegType = " /t " & RegType Else RegType = "" End If BatStr = BatStr & "reg add " & RegKey & " /ve" & RegType & " /d " & RegData & sForce & vbCrLf End If ElseIf HeadChar = "" "" And ( Not Len(RegKey)) Then RegLines(i) = Replace(RegLines(i), "\\" ,vbNullChar) vPos = InStr(2,RegLines(i), "" "=" ) If vPos > 2 Then If Mid(RegLines(i),vPos - 1,1) = "\" Then Do Until Mid(RegLines(i),vPos - 1,1) <> "\" vPos = InStr(vPos + 2,RegLines(i), "" "=" ) If vPos = 0 Then Exit Do Loop End If If vPos <> 0 Then RegVal = Replace(Left(RegLines(i),vPos),vbNullChar,"\") RegVal = RegSpe(RegVal,0) GetTD Replace(Mid(RegLines(i),vPos + 2),vbNullChar,"\"),RegType,RegData If RegData = "-" Then BatStr = BatStr & "reg delete " & RegKey & " /v " & RegVal & sForce & vbCrLf Else If RegType <> "REG_SZ" Then RegType = " /t " & RegType Else RegType = "" End If BatStr = BatStr & "reg add " & RegKey & " /v " & RegVal & RegType & " /d " & RegData & sForce & vbCrLf End If End If End If ElseIf HeadChar = ";" Then '注释内容处理(仅支持纯注释行,并且不在Hex数据中间。) BatStr = BatStr & "rem " & Mid(RegLines(i),2) & vbCrLf HeadChar = LastHeadChar End If LastHeadChar = HeadChar Next '保存为批处理文件 If Len(BatHead) Then BatStr = BatHead & vbCrLf & BatStr If LCase(Right(BatFile,4)) <> "." & BatFileExt Then BatFile = BatFile & "." & BatFileExt Set objFile = objFSO.CreateTextFile(BatFile,blnOverWrite) objFile.Write BatStr objFile.Close Sub ShowHelp() MsgBox "命令行参数说明(不区分大小写)" & vbCrLf & _ "/?、/h、/help 查看此帮助信息" & vbCrLf & _ "/i:RegFileName 指定要转换的注册表文件路径" & vbCrLf & _ "/o:BatFileName 指定转换后的批处理文件路径" & vbCrLf & _ "可选参数" & vbCrLf & _ "/S:Separator REG_MULTI_SZ 数据字符串中用作分隔符的字符" & vbCrLf & _ " 仅限一个字符,默认" "\0" "用作分隔符" & vbCrLf & _ "/Q 安静模式,不弹出错误提示" & vbCrLf & _ "/NF 转换后REG命令无/F参数" & vbCrLf & _ "/NH 忽略注册表文件头检测" & vbCrLf & vbCrLf & _ "例如:" & vbCrLf & _ "简易模式:CScript Reg2Bat.vbs [/i:]slore.reg /S:轩 /Q" & vbCrLf & _ " 省略批处理文件路径,将输出为注册表文件同名文件" & vbCrLf & _ "经典模式:CScript Reg2Bat.vbs slore.reg slore.bat /S:轩 /Q" & vbCrLf & _ " 其中注册表文件路径和批处理文件路径顺序不可调换。" & vbCrLf & _ "标准模式:CScript Reg2Bat.vbs /i:slore.reg /o:slore.bat /S:轩 /Q" & vbCrLf & _ " 其中/i:、/o:、/S:中的冒号不可省略,顺序可变。" _ ,vbInformation, "Reg2Bat By Slore" WSH.Quit End Sub '---------------------------------自定义函数------------------------------- '打开文件函数 '参数:初始路径,文件类型过滤器 Function OpenFile(IntDir,Fltr) Dim objDialog Set objDialog = CreateObject( "UserAccounts.CommonDialog" ) objDialog.Filter = Fltr objDialog.InitialDir = IntDir intResult = objDialog.ShowOpen If intResult Then OpenFile = objDialog.FileName Else OpenFile = "" End If End Function '修改后的打开文件函数: 'Works in Windows XP, Vista, Windows 7, Windows 8, Windows 8.1. Function SelectFile( ) Dim objExec, strMSHTA, wshShell SelectFile = "" ' For use in HTAs as well as "plain" VBScript: strMSHTA = "mshta.exe " "about:" & "<" & "input type=file id=FILE>" _ & "<" & "script>FILE.click();new ActiveXObject('Scripting.FileSystemObject')" _ & ".GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);" & "<" & "/script>" "" ' For use in "plain" VBScript only: ' strMSHTA = "mshta.exe ""about:<input type=file id=FILE>" _ ' & "<script>FILE.click();new ActiveXObject('Scripting.FileSystemObject')" _ ' & ".GetStandardStream(1).WriteLine(FILE.value);close();resizeTo (0,0);</script> "" " Set wshShell = CreateObject( "WScript.Shell" ) Set objExec = wshShell.Exec( strMSHTA ) SelectFile = objExec.StdOut.ReadLine( ) Set objExec = Nothing Set wshShell = Nothing Set wshShell = Nothing End Function '调用方法 'Dim strFile 'strFile = SelectFile( ) 'If strFile = "" Then ' WScript.Echo "No file selected." 'Else ' WScript.Echo """" & strFile & """" 'End If '检测文本文件编码 Function GetEncoding(FileName) Dim objStreamR,HeadBin,HeadBytes(1) Set objStreamR = CreateObject( "Adodb.Stream" ) objStreamR.Type = adTypeBinary objStreamR.Mode = adModeReadWrite objStreamR.Open objStreamR.LoadFromFile FileName HeadBin = objStreamR.Read(2) objStreamR.Close HeadBytes(0) = AscB(MidB(HeadBin,1,1)) HeadBytes(1) = AscB(MidB(HeadBin,2,1)) GetEncoding = "ANSI" If HeadBytes(0) = &HFF And HeadBytes(1) = &HFE Then GetEncoding = "Unicode" If HeadBytes(0) = &HFE And HeadBytes(1) = &HFF Then GetEncoding = "Unicode Big Endian" If HeadBytes(0) = &HEF And HeadBytes(1) = &HBB Then GetEncoding = "UTF-8" End Function '剔除字符串两边的Tab字符和空格 Function MyTrim(iStr) Dim sPos,ePos sPos = 1 For i = 1 To Len(iStr) If Mid(iStr,i,1) = vbTab or Mid(iStr,i,1) = " " Then sPos = i + 1 Else Exit For End If Next ePos = Len(iStr) For i = Len(iStr) To 1 Step - 1 If Mid(iStr,i,1) = vbTab or Mid(iStr,i,1) = " " Then ePos = i - 1 Else Exit For End If Next If (ePos - sPos + 1) < 0 Then MyTrim = "" Exit Function End If MyTrim = Mid(iStr,sPos,ePos - sPos + 1) End Function Sub GetTD(iStr,oType,oData) Dim i oType = "" :oData = "" If iStr = "" Then Exit Sub If iStr = "-" Then oData = "-" : Exit Sub If Left(iStr,1) = "" "" Then oType = "REG_SZ" oData = RegSpe(iStr,0) ElseIf LCase(Left(iStr,4)) = "hex:" Then oType = "REG_BINARY" oData = "" "" & Replace(Mid(iStr,5), "," , "" ) & "" "" ElseIf LCase(Left(iStr,6)) = "dword:" Then oType = "REG_DWORD" oData = Hex2Dec(Mid(iStr,7)) Else For i = 0 To 9 If LCase(Left(iStr,7)) = "hex(" & i & "):" Then oType = RegHexType(i) If (i = 1) or (i = 2) or (i = 3) or (i = 7) Then oData = RegHexToAscii(Replace(Mid(iStr,8), "," , "" ),i,RegSeptr) Else oData = "" "对不起,这是不支持的类型。" "" End If Exit For End If Next End If End Sub Function RegSpe(iStr,t) Dim i,f,QPos If Left(iStr,1) = "" "" Then RegSpe = Mid(iStr,2,Len(iStr) - 2) '去除双引号 Else RegSpe = iStr End If '反斜杠与引号字符串处理 QPos = - 1 Do Until QPos = 0 QPos = InstrRev(RegSpe, "\"" ",QPos) If QPos = 0 Then Exit Do f = t For i = QPos To 1 Step - 1 If Mid(RegSpe,i,1) = "\" Then If f = 1 Then RegSpe = Left(RegSpe,i) & "\" & Mid(RegSpe,i + 1) Else f = 1 End If Else Exit For End If Next QPos = i Loop For i = Len(RegSpe) To 1 Step - 1 If Mid(RegSpe,i,1) = "\" Then RegSpe = RegSpe & "\" Else Exit For End If Next '百分号字符处理 RegSpe = Replace(RegSpe, "%" , "%%" ) '对HexData转ASCII的引号字符进行转义 If t = 1 Then RegSpe = Replace(RegSpe, "" "" , "\"" ") End If '添加双引号 RegSpe = "" "" & RegSpe & "" "" End Function Function Hex2Dec(strHex) Hex2Dec = 268435456 * CLng ( "&H" & Left(strHex,1)) Hex2Dec = Hex2Dec + CLng ( "&H" & Mid(strHex,2)) End Function Function RegHexToAscii(iStr,RegTypeIndex,Separator) Dim i,n,sRet,HexL,HexH For i = 1 To Len(iStr) Step 4 HexL = Mid(iStr, i, 2) HexH = Mid(iStr, i + 2, 2) If HexL & HexH = "0000" Then If RegTypeIndex = 7 Then '多字符串(REG_MULTI_SZ)类型 HexChar = Separator Else 'ElseIf RegTypeIndex = 2 Then '可扩充字符串(REG_EXPAND_SZ)类型 Exit For End If ElseIf HexL = "00" Then HexChar = Chr( CLng ( "&H" & HexL)) Else HexChar = ChrW( CLng ( "&H" & HexH & HexL)) End If sRet = sRet & HexChar Next If RegTypeIndex = 7 Then n = Len(Separator) Do Until Right(sRet,n) <> Separator sRet = Left(sRet,Len(sRet) - n) Loop End If RegHexToAscii = RegSpe(sRet,1) End Function |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫