1

What I'd like to do is get 6 pieces of file information from every file in a folder, and all folders beneath it. These pieces are:

  • Type (you can do this in Access, it will return "Microsoft Excel Worksheet" or "Winzip File" or something, just like you see in the Explorer window, but I didn't see a property for this in C#)
  • FullName
  • Length
  • CreationTime
  • LastAccessTime
  • LastWriteTime

I have the following code, which will spit out a list of files complete with path, but I'm trying to figure out how to get that additional info in my output. If it can be separated by a semicolon or something, that would be good as the eventual desire is to write this info to a table. Any help is appreciated.

    private void GetMyFiles () {
        string rootDirectory = @"" + txtPathToScan.Text + "";
        string extension = "*";

        List<string> files = GetFilesRecursively (rootDirectory, extension);

        Console.WriteLine ("Got {0} files of extension {1} in directory {2}", files.Count, extension, rootDirectory);
        foreach (string file in files) {
            Console.WriteLine (file);
        }
    }

    /// <summary>
    /// This function calls itself recursively for each of the subdirectories that it finds
    /// in the root directory passed to it. It returns files of the extension as specified
    /// by the caller.
    /// </summary>
    /// <param name="rootDirectory">The directory from which the file list is sought.</param>
    /// <param name="extension">The particular extension for which the file list is sought.</param>
    /// <returns>A list of all the files with extension as specified by the caller. This list
    /// includes the files in the current directory as well its sub-directories.</returns>
    static List<string> GetFilesRecursively (string rootDirectory, string extension) {
        // Uncomment this line only if you want to trace the control as it goes from
        // sub-directory to sub-directory. Be ready for long scrolls of text output:
        //Console.WriteLine ("Currently in directory {0}", rootDirectory);

        // Create an output list:
        List<string> opList = new List<string> ();

        // Get all files in the current directory:
        string[] allFiles = Directory.GetFiles (rootDirectory, "*." + extension);

        // Add these files to the output list:
        opList.AddRange (allFiles);

        // Get all sub-directories in current directory:
        string[] subDirectories = Directory.GetDirectories (rootDirectory);

        // And iterate through them:
        foreach (string subDir in subDirectories) {
            // Get all the files from the sub-directory:
            List<string> subDirFileList = GetFilesRecursively (subDir, extension);
            // And add it to this list:
            opList.AddRange (subDirFileList);
        }

        // Finally return the output list:
        return opList;
    }
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Johnny Bones
  • 8,786
  • 7
  • 52
  • 117

2 Answers2

1

You can get most of these using the FileInfo class....

var info = new System.IO.FileInfo(@"c:\folder\file.ext");

It has properties for Length, CreationTime, LastAccessTime and LastWriteTime, to output them (for example) to the console, you could do...

Console.WriteLine("For file {0}", info.Name);
Console.WriteLine("Length is {0}", info.Length);
Console.WriteLine("Creation time is {0}", info.CreationTime);
Console.WriteLine("Last access time is {0}", info.LastAccessTime);
Console.WriteLine("Last write time is {0}", info.LastWriteTime);

To get the full filename, you can use GetFileNameWithoutExtension...

var fileName = System.IO.Path.GetFileNameWithoutExtension(@"c:\folder\file.ext")

filename will equal "file" in the above example.

Getting the description of a file extension is a bit more work, but can be done using SHGetFIleInfo, See this answer for details.

To integrate this with your current code (if you're not so worried about the file descriptions), you could change your GetFilesRecursively method to return a List<FileInfo>...

static List<FileInfo> GetFilesRecursively (string rootDirectory, string extension)

Update your opList variable accordingly...

List<FileInfo> opList = new List<FileInfo> ();

And then tweak your AddRange call to add FileInfo objects...

opList.AddRange (allFiles.Select(f => new FileInfo(f)));

Lastly, the type of subDirFileList will need to be changed as well, so a List<FileInfo>.

(I think that's everything!)

Community
  • 1
  • 1
Richard Ev
  • 52,939
  • 59
  • 191
  • 278
  • How would I integrate this into what I already have, to get what I'm looking for? We'll leave out the SHGetFileInfo piece for now as that's more complex. – Johnny Bones Feb 21 '15 at 15:22
0

For posterity, the completed answer is as below. Please only vote for Richard's answer if you're going to upvote anything. Thanks!

    private void GetMyFiles () {
        string rootDirectory = @"" + txtPathToScan.Text + "";
        string extension = "*";

        List<FileInfo> files = GetFilesRecursively (rootDirectory, extension);

        Console.WriteLine ("Got {0} files of extension {1} in directory {2}", files.Count, extension, rootDirectory);
        foreach (FileInfo file in files)
        {
            Console.WriteLine(file);
            var info = new System.IO.FileInfo(@"" + file + "");

            Console.WriteLine("For file {0}", info.Name);
            Console.WriteLine("Length is {0} KB", info.Length/1024);
            Console.WriteLine("Creation time is {0}", info.CreationTime);
            Console.WriteLine("Last access time is {0}", info.LastAccessTime);
            Console.WriteLine("Last write time is {0}", info.LastWriteTime);

        }
    }

    /// <summary>
    /// This function calls itself recursively for each of the subdirectories that it finds
    /// in the root directory passed to it. It returns files of the extension as specified
    /// by the caller.
    /// </summary>
    /// <param name="rootDirectory">The directory from which the file list is sought.</param>
    /// <param name="extension">The particular extension for which the file list is sought.</param>
    /// <returns>A list of all the files with extension as specified by the caller. This list
    /// includes the files in the current directory as well its sub-directories.</returns>
    static List<FileInfo> GetFilesRecursively(string rootDirectory, string extension)
    {
        // Uncomment this line only if you want to trace the control as it goes from
        // sub-directory to sub-directory. Be ready for long scrolls of text output:
        //Console.WriteLine ("Currently in directory {0}", rootDirectory);

        // Create an output list:
        List<FileInfo> opList = new List<FileInfo>();

        // Get all files in the current directory:
        string[] allFiles = Directory.GetFiles (rootDirectory, "*." + extension);

        // Add these files to the output list:
        opList.AddRange(allFiles.Select(f => new FileInfo(f)));

        // Get all sub-directories in current directory:
        string[] subDirectories = Directory.GetDirectories (rootDirectory);

        // And iterate through them:
        foreach (string subDir in subDirectories) {
            // Get all the files from the sub-directory:
            List<FileInfo> subDirFileList = GetFilesRecursively (subDir, extension);
            // And add it to this list:
            opList.AddRange (subDirFileList);
        }

        // Finally return the output list:
        return opList;
    }
Johnny Bones
  • 8,786
  • 7
  • 52
  • 117