Solved

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

Posted on 2010-08-29
7
382 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 74

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now