Reading Directory and Files

  • Thread starter Thread starter Tim
  • Start date Start date
T

Tim

Can someone please tell me how I can get the last file added to a directory
( I am assuming it is a file attribute). I have built a text parsing
application. Right now I have to manually get the file to load it up. What
I want to be able to do is have the application (on load) check a directory,
get the last file added and parse the file itself. Can someone please tell
me how to do this?

Thanks
Tim
 
I don't know if there is some way to sort the files in a folder by date but
you could do this by using Directory.GetFiles and then iterate through the
files and compare the dates with File.GetAttributes(path).

Rick
 
Tim said:
Can someone please tell me how I can get the last file added to a directory
( I am assuming it is a file attribute). I have built a text parsing
application. Right now I have to manually get the file to load it up. What
I want to be able to do is have the application (on load) check a directory,
get the last file added and parse the file itself. Can someone please tell
me how to do this?

Thanks
Tim

The following is what I knocked-up for you, I'm sure someone else will
find a better way, but it might get you started - (watch for wrapping)

Function fnMostRecentlyCreatedFile(ByVal sPathName As String) As String
Dim iIndexCounter, iNewestFileIndex As Integer
Dim dNewestFileDateTime, dCreationDateTime As Date
Dim sFiles() As String = Directory.GetFiles(sPathName, "*.*",
SearchOption.TopDirectoryOnly)
For Each sFileName As String In sFiles
dCreationDateTime = Directory.GetLastWriteTime(sFileName)
If dCreationDateTime > dNewestFileDateTime Then
dNewestFileDateTime = dCreationDateTime
iNewestFileIndex = iIndexCounter
End If
iIndexCounter += 1
Next
Return Path.GetFileName(sFiles(iNewestFileIndex))
End Function


To use -

MsgBox(String.Format("The Most Recently Created Windows File is {0}",
fnMostRecentlyCreatedFile("C:\Windows")))


Hope this helps.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
ShaneO said:
The following is what I knocked-up for you, I'm sure someone else will
find a better way, but it might get you started - (watch for wrapping)

Almost forgot - You'll need the following NameSpace:

Imports System.IO

but you probably already knew that!

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
Thanks Shane I appreciate it very much.
ShaneO said:
The following is what I knocked-up for you, I'm sure someone else will
find a better way, but it might get you started - (watch for wrapping)

Function fnMostRecentlyCreatedFile(ByVal sPathName As String) As String
Dim iIndexCounter, iNewestFileIndex As Integer
Dim dNewestFileDateTime, dCreationDateTime As Date
Dim sFiles() As String = Directory.GetFiles(sPathName, "*.*",
SearchOption.TopDirectoryOnly)
For Each sFileName As String In sFiles
dCreationDateTime = Directory.GetLastWriteTime(sFileName)
If dCreationDateTime > dNewestFileDateTime Then
dNewestFileDateTime = dCreationDateTime
iNewestFileIndex = iIndexCounter
End If
iIndexCounter += 1
Next
Return Path.GetFileName(sFiles(iNewestFileIndex))
End Function


To use -

MsgBox(String.Format("The Most Recently Created Windows File is {0}",
fnMostRecentlyCreatedFile("C:\Windows")))


Hope this helps.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
Back
Top