Delelte old files/ messagebox
Delelte old files/ messagebox
Delelte old files/ messagebox
Delelte old files/ messagebox
Delelte old files/ messagebox
Delelte old files/ messagebox Delelte old files/ messagebox Delelte old files/ messagebox Delelte old files/ messagebox Delelte old files/ messagebox Delelte old files/ messagebox Delelte old files/ messagebox Delelte old files/ messagebox
Delelte old files/ messagebox Delelte old files/ messagebox
Delelte old files/ messagebox
Go Back  Xtreme Visual Basic Talk > > > Delelte old files/ messagebox


Reply
 
Thread Tools Display Modes
  #1  
Old 06-27-2011, 05:52 AM
--crafter-- --crafter-- is offline
Newcomer
 
Join Date: Jun 2011
Posts: 16
Post Delelte old files/ messagebox


Hello,
I'm working on this program to delete .tmp files that are older then 5 days. The only problem that im running into is if there are no files older than 5 days to display a messagebox "There are no .tmp files older than 5 days".
how can i go about doing this? Im using VB.net, this is that i have so far.

Quote:
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Try
For Each file As IO.FileInfo In New IO.DirectoryInfo("directory").GetFiles("*.tmp")
If (Now - file.CreationTime).Days > 5 Then file.Delete()
Next
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
End Clas
Thanks in advance.
Reply With Quote
  #2  
Old 06-27-2011, 07:01 AM
DrPunk's Avatar
DrPunkDelelte old files/ messagebox DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Have a boolean variable that records if you have deleted something.

When you have finished the loop, if nothing has been deleted then you know there are no files older than 5 days and can display the messagebox.
Code:
Dim deleted as Boolean = False

For Each [File]
    If [FileIsOlderThan5Days] Then
        deleted = True
        [DeleteTheFile]
    End if
Next

If not deleted Then
    messagebox.show("No files older than 5 days.")
Endif
You might also want to think about your error catching. The way it's set up, if it fails to delete a file then it will stop and not try to delete any more files. If you moved the catch inside the loop then it would report the failure of deleting a file but then carry on trying to delete any other files that might exist.
__________________
There are no computers in heaven!
Reply With Quote
  #3  
Old 07-02-2011, 02:27 AM
--crafter-- --crafter-- is offline
Newcomer
 
Join Date: Jun 2011
Posts: 16
Default

Cool, i edited the file and it works. !!!

Thanks for your help DrPunk.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Delelte old files/ messagebox
Delelte old files/ messagebox
Delelte old files/ messagebox Delelte old files/ messagebox
Delelte old files/ messagebox
Delelte old files/ messagebox
Delelte old files/ messagebox Delelte old files/ messagebox Delelte old files/ messagebox Delelte old files/ messagebox Delelte old files/ messagebox Delelte old files/ messagebox Delelte old files/ messagebox
Delelte old files/ messagebox
Delelte old files/ messagebox
 
Delelte old files/ messagebox
Delelte old files/ messagebox
 
-->