Board logo

标题: [问题求助] 求助处理unicode格式文件的VBS代码修改并精简 [打印本页]

作者: yuanyannian    时间: 2014-8-6 11:19     标题: 求助处理unicode格式文件的VBS代码修改并精简

这段时期,关于处理 unicode 格式文件,不断劳烦 apang 等老师,非常抱歉,这里衷心感谢!!!
仍然是这个代码,由于本人对批处理是丈二外行,不得要领,不断地更改代码功能,所以也就不断地麻烦各位老师指教、修改,请见谅。
下面的代码是在 apang 等老师指点下,自己东拼西凑起来的,虽然可以运行,但自己都觉得太外行了,所以再请老师修改。


功能需求:
1. 命令行输入 4 个参数,各个参数前的如 -S 等要求正确输入,不要求必须全部输入 4 个参数,但如输入1个,或2个,或3个,或4个,则
   分别处理。当第一个参数为 ? ,显示帮助信息,即 msg。其它情况下则要求给出 sd、fd 路径。
2. 第 4 个参数暂时赋值给变量,以后会用到。
3. 判断第一个参数 -S 后面的文件名,如 default 或 setupreg.hiv,仅处理1个文件,如 software 则需要处理多个文件,需要
   先判断 sd 目录中如存在该文件,先复制到 fd 目录中再处理,如不存在就忽略。
4. 处理文件过程:先判断是否 unicode 格式,否则先转换为 unicode 格式。替换文件中的字符串,添加行。
  1. Dim msg, ws, oArgs, fd, sd
  2. msg = "Command prompt: HojoHE.vbs -SA.INF -TD:\a -IC:\a\b -L0409"
  3. Set ws = CreateObject("WScript.Shell")
  4. Set oArgs = WScript.Arguments
  5. If oArgs.Count = 4 Then
  6.     If Left(oArgs(0),2) = "-S" and (Left(oArgs(1),2) = "-T") and (Left(oArgs(2),2) = "-I") and (Left(oArgs(2),2) = "-L") Then
  7.         loc = Mid(oArgs(3), 3)
  8.         sd = Mid(oArgs(2), 3) & "\"
  9.         fd = Mid(oArgs(1), 3) & "\"
  10.     Else MsgBox "Input error!"&vbcrlf&vbcrlf&msg : WScript.Quit
  11.     End If
  12. If oArgs.Count = 3 Then
  13.     If Left(oArgs(0),2) = "-S" and (Left(oArgs(1),2) = "-T") and (Left(oArgs(2),2) = "-I") Then
  14.         sd = Mid(oArgs(2), 3) & "\"
  15.         fd = Mid(oArgs(1), 3) & "\"
  16.     Else MsgBox "Input error!"&vbcrlf&vbcrlf&msg : WScript.Quit
  17.     End If
  18. ElseIf oArgs.Count = 2 Then
  19.     If Left(oArgs(0),2) = "-S" and (Left(oArgs(1),2) = "-T") Then
  20.         sd = Left(WScript.ScriptFullName,InStrRev(WScript.ScriptFullName,"\"))
  21.         fd = Mid(oArgs(1), 3) & "\"
  22.     Else MsgBox "Input error!"&vbcrlf&vbcrlf&msg : WScript.Quit
  23.     End If
  24. ElseIf oArgs.Count = 1 Then
  25.     If oArgs(0) = "?" Then
  26.         MsgBox msg
  27.     ElseIf Left(oArgs(0),2) = "-S" Then
  28.         sd = Left(WScript.ScriptFullName,InStrRev(WScript.ScriptFullName,"\"))
  29.         fd = Left(WScript.ScriptFullName,InStrRev(WScript.ScriptFullName,"\"))
  30.     Else MsgBox "Input error!"&vbcrlf&vbcrlf&msg : WScript.Quit
  31.     End If
  32. Else MsgBox "Input error!"&vbcrlf&vbcrlf&msg : WScript.Quit
  33. End If
  34. Dim ifile, file, fso, f, s, ss
  35. Set fso = CreateObject("Scripting.FileSystemObject")
  36. If fso.FolderExists(fd) = False Then fso.CreateFolder fd
  37. If LCase(Mid(oArgs(0), 3)) = "default" Then
  38.     If (fso.FileExists(sd & "HIVEDEF.INF")) Then
  39.         ifile = sd & "HIVEDEF.INF"
  40.         fso.CopyFile ifile,fd,true
  41.         file = fd & "HIVEDEF.INF"
  42.         Call GetFile()
  43.     End If
  44.     ElseIf LCase(Mid(oArgs(0), 3)) = "software" Then
  45.         If (fso.FileExists(sd & "HIVESFT.INF")) Then
  46.             ifile = sd & "HIVESFT.INF"
  47.             fso.CopyFile ifile,fd,true
  48.             file = fd & "HIVESFT.INF"
  49.             Call GetFile()
  50.         End If
  51.         If (fso.FileExists(sd & "HIVECLS.INF")) Then
  52.             ifile = sd & "HIVECLS.INF"
  53.             fso.CopyFile ifile,fd,true
  54.             file = fd & "HIVECLS.INF"
  55.             Call GetFile()
  56.         End If
  57.         If (fso.FileExists(sd & "HIVESXS.INF")) Then
  58.             ifile = sd & "HIVESXS.INF"
  59.             fso.CopyFile ifile,fd,true
  60.             file = fd & "HIVESXS.INF"
  61.             Call GetFile()
  62.         End If
  63.         If (fso.FileExists(sd & "DMREG.INF")) Then
  64.             ifile = sd & "DMREG.INF"
  65.             fso.CopyFile ifile,fd,true
  66.             file = fd & "DMREG.INF"
  67.             Call GetFile()
  68.         End If
  69.         If (fso.FileExists(sd & "HIVCLS32.INF")) Then
  70.             ifile = sd & "HIVCLS32.INF"
  71.             fso.CopyFile ifile,fd,true
  72.             file = fd & "HIVCLS32.INF"
  73.             Call GetFile()
  74.         End If
  75.         If (fso.FileExists(sd & "HIVSFT32.INF")) Then
  76.             ifile = sd & "HIVSFT32.INF"
  77.             fso.CopyFile ifile,fd,true
  78.             file = fd & "HIVSFT32.INF"
  79.             Call GetFile()
  80.         End If
  81.     ElseIf LCase(Mid(oArgs(0), 3)) = "setupreg.hiv" Then
  82.         If (fso.FileExists(sd & "HIVESYS.INF")) Then
  83.             ifile = sd & "HIVESYS.INF"
  84.             fso.CopyFile ifile,fd,true
  85.             file = fd & "HIVESYS.INF"
  86.             Call GetFile()
  87.         End If
  88.     Else MsgBox "The parameter isn't supported!"&vbcrlf&vbcrlf&"Must be 'default', or 'software', or 'setupreg.hiv'." : WScript.Quit
  89. End If
  90. Function GetFile()
  91.     Set f = fso.OpenTextFile(file, 1, false, GetFileFormat(file))
  92.     s = f.ReadAll : f.Close
  93.     If file = fd & "HIVEDEF.INF" Then
  94.         s = ReplaceStr(s, "HKCU, *""", "HKLM,""WB-default\")
  95.         s = ReplaceStr(s, "HKLM, *""SYSTEM\\", "HKLM,""WB-setup\")
  96.         s = ReplaceStr(s, "HKLM, *""SOFTWARE\\", "HKLM,""WB-software\")
  97.     End If
  98.     If file = fd & "HIVESFT.INF" Then
  99.         s = ReplaceStr(s, "HKCU, *""", "HKLM,""WB-default\")
  100.         s = ReplaceStr(s, "HKLM, *""SYSTEM\\", "HKLM,""WB-setup\")
  101.         s = ReplaceStr(s, "HKLM, *""SOFTWARE\\", "HKLM,""WB-software\")
  102.     End If
  103.     If file = fd & "HIVESXS.INF" Then
  104.         s = ReplaceStr(s, "HKCU, *""", "HKLM,""WB-default\")
  105.         s = ReplaceStr(s, "HKLM, *""SYSTEM\\", "HKLM,""WB-setup\")
  106.         s = ReplaceStr(s, "HKLM, *""SOFTWARE\\", "HKLM,""WB-software\")
  107.     End If
  108.     If file = fd & "HIVECLS.INF" Then
  109.         s = ReplaceStr(s, "HKLM, *""SOFTWARE\\", "HKLM,""WB-software\")
  110.         s = ReplaceStr(s, "HKCR, *""", "HKLM,""WB-software\Classes\")
  111.         s = ReplaceStr(s, "HKCR,\.", "HKLM,WB-software\Classes\.")
  112.         s = ReplaceStr(s, "HKCU, *""", "HKLM,""WB-default\")
  113.         s = ReplaceStr(s, "HKLM, *""SYSTEM\\", "HKLM,""WB-setup\")
  114.     End If
  115.     If file = fd & "DMREG.INF" Then
  116.         s = ReplaceStr(s, "HKCU, *""", "HKLM,""WB-default\")
  117.         s = ReplaceStr(s, "HKLM, *""SYSTEM\\", "HKLM,""WB-setup\")
  118.         s = ReplaceStr(s, "HKLM, *""SOFTWARE\\", "HKLM,""WB-software\")
  119.     End If
  120.     If file = fd & "HIVSFT32.INF" Then
  121.         s = ReplaceStr(s, "HKCU, *""", "HKLM,""WB-default\")
  122.         s = ReplaceStr(s, "HKLM, *""SYSTEM\\", "HKLM,""WB-setup\")
  123.         s = ReplaceStr(s, "HKLM, *""SOFTWARE\\", "HKLM,""WB-software\")
  124.     End If
  125.     If file = fd & "HIVCLS32.INF" Then
  126.         s = ReplaceStr(s, "HKLM, *""SOFTWARE\\", "HKLM,""WB-software\")
  127.         s = ReplaceStr(s, "HKCR, *""", "HKLM,""WB-software\Classes\")
  128.         s = ReplaceStr(s, "HKCR,\.", "HKLM,WB-software\Classes\.")
  129.         s = ReplaceStr(s, "HKCU, *""", "HKLM,""WB-default\")
  130.         s = ReplaceStr(s, "HKLM, *""SYSTEM\\", "HKLM,""WB-setup\")
  131.     End If
  132.     If file = fd & "HIVESYS.INF" Then
  133.         s = ReplaceStr(s, "HKCU, *""", "HKLM,""WB-default\")
  134.         s = ReplaceStr(s, "HKLM, *""SYSTEM\\", "HKLM,""WB-setup\")
  135.         s = ReplaceStr(s, "HKLM, *""SOFTWARE\\", "HKLM,""WB-software\")
  136.     End If
  137.     fso.OpenTextFile(file, 2, true, -1).Write s
  138.     ws.Run "rundll32 syssetup,SetupInfObjectInstallAction DefaultInstall 128 " & file, , true
  139. End Function
  140. WScript.Quit
  141. Function GetFileFormat(ByVal file)
  142.     Dim Bin
  143.     with CreateObject("Adodb.Stream")
  144.         .Type = 1
  145.         .Mode = 3
  146.         .Open
  147.         .Position = 0
  148.         .Loadfromfile file
  149.         Bin = .read(2)
  150.     End with
  151.     If AscB(MidB(Bin,1,1))=&HFF and AscB(MidB(Bin,2,1))=&HFE Then
  152.         GetFileFormat = -1   ''unicode
  153.     Else GetFileFormat = 0   ''ansi
  154.     End If
  155. End Function
  156. Function ReplaceStr(ByVal s, pattern, s1)
  157.     Dim re
  158.     If Left(s, 16) <> "[DEFAULTINSTALL]" Then
  159.         s = "[DEFAULTINSTALL]" & vbCrLf & "ADDREG = AddReg" & vbCrLf & s
  160.     End If
  161.     Set re = New RegExp
  162.     re.Pattern = pattern
  163.     re.Global = true
  164.     re.IgnoreCase = true
  165.     ReplaceStr = re.Replace(s, s1)
  166. End Function
复制代码

作者: 523066680    时间: 2014-8-6 19:23

holy high!
(好厉害)
作者: yuanyannian    时间: 2014-8-7 07:55

参照网上的例子,自己修改如下,不知对不对?目前运行好像没有提示错误之处。
但有几个问题请教(见代码中):
  1. Dim msg, ws, oArgs, sLoca, iPath, tPath
  2. msg = "Command prompt: HojoHE.exe -Sdefault -TD:\aa -IC:\aa\bb -L00000409"
  3. Set ws = CreateObject("WScript.Shell")
  4. Set oArgs = WScript.Arguments
  5. Select Case oArgs.Count
  6.     Case 4
  7.         If Left(oArgs(0),2) = "-S" and (Left(oArgs(1),2) = "-T") and (Left(oArgs(2),2) = "-I") and (Left(oArgs(3),2) = "-L") Then
  8.             sLoca = Mid(oArgs(3), 3)
  9.             iPath = Mid(oArgs(2), 3) & "\"
  10.             tPath = Mid(oArgs(1), 3) & "\"
  11.         Else MsgBox "Input error!"&vbcrlf&vbcrlf&msg : WScript.Quit
  12.         End If
  13.     Case 3
  14.         If Left(oArgs(0),2) = "-S" and (Left(oArgs(1),2) = "-T") and (Left(oArgs(2),2) = "-I") Then
  15.             iPath = Mid(oArgs(2), 3) & "\"
  16.             tPath = Mid(oArgs(1), 3) & "\"
  17.         Else MsgBox "Input error!"&vbcrlf&vbcrlf&msg : WScript.Quit
  18.         End If
  19.     Case 2
  20.         If Left(oArgs(0),2) = "-S" and (Left(oArgs(1),2) = "-T") Then
  21.             iPath = Left(WScript.ScriptFullName,InStrRev(WScript.ScriptFullName,"\"))
  22.             tPath = Mid(oArgs(1), 3) & "\"
  23.         Else MsgBox "Input error!"&vbcrlf&vbcrlf&msg : WScript.Quit
  24.         End If
  25.     Case 1
  26.         If oArgs(0) = "?" Then
  27.             MsgBox msg : WScript.Quit
  28.         ElseIf Left(oArgs(0),2) = "-S" Then
  29.             iPath = Left(WScript.ScriptFullName,InStrRev(WScript.ScriptFullName,"\"))
  30.             tPath = Left(WScript.ScriptFullName,InStrRev(WScript.ScriptFullName,"\"))
  31.         Else MsgBox "Input error!"&vbcrlf&vbcrlf&msg : WScript.Quit
  32.         End If
  33.     Case Else MsgBox "Input error!"&vbcrlf&vbcrlf&msg : WScript.Quit
  34. End Select
  35. '35
  36. Dim file, fso, f, s, ss                    ‘’此处的 f, s 是否应该放在 Function ProcessFile() 之下?
  37. Set fso = CreateObject("Scripting.FileSystemObject")
  38. If fso.FolderExists(tPath) = False Then fso.CreateFolder tPath
  39. Dim MyArray()                             ''定义一个一维动态数组
  40. ReDim MyArray(7)                          ''定义数组大小
  41.     Select Case LCase(Mid(oArgs(0), 3))
  42.         Case "default"
  43.             MyArray(0) = "HIVEDEF.INF"    ''为数组赋值
  44.         Case "software"
  45.             MyArray(1) = "HIVESFT.INF"
  46.             MyArray(2) = "HIVECLS.INF"
  47.             MyArray(3) = "HIVESXS.INF"
  48.             MyArray(4) = "HIVCLS32.INF"
  49.             MyArray(5) = "HIVSFT32.INF"
  50.             MyArray(6) = "DMREG.INF"
  51.         Case "setupreg.hiv"
  52.             MyArray(7) = "HIVESYS.INF"
  53.         Case Else MsgBox "The parameter isn't supported!"&vbcrlf&vbcrlf&"Must be 'default', or 'software', or 'setupreg.hiv'." : WScript.Quit
  54.     End Select
  55. For i=0 To UBound(MyArray)
  56. ss = MyArray(i)                          ''循环遍历数组,并输出数组值给变量 ss
  57.     If (fso.FileExists(iPath & ss)) Then
  58.         fso.CopyFile iPath & ss,tPath,true
  59.         file = tPath & ss
  60.         Call ProcessFile()
  61.     End If
  62. Next
  63. Function ProcessFile()
  64.     Set f = fso.OpenTextFile(file, 1, false, GetFileFormat(file))
  65.     s = f.ReadAll : f.Close
  66.     s = ReplaceStr(s, "HKCU, *""", "HKLM,""WB-default\")     ‘’如果存在就替换,否则忽略,       这样对吗?下同
  67.     s = ReplaceStr(s, "HKLM, *""SYSTEM\\", "HKLM,""WB-setup\")
  68.     s = ReplaceStr(s, "HKLM, *""SOFTWARE\\", "HKLM,""WB-software\")
  69.     s = ReplaceStr(s, "HKCR, *""", "HKLM,""WB-software\Classes\")
  70.     s = ReplaceStr(s, "HKCR,\.", "HKLM,WB-software\Classes\.")
  71.     fso.OpenTextFile(file, 2, true, -1).Write s
  72. ''    ws.Run "rundll32 syssetup,SetupInfObjectInstallAction DefaultInstall 128 " & file, , true       ‘’此处如何判断是否安装成功,如果安装成功,如何删除 file?
  73. End Function
  74. WScript.Quit
  75. Function GetFileFormat(ByVal file)
  76.     Dim Bin
  77.     with CreateObject("Adodb.Stream")
  78.         .Type = 1
  79.         .Mode = 3
  80.         .Open
  81.         .Position = 0
  82.         .Loadfromfile file
  83.         Bin = .read(2)
  84.     End with
  85.     If AscB(MidB(Bin,1,1))=&HFF and AscB(MidB(Bin,2,1))=&HFE Then
  86.         GetFileFormat = -1   ''unicode
  87.     Else GetFileFormat = 0   ''ansi
  88.     End If
  89. End Function
  90. Function ReplaceStr(ByVal s, pattern, s1)
  91.     Dim re
  92.     If Left(s, 16) <> "[DEFAULTINSTALL]" Then
  93.         s = "[DEFAULTINSTALL]" & vbCrLf & "ADDREG = AddReg" & vbCrLf & s
  94.     End If
  95.     Set re = New RegExp
  96.     re.Pattern = pattern
  97.     re.Global = true
  98.     re.IgnoreCase = true
  99.     ReplaceStr = re.Replace(s, s1)
  100. End Function
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2