locked
Deleting temporary internet files, cookies and history etc from VB

    Question

  • Hi, I am new to VB ,but have been learning for a few months now and trying to write my own little disk cleanup utility (for a client)

    I have got most of the app done but having the following issues which I hope someone would be kind enough to assist with:-

    1) Can clean out windows/temp folder okay, but when it finds a file locked by a process it crashes - I have found out how to use error handlers etc to control the error but how can I unlock(?) the files in use before I try to delete them

    2) How can I clear cookes, history, temp internet files etc from within vb?  i have looked at my.computer.filesystem.specialdirectories.temp and also environment.specialfolders but cannot get it to work


    Thanks for reading and in advance for any replies
    Thursday, June 26, 2008 11:57 AM

Answers

  • Hi Darren,

    Try this
            For Each s As String In System.IO.Directory.GetFiles("C:\WINDOWS\TEMP")
                Try
                    System.IO.File.Delete(s)
                Catch ex As IO.IOException
                    Debug.WriteLine(ex.Message)
                Catch ex As Exception
                    Throw
                End Try
            Next

     
    Asking questions is learning, don't worry about it ;)

    Ralf
    Thursday, June 26, 2008 9:28 PM

All replies

  • Hi,

    1) The files are locked by programs currently running on your system. It would be best just to leave them be.
    2) Use Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) to find the internet explorer cache folde.

    Ralf
    • Proposed as answer by Ralf de Kleine XCESS Friday, June 27, 2008 6:21 AM
    • Unproposed as answer by wingers Wednesday, July 02, 2008 6:28 PM
    Thursday, June 26, 2008 8:40 PM
  • Thanks Ralf

    Fair point, so how can I get my code to continue without giving an error when it hits a file in use, because at the moment it just gives me ioexception and stops leaving all the rest of the files in the temp folder?

    Thursday, June 26, 2008 8:45 PM
  • Hi Darren,

    Not sure if there is a clean way to check if a file is locked. Place a try catch block around the filedelete routine to solve this.

    Ralf
    Thursday, June 26, 2008 8:50 PM
  • This is the code I am using


        Try

        Catch ex As Exception

        Dim s As String

        For Each s In System.IO.Directory.GetFiles("C:\WINDOWS\TEMP")

        System.IO.File.Delete(s)

        Next s

        End Try


    But it doesn't seem to work still, leaves loads of files in temp that I can delete manually okay so why is it not doing them?

    Sorry to ask so many questions Ralf

    Thursday, June 26, 2008 9:22 PM
  • Hi Darren,

    Try this
            For Each s As String In System.IO.Directory.GetFiles("C:\WINDOWS\TEMP")
                Try
                    System.IO.File.Delete(s)
                Catch ex As IO.IOException
                    Debug.WriteLine(ex.Message)
                Catch ex As Exception
                    Throw
                End Try
            Next

     
    Asking questions is learning, don't worry about it ;)

    Ralf
    Thursday, June 26, 2008 9:28 PM
  • fantastic!! works like a dream

    thanks Ralf
    Thursday, June 26, 2008 10:18 PM
  •  would it be easy to modify this so it did all the subfolders under temp too?
    Friday, June 27, 2008 5:21 PM
  • Recursion.
    With just a little modification you could use that code in a recursive method.

    -Martin
    Saturday, June 28, 2008 2:27 PM
  • thanks, think I need to go back to the study books then to work out how!
    Sunday, June 29, 2008 2:31 PM
  •   help!!!

    whatever I do to modify the script it still doesn't want to delete all the files and subfolders in c:\windows\temp

    please could someone be kind enough to assist revising this script to do sub dirs etc

    Thanks

    For Each s As String In System.IO.Directory.GetFiles("C:\WINDOWS\TEMP")     
                Try     
                    System.IO.File.Delete(s)     
                Catch ex As IO.IOException     
                    Debug.WriteLine(ex.Message)     
                Catch ex As Exception     
                    Throw     
                End Try     
            Next    
     
    Tuesday, July 01, 2008 12:37 AM
  • Hi,

    Try this code:
        Private Sub RemoveFilesAndDirectories(ByVal dir As String
            'Recursive 
            For Each d As String In IO.Directory.GetDirectories(dir) 
                RemoveFilesAndDirectories(d) 
            Next 
     
            'Remove files 
            For Each f As String In IO.Directory.GetFiles(dir) 
                Try 
                    IO.File.Delete(f) 
                Catch ex As IO.IOException 
                    Debug.WriteLine(ex.Message) 
                Catch ex As Exception 
                    Throw 
                End Try 
            Next 
     
            'Remove directory 
            Try 
                IO.Directory.Delete(dir) 
            Catch ex As IO.IOException 
                Debug.WriteLine(ex.Message) 
            Catch ex As Exception 
                Throw 
            End Try 
        End Sub 

    Ralf



    Tuesday, July 01, 2008 6:19 AM
  • I get 

    unauthorizedaccessexception was unhandled

    Access to the path 'C:\windows\temp\Temp1_extending_dw.zip\mm_jsapi_environment.h' is denied.


    This is basically the sort of problem I ran into trying any other method, I need it to ignore files that are locked and continue on to the next one etc

    unless there is a vb way of unlocking the files, or killing the process holding it


    thanks for your help, much appreciated
    Tuesday, July 01, 2008 2:20 PM
  • please could someone help with the above code supplied by Ralf - it works great except getting unauthorizedaccessexception was unhandled errors

    can it easily be modified to catch those and the io.exception errors?

     Private Sub RemoveFilesAndDirectories(ByVal dir As String)    
            'Recursive    
            For Each d As String In IO.Directory.GetDirectories(dir)    
                RemoveFilesAndDirectories(d)    
            Next    
        
            'Remove files    
            For Each f As String In IO.Directory.GetFiles(dir)    
                Try    
                    IO.File.Delete(f)    
                Catch ex As IO.IOException    
                    Debug.WriteLine(ex.Message)    
                Catch ex As Exception    
                    Throw    
                End Try    
            Next    
        
            'Remove directory    
            Try    
                IO.Directory.Delete(dir)    
            Catch ex As IO.IOException    
                Debug.WriteLine(ex.Message)    
            Catch ex As Exception    
                Throw    
            End Try    
        End Sub    
     
    • Proposed as answer by i3ern Sunday, September 13, 2009 10:00 AM
    Thursday, July 03, 2008 3:19 PM
  • I start almost every sub with


    Private sub whatever_yada()
    on error resume next

    end sub


    this isn't a fix, but it does however resume doing it's job through an error when it encounters one
    Sunday, September 13, 2009 10:02 AM