Question Locked

Batch File to delete cookies

0 helpful
22 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I'm looking at creating a batch file to delete cookies and temp files from a remote computer. So in other words, I would manually map a drive to a computer, with the Z:\ drive for instance, and have the batch file delete cookies and temp files to all profiles within that Z:\ drive. Some computers have over 100 profiles, so you can see why a batch file would be useful. We're in a Windows XP environment.

22 replies

by detroitreds

I don't have an account with them, but thanks anyways.

by unhappyuser

You don't need one. If you scroll down far enough the solution is visible.

EMD

by wizard-09

@echo off

del /Q /F "%USERPROFILE%\Cookies\*.txt"
del /Q /F "%USERPROFILE%\LocalSettings\Temp\*.*

goto

:end

Edited for mistake made on temp file del

tested now works

Keep us informed as to your progress if you require further assistance.

If you think that any of the posts that have been made by all TR Members, have solved or contributed to solving the problem, please Mark them as Helpful so that others may benefit from the outcome.

by unhappyuser

This appears to only delete files in one profile. How does it delete the files in many, which I think is what they asked for?

EMD

by wizard-09

If he was to add this to a shutdown script on AD then it will run for every user logged onto that OU when they shut down the machine.

by detroitreds

I don't see an option that it would delete on mapped drives, so I would assume this is only for the local C: drive

by wizard-09

I dont no way you are going to all that trouble, that bat file the new one will del every cookie temp file on all the profiles on that computer right, so way not use add this to the shutdown script thats the easy way to do it, i really dont have time to recreate another bat file for what you asked looks like to much work will be involved which i dont have as i am working at the min, but if you add that new bat file to the shutdown script it will do what your wanting really :p

by snuffy09

options>include>add folder

if you have the drive mapped you can select the folder you want to clean out

by detroitreds

Although this might might work in AD, I'm looking at running it from a local computer. This script would only work for local profiles on the computer it's ran from. Basically, I'm looking at saving time from having to open every profile on a mapped drive and deleting the said files.

by unhappyuser

I assumed he wanted to remove the files from all profiles in one quick script. Thanks.

EMD

by wizard-09

This would work on AD if the script was added to AD so i think he did get what he was looking for, as he only wants this for the local machine i will go back to the drawing board

by wizard-09

echo off
cd /d "%AllUsersProfile%\.."
set Docs=%CD%
for /d %%a in (*.*) do call :Del %%a
goto :eof

:Del
echo Cleaning up user "%*"
echo rd /s /q "%Docs%\%*\Cookies"
echo md /s /q "%Docs%\%*\Cookies"

echo rd /s /q "%Docs%\%*\Local Settings\Temporary Internet Files"
echo md /s /q "%Docs%\%*\Local Settings\Temporary Internet Files"

echo rd /s /q "%Docs%\%*\Local Settings\Temp"
echo md /s /q "%Docs%\%*\Local Settings\Temp"

I think this is the one your looking for :P

by detroitreds

I'm looking at the page. I see about 30 replies and 1 accepted. no matter which one I choose, it's asking to create an account. The page itself isn't that long. I must be missing something.

by unhappyuser

OK try

Delete Cookies\Temp Internet, using VB (don’t panic its easy!)

source: http://support.microsoft.com/default.aspx?scid=KB;en-us;q262110

Paste the text below (not the ****) into notepad and save as "delete.vbs"
Then simply run the file

****************************************************************************************************88
Option Explicit

Private Declare Function FindFirstUrlCacheGroup Lib "wininet.dll" ( _
ByVal dwFlags As Long, _
ByVal dwFilter As Long, _
ByRef lpSearchCondition As Long, _
ByVal dwSearchCondition As Long, _
ByRef lpGroupId As Date, _
ByRef lpReserved As Long) As Long

Private Declare Function FindNextUrlCacheGroup Lib "wininet.dll" ( _
ByVal hFind As Long, _
ByRef lpGroupId As Date, _
ByRef lpReserved As Long) As Long

Private Declare Function DeleteUrlCacheGroup Lib "wininet.dll" ( _
ByVal sGroupID As Date, _
ByVal dwFlags As Long, _
ByRef lpReserved As Long) As Long

Private Declare Function FindFirstUrlCacheEntry Lib "wininet.dll" Alias "FindFirstUrlCacheEntryA" ( _
ByVal lpszUrlSearchPattern As String, _
ByRef lpFirstCacheEntryInfo As INTERNET_CACHE_ENTRY_INFO, _
ByRef lpdwFirstCacheEntryInfoBufferSize As Long) As Long

Private Type INTERNET_CACHE_ENTRY_INFO
dwStructSize As Long
szRestOfData(1024) As Long
End Type

Private Declare Function DeleteUrlCacheEntry Lib "wininet.dll" Alias "DeleteUrlCacheEntryA" ( _
ByVal lpszUrlName As Long) As Long

Private Declare Function FindNextUrlCacheEntry Lib "wininet.dll" Alias "FindNextUrlCacheEntryA" ( _
ByVal hEnumHandle As Long, _
ByRef lpNextCacheEntryInfo As INTERNET_CACHE_ENTRY_INFO, _
ByRef lpdwNextCacheEntryInfoBufferSize As Long) As Long

Private Const CACHGROUP_SEARCH_ALL = &H0
Private Const ERROR_NO_MORE_FILES = 18
Private Const ERROR_NO_MORE_ITEMS = 259
Private Const CACHEGROUP_FLAG_FLUSHURL_ONDELETE = &H2
Private Const BUFFERSIZE = 2048

Private Sub Command1_Click()
Dim sGroupID As Date
Dim hGroup As Long
Dim hFile As Long
Dim sEntryInfo As INTERNET_CACHE_ENTRY_INFO
Dim iSize As Long

On Error Resume Next

' Delete the groups
hGroup = FindFirstUrlCacheGroup(0, 0, 0, 0, sGroupID, 0)

' To avoid error using it with IE4 as FindFirstUrlCacheGroup is not implemented
If Err.Number <> 453 Then
If (hGroup = 0) And (Err.LastDllError <> 2) Then
MsgBox "An error occurred enumerating the cache groups" & Err.LastDllError
Exit Sub
End If
Else
Err.Clear
End If

If (hGroup <> 0) Then
'we succeeded in finding the first cache group.. enumerate and
'delete
Do
If (0 = DeleteUrlCacheGroup(sGroupID, CACHEGROUP_FLAG_FLUSHURL_ONDELETE, 0)) Then

' To avoid error using it with IE4 as FindFirstUrlCacheGroup is not implemented
If Err.Number <> 453 Then
MsgBox "Error deleting cache group " & Err.LastDllError
Exit Sub
Else
Err.Clear
End If
End If
iSize = BUFFERSIZE
If (0 = FindNextUrlCacheGroup(hGroup, sGroupID, iSize)) And (Err.LastDllError <> 2) Then
MsgBox "Error finding next url cache group! - " & Err.LastDllError
End If
Loop Until Err.LastDllError = 2
End If

' Delete the files
sEntryInfo.dwStructSize = 80
iSize = BUFFERSIZE
hFile = FindFirstUrlCacheEntry(0, sEntryInfo, iSize)
If (hFile = 0) Then
If (Err.LastDllError = ERROR_NO_MORE_ITEMS) Then
GoTo done
End If
MsgBox "ERROR: FindFirstUrlCacheEntry - " & Err.LastDllError
Exit Sub
End If
Do
If (0 = DeleteUrlCacheEntry(sEntryInfo.szRestOfData(0))) _
And (Err.LastDllError <> 2) Then
Err.Clear
End If
iSize = BUFFERSIZE
If (0 = FindNextUrlCacheEntry(hFile, sEntryInfo, iSize)) And (Err.LastDllError <> ERROR_NO_MORE_ITEMS) Then
MsgBox "Error: Unable to find the next cache entry - " & Err.LastDllError
Exit Sub
End If
Loop Until Err.LastDllError = ERROR_NO_MORE_ITEMS
done:
MsgBox "cache cleared"
Command1.Enabled = True
End Sub
*************************************************************************************

by detroitreds

I appreciate your help, but it again cleans the files on the local C: drive. I have a batch file that does that already.

For example, let's say I manually map a drive or do net use z: \\computer name\c$, I'm looking at cleaning within that Z drive.

by bizzo

Change the value of profile root to whatever location the user profiles are in.

Also, remove the echos, just there for testing.

But please make sure it deletes only what you want deleting BEFORE removing the echos!

------------------------------------
@echo off
Set profileroot=d:\documents and settings

FOR /F "tokens=*" %%I IN ('DIR /B /AD "%profileroot%"') DO (
echo del /s /q "%profileroot%\%%I\Cookies\*"
echo del /s /q "%profileroot%\%%I\Local Settings\Temporary Internet Files\*"
echo del /s /q "%profileroot%\%%I\Local Settings\Temp\*"
)
------------------------------------

by detroitreds

I actually like your idea to delete and create the folder. This would obviously make it faster. Would you be able to implement it with the following. This deletes each file seperatly. I would appreciate it if you could incorporate yours with this one:

D:\documents and settings is where I would put in the mappped drive folder I want the batch file to point to.

------------------------------------
@echo off
Set profileroot=d:\documents and settings

FOR /F "tokens=*" %%I IN ('DIR /B /AD "%profileroot%"') DO (
echo del /s /q "%profileroot%\%%I\Cookies\*"
echo del /s /q "%profileroot%\%%I\Local Settings\Temporary Internet Files\*"
echo del /s /q "%profileroot%\%%I\Local Settings\Temp\*"
)
------------------------------------

This discussion is closed to new replies.