|
-
Jul 10th, 2007, 08:28 PM
#1
Thread Starter
PowerPoster
[RESOLVED] get all files in folder that are newer than a certain time.
Hey guys, I am trying to open up a folder and find all files of a certain type that are newer than a certain date/time.
I have this code here which gets all the files of the specified type, but I am not sure how to get it to also read the date/time of the file and only add ones that are newer than a specified date/time. Talking about the Creation date/time or last modified date/time whichever.
Code:
DirectoryInfo di = new DirectoryInfo(folderpath);
FileInfo[] rgFiles = di.GetFiles("*.env");//where date/time newer than 7/07/2007 5:34 PM
Any idea for an ellegant way of doing this?
Thanks!
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Jul 10th, 2007, 09:22 PM
#2
Re: get all files in folder that are newer than a certain time.
Is this elegant enough?
C# Code:
DateTime dt = new DateTime(2007,07,7,5,34,0);
DirectoryInfo di = new DirectoryInfo(folderpath);
foreach(FileInfo fi in di.GetFiles("*env"))
{
if (fi.CreationTime > dt)
{
}
}
-
Jul 11th, 2007, 04:32 PM
#3
Thread Starter
PowerPoster
Re: get all files in folder that are newer than a certain time.
That looks promising. Thanks a lot.
Now to go test it out
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Jul 11th, 2007, 05:05 PM
#4
Thread Starter
PowerPoster
Re: get all files in folder that are newer than a certain time.
Ok, thanks again for the help. I am trying to make a method out of this code here, but apparently I don't know enough about handling arrays in C# to get it to work.
I am inputing a folderpath into the method then I want to return an array of file names. But I am getting an error that says, "Use of unassigned local variable 'theFiles'" which is the array I am trying to store the file names into.
There may be other things wrong with the code as well. Anyone care to give me a lesson on C# arrays?
Thanks!
Code:
private string[] getFiles(string folderPath)
{
string[] theFiles;
DateTime dt = new DateTime(2006, 07, 7, 5, 34, 0);
DirectoryInfo di = new DirectoryInfo(folderPath);
FileInfo[] fi = di.GetFiles("*.env");
for (int i = 0; i < fi.Length; i++)
{
if (fi[i].CreationTime > dt)
{
theFiles[i] = fi.GetValue(i).ToString();
}
}//end for
return theFiles;
}//end getFiles
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Jul 11th, 2007, 05:49 PM
#5
Thread Starter
PowerPoster
Re: get all files in folder that are newer than a certain time.
Hrmmm. Ok I figured out that arrays have to be intialized and how to do it, I think. By adding this line to the code posted above I no longer get an error
theFiles = new string[] { };
However, I must be doing someting wrong when calling the method because it is not giving any results.
Here is the method
Code:
private string[] getFiles(string folderPath)
{
string[] theFiles;
theFiles = new string[] { };
DateTime dt = new DateTime(2006, 07, 7, 5, 34, 0);
DirectoryInfo di = new DirectoryInfo(folderPath);
FileInfo[] fi = di.GetFiles("*.env");
for (int i = 0; i < fi.Length-1; i++)
{
if (fi[i].CreationTime > dt)
{
theFiles[i] = fi.GetValue(i).ToString();
}
}//end for
return theFiles;
}//end getFiles
And here is how I am calling it
Code:
string[] fileArray;
fileArray = new string[] { };
fileArray = getFiles(pathwaysPath);
for (int i = 0; i < fileArray.Length; i++)
{
MessageBox.Show(fileArray[i]);
}//end for
Any Idears?
Thanks!
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Jul 11th, 2007, 06:58 PM
#6
Re: get all files in folder that are newer than a certain time.
Code:
public string[] GetFilesAfterDate(string folder, DateTime date)
{
FileInfo[] files = new DirectoryInfo(folder).GetFiles();
List<FileInfo> fileList = new List<FileInfo>();
fileList.AddRange(files);
if (files.Length > 0)
{
for (int index = 0; index < files.GetUpperBound(0); index++)
{
if (files[index].LastWriteTime > date)
{
fileList.Add(files[index]);
}
}
}
string[] filePaths = new string[fileList.Count];
for (int index = 0; index < filePaths.GetUpperBound(0); index++)
{
filePaths[index] = fileList[index].FullName;
}
return filePaths;
}
-
Jul 12th, 2007, 02:48 PM
#7
Thread Starter
PowerPoster
Re: get all files in folder that are newer than a certain time.
Thanks for that code jmc. I am getting a return result and I am able to loop thru the results. However, it doesn't seem to be taking the date into account. It is returning every file regardless of date.
I tried changing the line if (files[index].LastWriteTime >date) to CreationTime, CreationTimeUtc, LastWriteTimeUtc etc.... But I get the same result.
The Date I am passing looks like 7/7/2007 2:34:35 PM for example. It is a string I am pulling out of the registry that I am converting to DateTime.
Here is the code I am using to call the method and then show the results..for testing purposes.
Code:
string[] theFiles;
theFiles = new string[] { };
DateTime scan = Convert.ToDateTime(lastScan);
theFiles = GetFilesAfterDate(pathwaysPath,scan);
for (int i = 0; i < theFiles.Length; i++)
{
MessageBox.Show(theFiles[i]);
}//end for
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Jul 12th, 2007, 03:35 PM
#8
Thread Starter
PowerPoster
Re: get all files in folder that are newer than a certain time.
Never mind, I figured out the issue.
I just had to remove this line fileList.AddRange(files);
Thanks again!
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Jul 12th, 2007, 05:53 PM
#9
Re: [RESOLVED] get all files in folder that are newer than a certain time.
Oops! Sorry about that. I changed the implementation and forgot to remove that line from the way I was doing it previously.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|