472,353 Members | 1,547 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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(string strURL)
{
string [] files;
DateTime datLastWriteTime;
DateTime datNewLastWriteTime;
string strNewestDoc ;
files = Directory.GetFiles(strURL);
if (files.Length > 0)
{
datNewLastWriteTime = File.GetLastWriteTime(files[0]);
strNewestDoc = files[0];
//find latest doc
foreach(string fileName in files)
{
datLastWriteTime = File.GetLastWriteTime(fileName);
if (datLastWriteTime > datNewLastWriteTime)
{
datNewLastWriteTime = datLastWriteTime;
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
Nov 15 '05 #1
2 1634
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]
- mv*@spam.guard.caspershouse.com

"Rosa" <Ro**********@hotmail.com> wrote in message
news:39**************************@posting.google.c om...
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(string strURL)
{
string [] files;
DateTime datLastWriteTime;
DateTime datNewLastWriteTime;
string strNewestDoc ;
files = Directory.GetFiles(strURL);
if (files.Length > 0)
{
datNewLastWriteTime = File.GetLastWriteTime(files[0]);
strNewestDoc = files[0];
//find latest doc
foreach(string fileName in files)
{
datLastWriteTime = File.GetLastWriteTime(fileName);
if (datLastWriteTime > datNewLastWriteTime)
{
datNewLastWriteTime = datLastWriteTime;
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

Nov 15 '05 #2
Thank Nicholas! I will carry on working down the lines of this solution.

Rosa

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:<e$**************@TK2MSFTNGP10.phx.gbl>...
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]
- mv*@spam.guard.caspershouse.com

"Rosa" <Ro**********@hotmail.com> wrote in message
news:39**************************@posting.google.c om...
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(string strURL)
{
string [] files;
DateTime datLastWriteTime;
DateTime datNewLastWriteTime;
string strNewestDoc ;
files = Directory.GetFiles(strURL);
if (files.Length > 0)
{
datNewLastWriteTime = File.GetLastWriteTime(files[0]);
strNewestDoc = files[0];
//find latest doc
foreach(string fileName in files)
{
datLastWriteTime = File.GetLastWriteTime(fileName);
if (datLastWriteTime > datNewLastWriteTime)
{
datNewLastWriteTime = datLastWriteTime;
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

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: cnu | last post by:
My program generates a log file for every event that happens in the program. So, I open the file and keep it open till the end. This is how I open...
1
by: Alex | last post by:
Hello, in my company, we have a diagnostic tool for hardware. Depending on the hardware projcet, a different project configuration is needed. The...
4
by: Tony Liu | last post by:
Hi, how can I create multiple new file handles of a file without having to share to file to the other processes? I have a file that will be...
8
by: Braky Wacky | last post by:
Hello, I have an ASP.NET webpage that uses an instance of System.Web.UI.HtmlControls.HtmlInputFile for uploading files to our server. I came...
9
by: Carl | last post by:
Hi Is it possible to find out if another process is using a paticular file (xml)? There does not seem to be anything useful in the File och...
13
by: Avi | last post by:
Hi, Is there a UNIX C system command that will let me copy a file? I am looking for something similar to "cp" that can be called within a C...
2
by: jjlagtap | last post by:
Hey everyone When I try to open a file i get the Exception listed below. The weird thing is it works when I run the web app locally and when i use...
2
by: Andez | last post by:
I've wrote a windows service that performs simple functions within our application. To ensure safe running of the service - if it errors we want to...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.