Files & dates - help

  • Thread starter Thread starter Martin Ho
  • Start date Start date
M

Martin Ho

Two questions:

1.)
I am copying a whole bunch of files from one directory, but I need to
do a if command to copy on those which date of creation is the same
as today's date. How do I do this comparision?

To give you some hint , this is how it could look but this of course
doesn't work, both variables return date with the time signature...I
need to compare only dates between date of creation and local date.

if System.IO.File.GetCreationTime(path to my file) = date.Now then
copy ....
end if


2.)
How I would go about deleting all files that are 4 weeks (28 days)
older than today's date?

Thanks a lot for all your help and time.

Martin

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
Jan. 6, 2005

Instead of using Now (which includes the exact time) use Now.Date.

Dim f As New DirectoryInfo("PATH")
Dim fi As FileInfo
For Each fi In f.GetFiles
Dim d As Date
d = CType(File.GetCreationTime(fi.FullName), DateTime).Date
Dim t As New TimeSpan(28, 0, 0, 0) '
Timespan(days,hours,minutes,seconds)
If d.Date = Now.Date.Subtract(t) Then
fi.Delete()
End If
Next

This will convert the datetime object that is returned with
GetCreationTime to a date object. Datetime objects have a date property that
only returns the day in the form of Month/Day/Year. Then it compares the
file's date to Now.Date.Subtract(timespan) where timespan is a object created
earlier that specifies the number of days. Then it deletes it if it is 28
days old. If this message helped you, then please click on the "Yes" box
above this message. Thank you and I hope this helps!


Joseph MCAD
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top