Looking for an elegant solution to find a file within a fileshare

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

    Looking for an elegant solution to find a file within a fileshare

    Hi,
    I'm looking for an elegant solution on how to find the youngest file
    within a given directory.
    At the moment I'm storing all files in an array and loop through it
    comparing the creation date as follows:
    private string FileShare(strin g strURL)
    {
    string [] files;
    DateTime datLastWriteTim e;
    DateTime datNewLastWrite Time;
    string strNewestDoc ;
    files = Directory.GetFi les(strURL);
    if (files.Length > 0)
    {
    datNewLastWrite Time = File.GetLastWri teTime(files[0]);
    strNewestDoc = files[0];
    //find latest doc
    foreach(string fileName in files)
    {
    datLastWriteTim e = File.GetLastWri teTime(fileName );
    if (datLastWriteTi me > datNewLastWrite Time)
    {
    datNewLastWrite Time = datLastWriteTim e;
    strNewestDoc = fileName;
    }
    }
    return strNewestDoc;
    }
    else
    return "";
    }
    This is ok for a directory with a few documents, but when there is
    hundreds of them, I don't think performance will be very good...
    Please help
    Thanks
    Rosa
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Looking for an elegant solution to find a file within a fileshare

    Rosa,

    This is probably the best solution that you have. You could add all the
    files into a sortable data source, like a data set (with one column being
    the file name, and another being the date), and then sorting based on the
    criteria you have. However, that would probably take longer, because you
    have to cycle through the files in the directory and then place them in the
    structure, and then sort the structure.

    You are reducing most of the overhead here, and this is probably the
    quickest way. The only other thing that I can think of would be to use
    Index Serivces and then interop with that (I am not positive, but I seem to
    recall that index services is geared for this kind of thing).

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Rosa" <Rosa_Shelton@h otmail.com> wrote in message
    news:3988c67a.0 311100524.354cd 8d0@posting.goo gle.com...[color=blue]
    > Hi,
    > I'm looking for an elegant solution on how to find the youngest file
    > within a given directory.
    > At the moment I'm storing all files in an array and loop through it
    > comparing the creation date as follows:
    > private string FileShare(strin g strURL)
    > {
    > string [] files;
    > DateTime datLastWriteTim e;
    > DateTime datNewLastWrite Time;
    > string strNewestDoc ;
    > files = Directory.GetFi les(strURL);
    > if (files.Length > 0)
    > {
    > datNewLastWrite Time = File.GetLastWri teTime(files[0]);
    > strNewestDoc = files[0];
    > //find latest doc
    > foreach(string fileName in files)
    > {
    > datLastWriteTim e = File.GetLastWri teTime(fileName );
    > if (datLastWriteTi me > datNewLastWrite Time)
    > {
    > datNewLastWrite Time = datLastWriteTim e;
    > strNewestDoc = fileName;
    > }
    > }
    > return strNewestDoc;
    > }
    > else
    > return "";
    > }
    > This is ok for a directory with a few documents, but when there is
    > hundreds of them, I don't think performance will be very good...
    > Please help
    > Thanks
    > Rosa[/color]


    Comment

    • Rosa

      #3
      Re: Looking for an elegant solution to find a file within a fileshare

      Thank Nicholas! I will carry on working down the lines of this solution.

      Rosa

      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in message news:<e$MJ4O5pD HA.2848@TK2MSFT NGP10.phx.gbl>. ..[color=blue]
      > Rosa,
      >
      > This is probably the best solution that you have. You could add all the
      > files into a sortable data source, like a data set (with one column being
      > the file name, and another being the date), and then sorting based on the
      > criteria you have. However, that would probably take longer, because you
      > have to cycle through the files in the directory and then place them in the
      > structure, and then sort the structure.
      >
      > You are reducing most of the overhead here, and this is probably the
      > quickest way. The only other thing that I can think of would be to use
      > Index Serivces and then interop with that (I am not positive, but I seem to
      > recall that index services is geared for this kind of thing).
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      > "Rosa" <Rosa_Shelton@h otmail.com> wrote in message
      > news:3988c67a.0 311100524.354cd 8d0@posting.goo gle.com...[color=green]
      > > Hi,
      > > I'm looking for an elegant solution on how to find the youngest file
      > > within a given directory.
      > > At the moment I'm storing all files in an array and loop through it
      > > comparing the creation date as follows:
      > > private string FileShare(strin g strURL)
      > > {
      > > string [] files;
      > > DateTime datLastWriteTim e;
      > > DateTime datNewLastWrite Time;
      > > string strNewestDoc ;
      > > files = Directory.GetFi les(strURL);
      > > if (files.Length > 0)
      > > {
      > > datNewLastWrite Time = File.GetLastWri teTime(files[0]);
      > > strNewestDoc = files[0];
      > > //find latest doc
      > > foreach(string fileName in files)
      > > {
      > > datLastWriteTim e = File.GetLastWri teTime(fileName );
      > > if (datLastWriteTi me > datNewLastWrite Time)
      > > {
      > > datNewLastWrite Time = datLastWriteTim e;
      > > strNewestDoc = fileName;
      > > }
      > > }
      > > return strNewestDoc;
      > > }
      > > else
      > > return "";
      > > }
      > > This is ok for a directory with a few documents, but when there is
      > > hundreds of them, I don't think performance will be very good...
      > > Please help
      > > Thanks
      > > Rosa[/color][/color]

      Comment

      Working...