51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 2575|回复: 12
打印 上一主题 下一主题

[原创] 大虾们,给我点思路!

[复制链接]

该用户从未签到

1#
发表于 2010-5-6 13:06:30 | 显示全部楼层
给个txt文件的例子啊,一些你可能用到的

读写注册表:

Set WshShell = CreateObject("wscript.shell")
WshShell.RegDelete '删除
WshShell.RegRead '读取
WshShell.RegWrite '写入


读写txt文件
Set FSO = CreateObject("scripting.filesystemobject")
Set FS = FSO.OpenTextFile("C:\test.txt")
Do While(Not(FS.AtEndOfLine))
        str1 = FS.ReadLine '按行读取
        str2 = FS.Read(1000) '按字节读取
        str3 = FS.ReadAll '全部读取
Loop

FS.Close
Set FS = Nothing
Set FSO = Nothing
回复 支持 反对

使用道具 举报

该用户从未签到

2#
发表于 2010-5-6 13:25:50 | 显示全部楼层
随便啊,txt当然好拉
回复 支持 反对

使用道具 举报

该用户从未签到

3#
发表于 2010-5-6 13:56:06 | 显示全部楼层
估计是文件太大了?我给的只是一些函数的用法啊,不是程序,不能直接用的,你可以组织一下,自己写来试试嘛
回复 支持 反对

使用道具 举报

该用户从未签到

4#
发表于 2010-5-7 02:05:00 | 显示全部楼层
'需求:
'1.读取.txt文件(里面存放的是一些注册表的键值,包括名称、类型、数据)里面每行,然后去注册表里面的查找,看看是否匹配.
'2.然后输出到一个txt文件或者html文件,分别mathed和notmatched列。
'用法:
'1.将 strFileName 值修改为txt文件的位置, 在txt文件中,每行应该一项相应的注册表信息,依次为 "注册表键值名"<tab>"数据类型"<tab>"键值"如: "HKEY_CURRENT_USER\test\asdf        REG_SZ        asdf"
'2.如果注册表键值数据类型为二进制,即REG_BINARY型,需要手工进行转换, 如注册表值为 "00 01 02 03 09 0A 0F 11 1F"则应对应为"000102030910151731"
'3.如果注册表的键值为空,则需要用NULL来代替,如果本来值为"NULL", 则需要设置为"NULL", 如 "HKEY_CURRENT_USER\Console\FaceName        Reg_sZ "NULL"" 或者 "HKEY_CURRENT_USER\Console\FaceName        Reg_sZ NULL"
'4.生成的html文件为当前txt文件的同级目录下,名为 Result.html
On Error Resume Next

Const HKEY_CLASSES_ROOT = 2147483648 '''HKCR
Const HKEY_CURRENT_USER = 2147483649 '''HKCU
Const HKEY_LOCAL_MACHINE = 2147483650 '''HKLM
Const HKEY_Users = 2147483651 '''HKU
Const HKEY_Current_Config = 2147483653 '''HKCC
Const HKEY_DYN_DATA = 2147483654 '''HKDD

Const REG_SZ = 1 '''字符串型
Const REG_EXPAND_SZ = 2 '''扩展字符串型
Const REG_BINARY = 3 '''二进制型
Const REG_DWORD = 4 '''双字节型
Const REG_MULTI_SZ = 7 '''多字符串型

strFileName = "C:\test.txt" '存放注册表信息的地方

'格式化输出
strHTMLHead = "<html><head><title>比较结果</title></head><body><font size=5>文件来源: " + strFileName + ", 比较结果如下:</font><br><br><br><table border=2><tr><td><b>注册表键值名</b></td><td><b>数据类型</b></td><td><b>键值</b></td><td><b>比较结果</b></td></tr>"
strHTMLBody = ""
strHTMLEnd = "</table></body></html>"

Set WshShell = CreateObject("WScript.Shell")
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
Set FSO = CreateObject("scripting.filesystemobject")
Set FS = FSO.OpenTextFile(strFileName)

Do While Not(FS.AtEndOfLine)
        str = FS.ReadLine
        strRegValue = GetRegText(str)
        strMyString = Left(strRegValue,(Len(strRegValue) - 1)) '去掉strRegValue中的最后一个"|"符号
        s = Split(strMyString, "|")
        boolCheckResult1 = CheckType(s(0), s(1))
        If boolCheckResult1 Then
                boolCheckResult2 = CheckValue(s(0), s(1), s(2))
        End If
        boolCheckResult = boolCheckResult1 And boolCheckResult2
        FormatResult s(0), s(1), s(2), boolCheckResult
Loop

FS.Close

strFilePath = FSO.GetParentFolderName(strFileName)
strResultFile = strFilePath & Year(Date) & Month(Date) & Day(Date) & "-" & Hour(Time) & Minute(Time)& Second(Time) & "-Result.html"
Set FS2 = FSO.OpenTextFile(strResultFile, 2, True)

FS2.Write(strHTMLHead + strHTMLBody + strHTMLEnd)
FS2.Close

Set FS = Nothing
Set FS2 = Nothing
Set WshShell = Nothing
Set oReg = Nothing

Function GetRegText(str)
        strString = ""
        Set regEx = New RegExp
        ss = "([\S]*)\s([\S]*)\s([\S]*)$"
        RegEx.Global = True
        RegEx.IgnoreCase = True
        RegEx.Pattern = ss
        Set objReg = RegEx.Execute(str)
        For Each objSubReg In objReg
                For Each strValue In objSubReg.Submatches
                        If strValue = "NULL" Then
                                strValue = ""
                        ElseIf strValue = """NULL""" Then
                                strValue = "NULL"
                        End If
                        strString = strString + strValue + "|"
                Next
        Next
        Set RegEx = Nothing
        GetRegText = strString
End Function


Function CheckType(strString, strType)
        CheckType = False
        ss = "([^\\]+)\\(.*)\\(.*)"
        Set regEx = New RegExp
        regex.Global = True
        regEx.IgnoreCase = True
        regEx.Pattern = ss
        strKeyRoot = regEx.Replace(strString, "$1")
        strKeyPath = regEx.Replace(strString, "$2")
        arrValueName = regEx.Replace(strString, "$3")
       
        Select Case UCase(strKeyRoot)
                Case "HKEY_CLASSES_ROOT"
                        KeyRoot = HKEY_CLASSES_ROOT
                Case "HKEY_CURRENT_USER"
                        KeyRoot = HKEY_CURRENT_USER
                Case "HKEY_LOCAL_MACHINE"
                        KeyRoot = HKEY_LOCAL_MACHINE
                Case "HKEY_USERS"
                        KeyRoot = HKEY_Users
                Case "HKEY_CURRENT_CONFIG"
                        KeyRoot = HKEY_Current_Config
                Case "HKEY_DYN_DATA"
                        KeyRoot = HKEY_DYN_DATA
        End Select
       
        Select Case UCase(strType)
                Case "REG_SZ"
                        arrValueType = REG_SZ
                Case "REG_EXPAND_SZ"
                        arrValueType = REG_EXPAND_SZ
                Case "REG_BINARY"
                        arrValueType = REG_BINARY
                Case "REG_DWORD"
                        arrValueType = REG_DWORD
                Case "REG_MULTI_SZ"
                        arrValueType = REG_MULTI_SZ
        End Select
       
        oReg.EnumValues KeyRoot, strKeyPath, arrValueNames, arrValueTypes
        If IsNull(arrValueTypes) Then
                CheckType = False
                Exit Function
        End If
        For i=0 To UBound(arrValueNames)
                If arrValueNames(i) = arrValueName Then
                        If arrValueTypes(i) = arrValueType Then
                                CheckType = True
                                Exit Function
                        Else
                                CheckType = False
                                Exit Function
                        End If
                End If
        Next
        Set regEx = Nothing
End Function


Function CheckValue(strRegPath, strType, strRegValue)
        CheckValue = False
        strActualValue = WshShell.RegRead(strRegPath)
        If strType <> "REG_BINARY" Then
                If CStr(strActualValue) = CStr(strRegValue) Then
                        CheckValue = True
                        Exit Function
                End If
        Else  '判断为二进制时是否相等
                boolTempResult1 = True
                boolTempResult2 = True
                For i = 0 To UBound(strActualValue)
                        strValue = CInt(Right(Left(strRegValue, 2*(i+1)), 2))
                        If strValue <> strActualValue(i) Then
                                boolTempResult2 = False
                        End If
                Next
                CheckValue = boolTempResult1 And boolTempResult2
                Exit Function
        End If
End Function

Function FormatResult(strString1, strString2,strString3, boolResult)
        If boolResult Then
                strHTMLBody = strHTMLBody + "<tr bgcolor=Lime><td>" + strString1 + "</td><td>" + strString2 + "</td><td>" + strString3 + "</td><td>" + "匹配</td></tr>"
        Else
                strHTMLBody = strHTMLBody + "<tr bgcolor=red><td>" + strString1 + "</td><td>" + strString2 + "</td><td>" + strString3 + "</td><td>" + "不匹配</td></tr>"
        End If
End Function

如上

[ 本帖最后由 风雪夜归人 于 2010-5-7 11:22 编辑 ]
回复 支持 反对

使用道具 举报

该用户从未签到

5#
发表于 2010-5-7 02:10:40 | 显示全部楼层
啥意思?回复需要审核?
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-5-7 15:38 , Processed in 0.069635 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表