Click here to Skip to main content
15,912,400 members
Articles / Programming Languages / C#
Article

Getting File Info

Rate me:
Please Sign up or sign in to vote.
2.17/5 (5 votes)
26 Feb 2008CPOL 20.4K   16   2
Getting File Info from files in directory and subdirectories

Introduction

Get Information about when files were last updated in a directory.

Background

I was developing an ASP application where we worked on a TEST server first and then copied the files that contained the updated files to the LIVE server, I found myself forgetting on what files i had updated, so i decided to build an easy console app to show me what files that had been updated.

Using the code

static string path = @"Z:\";

    static void Main(string[] args)
    {

        if (!Directory.Exists(path))
        {
            Console.WriteLine("The Directory doesnt exist, or it cant be accessed!");
            Console.ReadLine();
        }
        else
        {

            List<string> fl = Getfiles(path);

            foreach (string f in fl)
            {

                StreamWriter sw;
                sw = File.AppendText(@"T:\Desktop\test.txt");
                sw.WriteLine(f);

                sw.Close();
                Console.WriteLine(f);
            }
        }

        Console.ReadLine();
    }
public static List<string> Getfiles(string path)
     {
         List<string> fileList = new List<string>();
         string[] subDirectories = Directory.GetDirectories(path);
         string[] files = Directory.GetFiles(path);

         foreach (string file in files)
         {
             DateTime dt = File.GetLastWriteTime(file);
             if (dt.ToShortDateString() == DateTime.Today.ToShortDateString())
             {

                 fileList.Add(file + " " + dt);

             }
         }
         foreach (string di in subDirectories)
         {
             string[] fi = Directory.GetFiles(di);
             foreach (string f in fi)
             {
                 DateTime dt = File.GetLastWriteTime(f);
                 if (dt.ToShortDateString() == DateTime.Today.ToShortDateString())
                 {
                     fileList.Add(f +" "+ dt);

                 }
             }
         }

         return fileList;
     }

Points of Interest

This is my first article, so please be gentle :) Im sure theres alot of things you can do better than I have done. Feel free to comment on things that can be improved.

History

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Amir Mohammad Nasrollahi13-Aug-13 9:33
professionalAmir Mohammad Nasrollahi13-Aug-13 9:33 
GeneralUse the built-in types... Pin
Ray Hayes26-Feb-08 23:58
Ray Hayes26-Feb-08 23:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.