Reading Directory and Files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim

    #1

    Reading Directory and Files

    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


  • Rick

    #2
    Re: Reading Directory and Files

    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.GetFi les and then iterate through the
    files and compare the dates with File.GetAttribu tes(path).

    Rick


    "Tim" <gtu@cablelynx. comwrote in message
    news:uuGo21FXHH A.4964@TK2MSFTN GP06.phx.gbl...
    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
    >

    Comment

    • ShaneO

      #3
      Re: Reading Directory and Files

      Tim wrote:
      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 fnMostRecentlyC reatedFile(ByVa l sPathName As String) As String
      Dim iIndexCounter, iNewestFileInde x As Integer
      Dim dNewestFileDate Time, dCreationDateTi me As Date
      Dim sFiles() As String = Directory.GetFi les(sPathName, "*.*",
      SearchOption.To pDirectoryOnly)
      For Each sFileName As String In sFiles
      dCreationDateTi me = Directory.GetLa stWriteTime(sFi leName)
      If dCreationDateTi me dNewestFileDate Time Then
      dNewestFileDate Time = dCreationDateTi me
      iNewestFileInde x = iIndexCounter
      End If
      iIndexCounter += 1
      Next
      Return Path.GetFileNam e(sFiles(iNewes tFileIndex))
      End Function


      To use -

      MsgBox(String.F ormat("The Most Recently Created Windows File is {0}",
      fnMostRecentlyC reatedFile("C:\ Windows")))


      Hope this helps.

      ShaneO

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

      Comment

      • ShaneO

        #4
        Re: Reading Directory and Files

        ShaneO wrote:
        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.

        Comment

        • Tim

          #5
          Re: Reading Directory and Files

          Thanks Shane I appreciate it very much.
          "ShaneO" <spcacc@optusne t.com.auwrote in message
          news:45e77d7f$0 $5747$afc38c87@ news.optusnet.c om.au...
          Tim wrote:
          >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 fnMostRecentlyC reatedFile(ByVa l sPathName As String) As String
          Dim iIndexCounter, iNewestFileInde x As Integer
          Dim dNewestFileDate Time, dCreationDateTi me As Date
          Dim sFiles() As String = Directory.GetFi les(sPathName, "*.*",
          SearchOption.To pDirectoryOnly)
          For Each sFileName As String In sFiles
          dCreationDateTi me = Directory.GetLa stWriteTime(sFi leName)
          If dCreationDateTi me dNewestFileDate Time Then
          dNewestFileDate Time = dCreationDateTi me
          iNewestFileInde x = iIndexCounter
          End If
          iIndexCounter += 1
          Next
          Return Path.GetFileNam e(sFiles(iNewes tFileIndex))
          End Function
          >
          >
          To use -
          >
          MsgBox(String.F ormat("The Most Recently Created Windows File is {0}",
          fnMostRecentlyC reatedFile("C:\ Windows")))
          >
          >
          Hope this helps.
          >
          ShaneO
          >
          There are 10 kinds of people - Those who understand Binary and those who
          don't.

          Comment

          Working...