51Testing软件测试论坛

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

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 2553|回复: 1
打印 上一主题 下一主题

[原创] VBS判断系统类型删除cookie和cache

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2011-7-6 20:47:53 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 zk_testing 于 2011-7-7 09:57 编辑

下面是我写的一段代码,VBS删除cookie和cache,运行不成功,但是没找到错误点。请高手帮忙。


'*********************************************
'Del_CookieCache返回一个长度为3的Dictionary
'第一个为cookie路径
'第二个为cache路经
'第三个为错误状态(用于判断是否是WIN7或XP系统)
'*********************************************

Function Del_CookieCache()
'设置Dictionary用于存储返回的三个字符串
Dim obj_Dictionary,a,b,c
Set obj_Dictionary = CreateObject ("Scripting.Dictionary")
'获取当前操作系统
Dim wmiService,wmiObjects,wmiObject
Set wmiService = GetObject("winmgmts:\\.\root\cimv2")
Set wmiObjects = wmiService.ExecQuery("SELECT * FROM Win32_OperatingSystem")
For Each wmiObject In wmiObjects
     str_Systype =  wmiObject.Caption   
next

str_Systype = Trim(str_Systype)

MsgBox ("操作系统类型:"&str_Systype)

'根据操作系统类型设置cookie与cache在注册表的位置
Dim str_cookiePath_forWin7,str_CachePath_forWin7
Dim str_cookiePath_forWinXP,str_CachePath_forWinXP
Dim str_CookiePath,str_CachePath
Dim objWshShell,objWshShell_P
Dim error_code
error_code =0

Set objWshShell = CreateObject("wscript.shell")

str_cookiePath_forWin7 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\cookies"
str_cookiePath_forWinXP = "KEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Cache\Special Paths\Cookies\Directory"
str_CachePath_forWin7 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\cache"
str_CachePath_forWinXP = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Cache\Paths\Directory"


Select Case str_Systype
  Case "Microsoft Windows 7 专业版"  
   str_CookiePath = objWshShell.RegRead(str_cookiePath_forWin7)
   str_CachePath = objWshShell.RegRead(str_CachePath_forWin7)
   error_code = 0
  Case "Microsoft Windows XP Professional"
   str_CookiePath = objWshShell.RegRead(str_cookiePath_forWinXP)
   str_CachePath = objWshShell.RegRead(str_CachePath_forWinXP)
   error_code = 0
  Case Else
   str_CookiePath = ""
   str_CachePath = ""
   error_code = 1
End Select
MsgBox str_CookiePath&str_CachePath


obj_Dictionary.Add "0",error_code
obj_Dictionary.Add "1",str_CookiePath
obj_Dictionary.Add "2",str_CachePath

Set Del_CookieCache = obj_Dictionary
   
' For i = 0 To 2 Step 1
'  MsgBox  obj_Dictionary.Item(i)
' Next
End Function   
Function del_exc()
Dim objTmp_Dictionary,dicArray,fso

'定义要删除的cookie类型
cookie_type = "\*paipai*"


Set objTmp_Dictionary = Del_CookieCache
dicArray = objTmp_Dictionary.Items
MsgBox dicArray(0)
If trim(dicArray(0)) = "1" Then
  MsgBox "当前操作系统未添加"
  Exit Function
Else If trim(dicArray(0)) = "0" Then
  Set fso = CreateObject("Scripting.filesystemobject")
  
  If (fso.folderexists(dicArray(1))) Then
   dicArray(1) = dicArray(1) +"\*.*"
   fso.deletefile(dicArray(1))
   MsgBox "cookie删除成功"
  End If
   
  If (fso.folderexists(dicArray(2))) Then
   dicArray(2) = dicArray(2) +"\*.*"
   fso.deletefile(dicArray(2))
   MsgBox "删除cache成功"
  End If
  
  'MsgBox ("error_code:"&a(0)&vbLf& _
  '  "str_CookiePath:"&vbLf&a(1)&vbLf& _
  '  "str_CachePath:"&vbLf&a(2))
  End If
End If
End Function
Call  del_exc()
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
 楼主| 发表于 2011-7-8 17:48:51 | 只看该作者
我自己搞定了。。
还能进一步优化一下。下面是代码。





'*********************************************
'Del_CookieCache返回一个长度为3的Dictionary
'第一个为cookie路径
'第二个为cache路经
'第三个为错误状态(用于判断是否是WIN7或XP系统)
'*********************************************


Function Del_CookieCache()

        '设置Dictionary用于存储返回的三个字符串
        Dim obj_Dictionary,a,b,c
        Set obj_Dictionary = CreateObject ("Scripting.Dictionary")

'获取当前操作系统
        Dim wmiService,wmiObjects,wmiObject
        Set wmiService = GetObject("winmgmts:\\.\root\cimv2")
        Set wmiObjects = wmiService.ExecQuery("SELECT * FROM Win32_OperatingSystem")
        For Each wmiObject In wmiObjects
            str_Systype =  wmiObject.Caption         
        next
       
        str_Systype = Trim(str_Systype)
       
'        MsgBox ("操作系统类型:"&str_Systype)
       
        '根据操作系统类型设置cookie与cache在注册表的位置
        Dim str_cookiePath_forWin7,str_CachePath_forWin7
        Dim str_cookiePath_forWinXP,str_CachePath_forWinXP
        Dim str_CookiePath,str_CachePath
        Dim objWshShell,objWshShell_P
        Dim error_code
        error_code = 0
       
        Set objWshShell = CreateObject("wscript.shell")
       
        str_cookiePath_forWin7 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\cookies"
        str_cookiePath_forWinXP = "KEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Cache\Special Paths\Cookies\Directory"
        str_CachePath_forWin7 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\cache"
        str_CachePath_forWinXP = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Cache\Paths\Directory"
       
       
        Select Case str_Systype
                Case "Microsoft Windows 7 专业版"        
                        str_CookiePath = objWshShell.RegRead(str_cookiePath_forWin7)
                        str_CachePath = objWshShell.RegRead(str_CachePath_forWin7)
                        error_code = 0
                Case "Microsoft Windows XP Professional"
                        str_CookiePath = objWshShell.RegRead(str_cookiePath_forWinXP)
                        str_CachePath = objWshShell.RegRead(str_CachePath_forWinXP)
                        error_code = 0
                Case Else
                        str_CookiePath = ""
                        str_CachePath = ""
                        error_code = 1
        End Select

        obj_Dictionary.Add "0",CStr(error_code        )
        obj_Dictionary.Add "1",CStr(str_CookiePath)
        obj_Dictionary.Add "2",CStr(str_CachePath)
       
        Set Del_CookieCache = obj_Dictionary
                               
'        For i = 0 To 2 Step 1
'                MsgBox         obj_Dictionary.Item(i)
'        Next

End Function                

Function del_exc()
        Dim objTmp_Dictionary,wsh
        Dim error_code,cookie_path,cache_path
        '定义要删除的cookie类型
        cookie_type = "\*paipai*"
       
        Set objTmp_Dictionary = Del_CookieCache
        error_code = objTmp_Dictionary.Item("0")
        cookie_path = objTmp_Dictionary.Item("1")
        cache_path = objTmp_Dictionary.Item("2")
       
       
'        MsgBox cookie_path
'                由于%userprofile%是环境变量,不是绝对路径
'                所以不能被filesystemobject对象识别,不能使用folderexists来判断,若必须用,则需要获取当前用户名并替换%userprofile%
'                需要改为cmd构造命令来执行,


        If error_code = "1" Then
                MsgBox "当前操作系统未添加"
                Exit Function
        ElseIf error_code = "0" Then
                Set wsh = CreateObject("Wscript.shell")
                wsh.Run "%comspec% /C del /F /Q  "&cookie_path&"\*.*"
                wsh.Run "%comspec% /C del /F /Q  "&cache_path&"\*.*"
        End If        
       
End Function

        Call  del_exc()
回复 支持 反对

使用道具 举报

本版积分规则

关闭

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

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

GMT+8, 2024-11-19 19:35 , Processed in 0.070959 second(s), 26 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

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