这段时期,关于处理 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 格式。替换文件中的字符串,添加行。- Dim msg, ws, oArgs, fd, sd
- msg = "Command prompt: HojoHE.vbs -SA.INF -TD:\a -IC:\a\b -L0409"
- 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) = "-T") and (Left(oArgs(2),2) = "-I") and (Left(oArgs(2),2) = "-L") Then
- loc = Mid(oArgs(3), 3)
- sd = Mid(oArgs(2), 3) & "\"
- fd = Mid(oArgs(1), 3) & "\"
- Else MsgBox "Input error!"&vbcrlf&vbcrlf&msg : WScript.Quit
- End If
- If oArgs.Count = 3 Then
- If Left(oArgs(0),2) = "-S" and (Left(oArgs(1),2) = "-T") and (Left(oArgs(2),2) = "-I") Then
- sd = Mid(oArgs(2), 3) & "\"
- fd = Mid(oArgs(1), 3) & "\"
- Else MsgBox "Input error!"&vbcrlf&vbcrlf&msg : WScript.Quit
- End If
- ElseIf oArgs.Count = 2 Then
- If Left(oArgs(0),2) = "-S" and (Left(oArgs(1),2) = "-T") Then
- sd = Left(WScript.ScriptFullName,InStrRev(WScript.ScriptFullName,"\"))
- fd = Mid(oArgs(1), 3) & "\"
- Else MsgBox "Input error!"&vbcrlf&vbcrlf&msg : WScript.Quit
- End If
- ElseIf oArgs.Count = 1 Then
- If oArgs(0) = "?" Then
- MsgBox msg
- ElseIf Left(oArgs(0),2) = "-S" Then
- sd = Left(WScript.ScriptFullName,InStrRev(WScript.ScriptFullName,"\"))
- fd = Left(WScript.ScriptFullName,InStrRev(WScript.ScriptFullName,"\"))
- Else MsgBox "Input error!"&vbcrlf&vbcrlf&msg : WScript.Quit
- End If
- Else MsgBox "Input error!"&vbcrlf&vbcrlf&msg : WScript.Quit
- End If
-
- Dim ifile, file, fso, f, s, ss
- Set fso = CreateObject("Scripting.FileSystemObject")
- If fso.FolderExists(fd) = False Then fso.CreateFolder fd
- If LCase(Mid(oArgs(0), 3)) = "default" Then
- If (fso.FileExists(sd & "HIVEDEF.INF")) Then
- ifile = sd & "HIVEDEF.INF"
- fso.CopyFile ifile,fd,true
- file = fd & "HIVEDEF.INF"
- Call GetFile()
- End If
- ElseIf LCase(Mid(oArgs(0), 3)) = "software" Then
- If (fso.FileExists(sd & "HIVESFT.INF")) Then
- ifile = sd & "HIVESFT.INF"
- fso.CopyFile ifile,fd,true
- file = fd & "HIVESFT.INF"
- Call GetFile()
- End If
- If (fso.FileExists(sd & "HIVECLS.INF")) Then
- ifile = sd & "HIVECLS.INF"
- fso.CopyFile ifile,fd,true
- file = fd & "HIVECLS.INF"
- Call GetFile()
- End If
- If (fso.FileExists(sd & "HIVESXS.INF")) Then
- ifile = sd & "HIVESXS.INF"
- fso.CopyFile ifile,fd,true
- file = fd & "HIVESXS.INF"
- Call GetFile()
- End If
- If (fso.FileExists(sd & "DMREG.INF")) Then
- ifile = sd & "DMREG.INF"
- fso.CopyFile ifile,fd,true
- file = fd & "DMREG.INF"
- Call GetFile()
- End If
- If (fso.FileExists(sd & "HIVCLS32.INF")) Then
- ifile = sd & "HIVCLS32.INF"
- fso.CopyFile ifile,fd,true
- file = fd & "HIVCLS32.INF"
- Call GetFile()
- End If
- If (fso.FileExists(sd & "HIVSFT32.INF")) Then
- ifile = sd & "HIVSFT32.INF"
- fso.CopyFile ifile,fd,true
- file = fd & "HIVSFT32.INF"
- Call GetFile()
- End If
- ElseIf LCase(Mid(oArgs(0), 3)) = "setupreg.hiv" Then
- If (fso.FileExists(sd & "HIVESYS.INF")) Then
- ifile = sd & "HIVESYS.INF"
- fso.CopyFile ifile,fd,true
- file = fd & "HIVESYS.INF"
- Call GetFile()
- End If
- Else MsgBox "The parameter isn't supported!"&vbcrlf&vbcrlf&"Must be 'default', or 'software', or 'setupreg.hiv'." : WScript.Quit
- End If
-
- Function GetFile()
- Set f = fso.OpenTextFile(file, 1, false, GetFileFormat(file))
- s = f.ReadAll : f.Close
- If file = fd & "HIVEDEF.INF" Then
- s = ReplaceStr(s, "HKCU, *""", "HKLM,""WB-default\")
- s = ReplaceStr(s, "HKLM, *""SYSTEM\\", "HKLM,""WB-setup\")
- s = ReplaceStr(s, "HKLM, *""SOFTWARE\\", "HKLM,""WB-software\")
- End If
- If file = fd & "HIVESFT.INF" Then
- s = ReplaceStr(s, "HKCU, *""", "HKLM,""WB-default\")
- s = ReplaceStr(s, "HKLM, *""SYSTEM\\", "HKLM,""WB-setup\")
- s = ReplaceStr(s, "HKLM, *""SOFTWARE\\", "HKLM,""WB-software\")
- End If
- If file = fd & "HIVESXS.INF" Then
- s = ReplaceStr(s, "HKCU, *""", "HKLM,""WB-default\")
- s = ReplaceStr(s, "HKLM, *""SYSTEM\\", "HKLM,""WB-setup\")
- s = ReplaceStr(s, "HKLM, *""SOFTWARE\\", "HKLM,""WB-software\")
- End If
- If file = fd & "HIVECLS.INF" Then
- s = ReplaceStr(s, "HKLM, *""SOFTWARE\\", "HKLM,""WB-software\")
- s = ReplaceStr(s, "HKCR, *""", "HKLM,""WB-software\Classes\")
- s = ReplaceStr(s, "HKCR,\.", "HKLM,WB-software\Classes\.")
- s = ReplaceStr(s, "HKCU, *""", "HKLM,""WB-default\")
- s = ReplaceStr(s, "HKLM, *""SYSTEM\\", "HKLM,""WB-setup\")
- End If
- If file = fd & "DMREG.INF" Then
- s = ReplaceStr(s, "HKCU, *""", "HKLM,""WB-default\")
- s = ReplaceStr(s, "HKLM, *""SYSTEM\\", "HKLM,""WB-setup\")
- s = ReplaceStr(s, "HKLM, *""SOFTWARE\\", "HKLM,""WB-software\")
- End If
- If file = fd & "HIVSFT32.INF" Then
- s = ReplaceStr(s, "HKCU, *""", "HKLM,""WB-default\")
- s = ReplaceStr(s, "HKLM, *""SYSTEM\\", "HKLM,""WB-setup\")
- s = ReplaceStr(s, "HKLM, *""SOFTWARE\\", "HKLM,""WB-software\")
- End If
- If file = fd & "HIVCLS32.INF" Then
- s = ReplaceStr(s, "HKLM, *""SOFTWARE\\", "HKLM,""WB-software\")
- s = ReplaceStr(s, "HKCR, *""", "HKLM,""WB-software\Classes\")
- s = ReplaceStr(s, "HKCR,\.", "HKLM,WB-software\Classes\.")
- s = ReplaceStr(s, "HKCU, *""", "HKLM,""WB-default\")
- s = ReplaceStr(s, "HKLM, *""SYSTEM\\", "HKLM,""WB-setup\")
- End If
- If file = fd & "HIVESYS.INF" Then
- s = ReplaceStr(s, "HKCU, *""", "HKLM,""WB-default\")
- s = ReplaceStr(s, "HKLM, *""SYSTEM\\", "HKLM,""WB-setup\")
- s = ReplaceStr(s, "HKLM, *""SOFTWARE\\", "HKLM,""WB-software\")
- End If
- fso.OpenTextFile(file, 2, true, -1).Write s
- ws.Run "rundll32 syssetup,SetupInfObjectInstallAction DefaultInstall 128 " & file, , true
- End Function
- WScript.Quit
-
- Function GetFileFormat(ByVal file)
- Dim Bin
- with CreateObject("Adodb.Stream")
- .Type = 1
- .Mode = 3
- .Open
- .Position = 0
- .Loadfromfile file
- Bin = .read(2)
- End with
- If AscB(MidB(Bin,1,1))=&HFF and AscB(MidB(Bin,2,1))=&HFE Then
- GetFileFormat = -1 ''unicode
- Else GetFileFormat = 0 ''ansi
- End If
- End Function
-
- Function ReplaceStr(ByVal s, pattern, s1)
- Dim re
- If Left(s, 16) <> "[DEFAULTINSTALL]" Then
- s = "[DEFAULTINSTALL]" & vbCrLf & "ADDREG = AddReg" & vbCrLf & s
- End If
- Set re = New RegExp
- re.Pattern = pattern
- re.Global = true
- re.IgnoreCase = true
- ReplaceStr = re.Replace(s, s1)
- End Function
复制代码
|