Results 1 to 9 of 9

Thread: [RESOLVED] get all files in folder that are newer than a certain time.

  1. #1

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    Resolved [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.


  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: get all files in folder that are newer than a certain time.

    Is this elegant enough?

    C# Code:
    1. DateTime dt = new DateTime(2007,07,7,5,34,0);
    2.             DirectoryInfo di = new DirectoryInfo(folderpath);
    3.             foreach(FileInfo fi in di.GetFiles("*env"))
    4.             {
    5.                 if (fi.CreationTime > dt)
    6.                 {
    7.  
    8.                 }
    9.             }
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    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.


  4. #4

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    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.


  5. #5

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    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.


  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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;
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    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.


  8. #8

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    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.


  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width