Friday, April 25, 2008

Delete Cookies Script



'Script is in testing as of 4.21.08

On Error Resume Next

' Parse time/date and then subtract a year from it

Const CONVERT_TO_LOCAL_TIME = True

Set dtmStart = CreateObject("WbemScripting.SWbemDateTime")
dtmStart.SetVarDate Now, CONVERT_TO_LOCAL_TIME
dtmRegular = dtmStart.GetVarDate(CONVERT_TO_LOCAL_TIME)
dtmRegular = DateAdd("yyyy", -1, dtmRegular)

' not needed due to not needing to convert back the time/date format
' dtmStart.SetVarDate dtmRegular, CONVERT_TO_LOCAL_TIME

'Wscript.Echo dtmRegular

' set constants for cookies folder and temp int folders

Const COOKIES = &H21&
Const TEMPINT = &H20&

Set objShell = CreateObject("Shell.Application")
Set objFSO = CreateObject("Scripting.FileSystemObject")

'set path to cookies

Set objFolder = objShell.Namespace(COOKIES)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path

'delete cookies in cookies folder

Set folder = objFSO.GetFolder(strPath)
Set files = folder.Files
for each objFile in files
if objFile.DateLastModified < dtmRegular and objFile.Type = "Text Document" then
objFile.Delete
end if
next

'set folder to temporary internet files

Set objFolder = objShell.Namespace(TEMPINT)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path

'delete cookies in temporary internet files

Set folder = objFSO.GetFolder(strPath)
Set files = folder.Files
for each objFile in files
if objFile.DateLastModified < dtmRegular and objFile.Type = "Text Document" then
objFile.Delete
end if
next



Note: this can also be accomplished a little simpler through the forfiles command.