Solved

How do I delete a file which was last modified yesterday or before ?

Posted on 2010-08-29
7
384 Views
Last Modified: 2013-11-07
I'm using the following code to delete a file, but I need to modify it to just delete a file if it was last modified yesterday or before. So not if todays date.

Dim sqlfile As DirectoryInfo = New DirectoryInfo(System.IO.Path.GetDirectoryName( _
               System.Reflection.Assembly.GetEntryAssembly.Location.ToString()) & "\") 

            Try : File.Delete(sqlfile.FullName & System.Reflection.Assembly.GetExecutingAssembly.GetName.Name & "PossSQLErr.txt") : Catch ex As Exception : LogError(ex, "MAIN-3a") : End Try

Open in new window

0
Comment
Question by:mindwarpltd
7 Comments
 
LVL 5

Accepted Solution

by:
Akin Delu earned 167 total points
ID: 33552684
       dim flinfo as New System.IO.FileInfo("filename")
        dim dt as DateTime = flinfo.LastWriteTime
        if DateTime.Now.Year >= dt.Year and DateTime.Now.Month >= dt.Month then
                dim daydiff as int = DateTime.Now.Day - dt.Day
                if daydiff >= 1 and daydiff < 2 then
                    System.IO.File.Delete("filename");
                end if
        end if
0
 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 166 total points
ID: 33553219
here you go

Dim sqlfile As DirectoryInfo = New DirectoryInfo(System.IO.Path.GetDirectoryName( _
               System.Reflection.Assembly.GetEntryAssembly.Location.ToString()) & "\")

        Dim fi As FileInfo = New FileInfo(sqlfile.FullName & System.Reflection.Assembly.GetExecutingAssembly.GetName.Name & "PossSQLErr.txt")

'check if exists and last modified time is equal or bigger than 1
        If fi.Exists And DateDiff(DateInterval.Day, DateTime.Now, fi.LastWriteTime) >= 1 Then
            Try
                fi.Delete()
            Catch ex As Exception

            End Try

        End If

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 167 total points
ID: 33556825
It should be as simple as:
System.IO.FileInfo fi = new System.IO.FileInfo("filename");

if (DateTime.Now.Subtract(fi.LastWriteTime).TotalDays > 0) fi.Delete(); // == 0 means we're in the same day

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 42

Expert Comment

by:sedgwick
ID: 33556866
@kaufmed

which is the same as my post without the exception handling and the file validation.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33557069
Except that you're using vb6 style functions and I'm using the .NET OO-centric functions  ;)
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33557234
that's true.
0
 

Author Comment

by:mindwarpltd
ID: 33573989
@kaufmed I also said vb.net
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question