Solved

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

Posted on 2010-08-29
7
385 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

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.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 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