VBS判断系统类型删除cookie和cache
本帖最后由 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
'MsgBoxobj_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
Calldel_exc() 我自己搞定了。。
还能进一步优化一下。下面是代码。
'*********************************************
'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
Calldel_exc()
页:
[1]