Wednesday, February 16, 2011

Delete Cookies - Sample Code


Const COOKIES = &H21&
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(COOKIES)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path & "\*.*"
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile(strPath)

    Browser("Browser").WinToolbar("ToolbarWindow32").Press "&Tools"
    Browser("Browser").WinToolbar("ToolbarWindow32").Press "&Tools"
    Browser("Browser").WinMenu("ContextMenu").Select "Delete Browsing History…"
    Browser("Browser").Dialog("Delete Browsing History").WinButton("Delete cookies").Click
    Browser("Browser").Dialog("Delete Browsing History").Dialog("Delete Cookies").WinButton("Yes").Click
    Browser("Browser").Dialog("Delete Browsing History").WinButton("Close").Click

    '##################################
    '#Function ID/Name: ClearCookies
    '#Function Description:The Function clears all cookies that are present on your HardDrive in an Internet Transaction. '#Function Logic: The function opens the Properies Window of Internet Explorer & fires the Click Event for the Delete Cookies ' Button & Delete Temporary Internet Files.
    '
    ' Example
    '
    ' Call ClearCookies()
    '
    '#Author: Manjula Palnati
    '##################################
    Function ClearCookies()
    SystemUtil.Run "Control.exe","inetcpl.cpl"
    Set objShell = CreateObject("Wscript.Shell")
    Do Until Success = True
    Success = objShell.AppActivate("Internet Properties")
    Wait(1)
    Loop
    objShell.Sendkeys "%i"
    Wait(1)
    objShell.Sendkeys "{ENTER}"
    Wait(1)
    objShell.Sendkeys "%f"
    Wait(1)
    objShell.Sendkeys "%d"
    Wait(1)
    objShell.Sendkeys "{ENTER}"
    Wait(4)
    objShell.Sendkeys "{ENTER}"
    End Function
    key codes
    Const VK_RETURN = 28
    Const VK_CONTROL = 29
    Const VK_DOWN = 80
    Dim oBrowser
    Set oBrowser = Description.Create()
    oBrowser( "micClass" ).Value = "Browser"
    'oBrowser( "title" ).Value = "about:blank"
    oBrowser( "application version" ).Value = "internet explorer 7"
    oBrowser( "version" ).Value = "internet explorer 7"
    Set BrowserObject = Browser(oBrowser).object
    Set deviceReplay = CreateObject( "Mercury.DeviceReplay" )
    Window("hwnd:=" & BrowserObject.hwnd).activate
    Window("hwnd:=" & BrowserObject.hwnd).Type micNumLockOff
    Window("hwnd:=" & BrowserObject.hwnd).Type micAltDwn + "t"+ micAltUp
    wait(1)
    ' deviceReplay.PressNKeys VK_DOWN , sIndex
    wait(1)
    deviceReplay.PressKey VK_RETURN
    Browser("Browser").Dialog("Delete Browsing History").WinButton("Delete cookies").Click
    wait(1)
    Browser("Browser").Dialog("Delete Browsing History").Dialog("Delete Cookies").WinButton("Yes").Click
    wait(1)
    Browser("Browser").Dialog("Delete Browsing History").WinButton("Close").Click
#4 Maniz on 11.20.08 at 1:20 pm
    We can use WebUtil.(Method)
    webutil.DeleteCookie
    webutil.DeleteCookies
#5 Will on 11.22.08 at 8:12 am
    It’s interesting to see the solutions here. It wouldn’t have even occurred to me to write a script that actually clicks through the IE menu items to do it.
#6 Ankur on 11.22.08 at 10:22 am
    @itpayz, Basim: Nice solutions.
    @Maniz: Good one. Webutil is an undocumented method (for some unknown reasons!)
    @Manjula: How about reaching the cookies directory directly through the shell object you created and deleting all the files inside? That will reduce the length of code to a single statement.
    @Will: :) Same here. By the way for quite some time I have not seen any updates on your blog… trust everything is fine?
#7 maddy on 11.22.08 at 12:35 pm
    @ankur : i need to clarify one doubt when u said to manjula that “How about reaching the cookies directory directly through the shell object you created and deleting all the files inside? That will reduce the length of code to a single statement” ?
    how to achieve this ?
#8 Sumit on 11.25.08 at 7:47 am
    Hi Ankur,
    there is 1 more code to delete TEMp files,COOKIES using SHELL codes.
    COOKIES = 0×21
    using the above code u prepare a vbscript to delete files.. i used that script around 1 year back in my project.. now i am unable to remember the exact code.. the script was just of 5-6 lines.. can you help me in this code..
#9 Sumit Kalra on 11.26.08 at 2:25 am
    Ankur - please ignore my previous comments..
    below is the code of cookies
    COOKIES = &H21&
#10 Sumit Kalra on 11.26.08 at 2:34 am
    below are the constants for special folders in windows:
    &H1& Internet Explorer
    &H2& Programs
    &H3& Control Panel
    &H4& Printers and Faxes
    &H5& My Documents
    &H6& Favorites
    &H7& Startup
    &H8& My Recent Documents
    &H9& SendTo
    &Ha& Recycle Bin
    &Hb& Start Menu
    &Hd& My Music
    &He& My Videos
    &H10& Desktop
    &H11& My Computer
    &H12& My Network Places
    &H13& NetHood
    &H14& Fonts
    &H15& Templates
    &H16& All Users Start Menu
    &H17& All Users Programs
    &H18& All Users Startup
    &H19& All Users Desktop
    &H1a& Application Data
    &H1b& PrintHood
    &H1c& Local Settings\Application Data
    &H19& All Users Favorites
    &H20& Local Settings\ Temporary Internet Files
    &H21& Cookies
    &H22& Local Settings\History
    &H23& All Users Application Data
    &H24& Windows
    &H25& System32
    &H26& Program Files
    &H27& My Pictures
    &H28& User Profile
    &H2b& Common Files
    &H2e& All Users Templates
    &H2f& Administrative Tools
    &H31& Network Connections
#11 Sumit Kalra on 11.26.08 at 2:35 am
    Ankur - without creating shell object.. u can delete cookies by VBscript..
    using the above code u prepare a vbscript to delete files.. i used that script around 1 year back in my project.. now i am unable to remember the exact code.. the script was just of 5-6 lines.. can you help me in this code..
#12 Sumit Kalra on 11.26.08 at 2:41 am
    below is the general code to delete cookies by creating shell obj.:
    Const COOKIES = &H21&
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.Namespace(COOKIES)
    Set objFolderItem = objFolder.Self
    strPath = objFolderItem.Path & "\*.*"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.DeleteFile(strPath)
    but u can delete without creating shell obj… forget that code :(
#13 Andrey Koshkarov on 12.02.08 at 1:08 am
    Hi
    For IE7 browser I use next function:
    Sub ClearCookies()
    ‘work only for IE7
    Set wshshell = CreateObject(”WScript.Shell”)
    WshShell.Run “Rundll32.exe InetCpl.cpl, ClearMyTracksByProcess 255?, 1, true
    End Sub
    To read more about clearing IE7 history from command line go to next URL:
    http://www.howtogeek.com/howto/windows/clear-ie7-browsing-history-from-the-command-line/
#14 zubair on 12.06.08 at 7:53 am
    try
    webutil.deletecookies
    :)
#15 Praj on 12.10.08 at 12:56 pm
    Function Deletecookies
    userName=Environment.Value(”UserName”)
    Dim oShell
    Set oShell=CreateObject(”WScript.shell”)
    oShell.run(”cmd /C cd C:\Documents and Settings\” & userName & “\cookies & del *.txt”)
    Set oshell=nothing
    End Function
#16 K Subba Raju (Delete a cookie) on 12.12.08 at 2:18 am
    Browser(”Browser”).WinToolbar(”ToolbarWindow32?).Press “&Tools”
    Browser(”Browser”).WinToolbar(”ToolbarWindow32?).Press “&Tools”
    Browser(”Browser”).WinMenu(”ContextMenu”).Select “Delete Browsing History…”
    Browser(”Browser”).Dialog(”Delete Browsing History”).WinButton(”Delete cookies”).Click
    Browser(”Browser”).Dialog(”Delete Browsing History”).Dialog(”Delete Cookies”).WinButton(”Yes”).Click
    Browser(”Browser”).Dialog(”Delete Browsing History”).WinButton(”Close”).Click

No comments:

Post a Comment