?
Solved

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

Posted on 2010-08-29
7
Medium Priority
?
392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 5

Accepted Solution

by:
Akin Delu earned 668 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 664 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 668 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
Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

718 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