Solved

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

Posted on 2010-08-29
7
390 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 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

617 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