Results 1 to 10 of 10

Thread: [2008] Help with cleanup programm

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    4

    Angry [2008] Help with cleanup programm

    Hi guys

    I have a problem and it is freakin me out for a week now.


    I have made a programm with vb 2008 express that must clean the temp folder the temporary internet folder and the cookies folder.


    But somehow it doesn't work,
    I think that the problem is that ONE of the many files is used (by another programm) and when the programm comes to that file and it see that it's in use it just stop's cleaning up.



    My code is:



    Code:
        Function clean()        
    
    On Error Resume Next
            Dim pathCache As String = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)
    
            My.Computer.FileSystem.DeleteDirectory(pathCache, FileIO.DeleteDirectoryOption.DeleteAllContents)
    
            Dim pathCookie As String = Environment.GetFolderPath(Environment.SpecialFolder.Cookies)
            My.Computer.FileSystem.DeleteDirectory(pathCookie, FileIO.DeleteDirectoryOption.DeleteAllContents)
    
            Dim pathTemp As String = Environment.GetFolderPath(Environment.SpecialFolder.Templates)
            My.Computer.FileSystem.DeleteDirectory(pathTemp, FileIO.DeleteDirectoryOption.DeleteAllContents)
    
    
            Return 0
        End Function

    And I also have this function I think this is the best one but this one has the same problem I think.


    Code:
        Public Sub DelIECache()
    
            Dim di As New IO.DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache))
    
            On Error Resume Next
            If di.Exists = False Then
    
                di.Create()
    
            End If
    
            System.IO.File.SetAttributes(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache).ToString, IO.FileAttributes.Normal)
    
            Dim Cache1 As String
    
            Dim Cache2() As String
    
            Cache2 = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache))
    
            For Each Cache1 In Cache2 'Get all files in Temporary internet files, ‘folder and then set their attribute to normal, and then delete them.
    
                IO.File.SetAttributes(Cache1, IO.FileAttributes.Normal)
    
                IO.File.Delete(Cache1)
    
            Next
    
            For Each Cache1 In Cache2 'Get all files in Temporary internet files, ‘folder and then set their attribute to normal, and then delete them.
    
                IO.File.SetAttributes(Cache1, IO.FileAttributes.Normal)
    
                IO.File.Delete(Cache1)
    
            Next
    
            ' The true indicates that if subdirectories
    
            ' or files are in this directory, they are to be deleted as well.
    
            di.Delete(True)
    
    err:    On Error Resume Next'///IGNORE ERROR///
    
        End Sub


    I need your help to make it work.


    Please edit the code where needed or help me with a solution

    Already thanks alot

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Help with cleanup programm

    First up, get rid of that old VB6-style error handling. Your code should be something like this:
    vb.net Code:
    1. For Each filePath As String In IO.Directory.GetFiles("folder path here")
    2.     Try
    3.         IO.File.Delete(filePath)
    4.     Catch ex As Exception
    5.         'Log error or whatever.
    6.     End Try
    7. Next
    Because the Try statement is inside the For loop, rather than the other way around, the code will loop back to the next file even if one file fails to delete.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    4

    Re: [2008] Help with cleanup programm

    I know have this function

    Code:
     Function clean()
    
            WebBrowser1.Dispose()
    
            Dim pathCache As String = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)
            Dim pathCookie As String = Environment.GetFolderPath(Environment.SpecialFolder.Cookies)
            Dim pathTemp As String = Environment.GetFolderPath(Environment.SpecialFolder.Templates)
    
    
            For Each filePath As String In IO.Directory.GetFiles(pathCache)
                Try
                    IO.File.Delete(filePath)
                Catch ex As Exception
                    'Log error or whatever.
                End Try
            Next
    
            For Each filePath As String In IO.Directory.GetFiles(pathCookie)
                Try
                    IO.File.Delete(filePath)
                Catch ex As Exception
                    'Log error or whatever.
                End Try
            Next
    
    
            Return 0
        End Function

    But It still doesn't work for me

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Help with cleanup programm

    There really isn't much point you copying my comment that says "Log error or whatever" is there? If files aren't deleted then exceptions are being thrown. Don't just ignore them. Examine them to see WHY it's not working.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    4

    Re: [2008] Help with cleanup programm



    This is the error that I get, I think that the problem is

    that the file index.dat is used by another programm and when the programm comes there, it see's that it generates the error above and then starts over and then does the same thing.

    It sort off "get stuck" by the file that is in use.

  6. #6
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: [2008] Help with cleanup programm

    For were you wrote:
    vb Code:
    1. 'Log error or whatever

    Replace it with: (one of these...)

    vb Code:
    1. Debug.Print(ex.Message)
    2. Debug.Print(ex.tostring)

    I am not sure which one is right...

    It is better to Debug.Print instead of creating a msgbox since if this is for a customer/friend etc... they may not understand the error message and it wouldnt be good for someone to be using the program when all of a sudden all these stange codes pop-up...

    Quote Originally Posted by Customer
    ...What's all that ****...?

  7. #7
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2008] Help with cleanup programm

    ex.Message will give you only the error message, which is doesn't always help very much. I'd suggest that you print out the frames on the call stack, it should help much more. But the ex.ToString may be the best solution, since you'd get all the information there.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  8. #8
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Re: [2008] Help with cleanup programm

    For testing purposes, until I get rid of any bugs I would do

    vb Code:
    1. For Each filePath As String In IO.Directory.GetFiles(pathCookie)
    2.     Try
    3.         IO.File.Delete(filePath)
    4.     Catch ex As Exception
    5.         MessageBox.Show("An error occured in Form1; Function Clean() PAth    Cookie;Error: " & ex.ToString)
    6.     End Try

    Tells you where the error is occurring and what the error is. Just my opinion.
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

  9. #9

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    4

    Re: [2008] Help with cleanup programm

    Ok

    Didn't know that, i'm kinda new to vb.net

    But I have the error now

    (see my previous post)
    Last edited by FleXCube; Aug 3rd, 2008 at 12:21 PM.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Help with cleanup programm

    You shouldn't be deleting index.dat. That's not a cookie. Cookies are TXT files. You should be getting only the TXT files when you call GetFiles. Read the documentation for the method and it will show you how to filter the files. That said, an error deleting one file shouldn't stop any of the others being deleted.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width