locked
Get file list in folder

    Question

  • Just playing around with getting lists of files or subfolders of a given folder or its entire subfolder tree.  Everything is working fine except that several (not all, and no apparent pattern) folders show a file desktop.ini that does not appear in the Windows Explorer view of the folder with the option to display system and hidden files checked.  (During testing I noticed one other file with the same behaviour.)

    All I wanted returned were the normal user files.  The code used (and don't laugh at the exception "handling" - it was only a proof of concept after all) is:

            /// <summary>
            /// Get the names of all files in a specified folder
            /// </summary>
            /// <param name="root">The full path of the folder required</param>
            /// <returns>string[] containing an array of all fully qualified filenames in that folder</returns>
            public static string[] GetFiles(string root)
            {
                string[] files = null;
                try
                {
                    files = Directory.GetFiles(root);
                }
                catch (Exception)
                {
                    throw new Exception("What's going on?");
                }
                return files;
            }
    
    

    Can someone please explain to me why these odd files are cropping up yet according to Win Explorere are apparently not system or hidden (both of which I would have thought they were) and how I could modify the code to exclude such cases?  (Bear in mind there was at least one other file that did not have a .ini extension, so I can't safely just exclude them on file/extension name).

    Friday, September 16, 2011 1:44 PM

Answers

  • Hi,

    I have modifed your code,check ait and let me know if it works

     

    public static string[] GetFiles(string root)

    {

     

    string[] files = null;

     

    try
     

    {

     

     

    files =

    Directory.GetFiles(root);

     

    foreach (String path in files)

    {

     

    FileInfo f = new FileInfo(path);

     

    if (((f.Attributes == FileAttributes.Hidden) || (f.Attributes == FileAttributes.Archive)))

    {

     

    MessageBox.Show(path );

     

    //add the things to your folder
     

    }

    }

    }

     

    catch (Exception)

    {

     

    throw new Exception("What's going on?");

    }

     

    return files;

    }

     

    Regards,

    A.Murugan


    If it solved your problem,Please click "Mark As Answer" on that post and "Mark as Helpful". Happy Programming!
    • Edited by Murugan A Friday, September 16, 2011 2:27 PM
    • Marked as answer by Bill Hardwick Friday, September 16, 2011 3:01 PM
    Friday, September 16, 2011 2:21 PM

All replies

  • If Directory.GetFiles returns them they definitely exist. As to why they don't show up in Windows Explorer, I have no idea. Out of curiousity, what happens when you list the contents of these folders from the command prompt? Use dir /a to include hidden and system folders.

    When listing the files in your code, if you want to include all system and hidden files except desktop.ini, then your only option is to exclude on the literal file name.

    Here's a version that takes an array of lower-case file names to exclude, and uses a LINQ query to filter the results of GetFiles:

    public static string[] GetFiles(string root, string[] exclude)
        {
            string[] files = null;
            try
            {
                string[] allFiles = Directory.GetFiles(root);
                var fQuery = from string file in allFiles
                                where !exclude.Contains(new FileInfo(file).Name.ToLower())
                                select file;
                files = new List<string>(fQuery).ToArray();
            }
            catch (Exception)
            {
                throw new Exception("What's going on?");
            }
            return files;
        }
    

     


    jmh
    Friday, September 16, 2011 2:12 PM
  • And if you want to get fancy, and also handle an exceptions in the code that calls GetFiles (instead of in GetFiles itself) you could collapse the whole function to:

        public static string[] GetFiles(string root, string[] exclude)
        {
            return new List<string>(from string file in Directory.GetFiles(root)
                                    where !exclude.Contains(new FileInfo(file).Name.ToLower())
                                    select file).ToArray();
        }
    

     


    jmh
    Friday, September 16, 2011 2:15 PM
  • Hi,

    I have modifed your code,check ait and let me know if it works

     

    public static string[] GetFiles(string root)

    {

     

    string[] files = null;

     

    try
     

    {

     

     

    files =

    Directory.GetFiles(root);

     

    foreach (String path in files)

    {

     

    FileInfo f = new FileInfo(path);

     

    if (((f.Attributes == FileAttributes.Hidden) || (f.Attributes == FileAttributes.Archive)))

    {

     

    MessageBox.Show(path );

     

    //add the things to your folder
     

    }

    }

    }

     

    catch (Exception)

    {

     

    throw new Exception("What's going on?");

    }

     

    return files;

    }

     

    Regards,

    A.Murugan


    If it solved your problem,Please click "Mark As Answer" on that post and "Mark as Helpful". Happy Programming!
    • Edited by Murugan A Friday, September 16, 2011 2:27 PM
    • Marked as answer by Bill Hardwick Friday, September 16, 2011 3:01 PM
    Friday, September 16, 2011 2:21 PM
  • Thanks jmh - haven't really dipped my toe into LINQ yet but that's an easy to understand example (and I like your fancy follow-up too!) and clearly better than my unimplemented initial thought of iterating through the file list to eliminate it.

    Unfortunately it doesn't completely solve my problem because, as stated earlier, there was at least one other "phantom" file that showed up during testing one of the recursive calls to get file in the complete subfolder tree whose name I can't remember but it was certainly not desktop.ini or any other .ini file.  So what I really need is a filter to remove all hidden or system files, which, Win Exp notwithstanding, I take desktop.ini to be.

    (Windows Explorer's anomaly remains a mystery to both of us, but following your suggestion I tried directory listing from the command prompt.  Without the /a switch, all that showed up were the "normal" files that appear in Explorer.  With the /a switch... voila - desktop.ini appeared.)

    Friday, September 16, 2011 2:28 PM
  • The desktop.ini files are system files and Explorer seems to be configured to not ever show them. There might be a Registry key that controls this but I've not investigated.

    I would just ignore them. I once deleted one in a users settings folder and had the devil of a job getting to log on as the user.

    As to other files, there are some system files that do not show in Explorer - if e.g. you do a dir *.* /s from C:\ in a command window you get a different count to that in the Explorer properties tab for C:\.

     


    Regards David R
    ---------------------------------------------------------------
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.
    Friday, September 16, 2011 2:29 PM
  • Go to the Control Panel, Folder Options and in the View tab check the Show hidden files, folders and drives. Then you should see desktop.ini files in Explorer.
    Friday, September 16, 2011 2:33 PM
  • Desktop.ini should appear if you choose to display hidden files and do not choose to hide system files.
    Friday, September 16, 2011 2:34 PM
  • Thanks Murugan - that confirms the nature of the file and provides the means of a solution.

    Interestingly I had one further anomaly when I ran your code.  Reporting one directory, a number of other archived files were flagged up in your message box, but not the desktop.ini file.  Running on a second directory, desktop.ini was flagged up.

    Using debug and a checkpoint it turns out that in the second case the file's attributes were hidden and system; in the first case it was hidden, system and archived.  So one wonders why it wasn't flagged in the first case.  I understood the || operator to be an inclusive OR which short-circuits if the first condition is true.  As I type this, the thought occurs to me that maybe just using the == operator tests the first attribute only, and maybe by default that is system, not either of the other two?  Don't have time to check this out at the moment, but I suspect that may be the answer.

    Friday, September 16, 2011 3:12 PM
  • Thanks Riced - your second sentence is precisely what bothered me.  The little test prototype was with a view to a personalised file management utility which in addition to backing files up might well include deletion features... and not keen on deleting system files accidentally for all the obvious reasons!
    Friday, September 16, 2011 3:15 PM