panman
panman

Reputation: 75

c# finding files with some extension on hard disk

I want to find all files in whole harddrive (.jpg, .png, .apk and a few other formats) but this code is not working..all it does is : for example if in my 'D:\' drive I have: 1)a image file 'i1', 2)a folder 'f1' which has image file and 3) another folder 'f2' which has image file and 'f2' contains folder 'f3' which has image file .It only performs 'calcFile()' on contents inside f1 and f2 but not on contents of f3 and i1 same happens with all other partitions .What is wrong here? Any help is appreciated. I hope you understand what is happening because that's the easiest way I could explain my situation, if more info is required just tell me. thanks :)

public void calcDirectory(string token)
    {
var validExtensions = new[]
        {

             ".jpg", ".png", ".apk"

        };
        foreach (string s in Directory.GetLogicalDrives())
        {
            DriveInfo driveInfo = new DriveInfo(s);
            if (driveInfo.IsReady)
            {


                foreach (string d in Directory.GetDirectories(s))
                {
                    FileAttributes attrs1 = File.GetAttributes(d);
                    if (!attrs1.HasFlag(FileAttributes.ReparsePoint))
                    {
                        if (!attrs1.HasFlag(FileAttributes.System))
                        { 
                    string[] allFilesInDir = Directory.GetFiles(d);
                    for (int i = 0; i < allFilesInDir.Length; i++)
                    {
                        string file = allFilesInDir[i];
                        string extension = Path.GetExtension(file);
                        if (validExtensions.Contains(extension))
                        {
                            string p = Path.GetFullPath(file);
                            FileAttributes attrs = File.GetAttributes(p);
                            if (attrs.HasFlag(FileAttributes.ReadOnly))
                            {
                                File.SetAttributes(p, attrs & ~FileAttributes.ReadOnly);
                                calcFile(file, token);
                            }
                            else
                            {
                                calcFile(file, token);
                            }
                        }
                    }

                }
            }
           }
         }
        }
    }

and I call the calcDirectory() here

public void start()
    {
         string token = File.ReadAllText(tokencalcalculated);
         calcDirectory(token); 
    }

Upvotes: 1

Views: 1311

Answers (2)

thinklarge
thinklarge

Reputation: 682

I created a function based on your code that will recursively search the directory for anything matching your pattern. You'll have to call this from your loop of the base directories.

public static void crawlDirectory(string token,string directory)
    {
        if (string.IsNullOrEmpty(directory)) return;
        var validExtensions = new[]
        {

             ".jpg", ".png", ".apk"

        };

        foreach (string d in Directory.GetDirectories(directory))
        {
            FileAttributes attrs1 = File.GetAttributes(d);
            if (!attrs1.HasFlag(FileAttributes.ReparsePoint) && !attrs1.HasFlag(FileAttributes.System))
            {
                string[] allFilesInDir = Directory.GetFiles(d);
                for (int i = 0; i < allFilesInDir.Length; i++)
                {
                    string file = allFilesInDir[i];
                    string extension = Path.GetExtension(file);
                    if (validExtensions.Contains(extension))
                    {
                        string p = Path.GetFullPath(file);
                        FileAttributes attrs = File.GetAttributes(p);
                        if (attrs.HasFlag(FileAttributes.ReadOnly))
                        {
                            File.SetAttributes(p, attrs & ~FileAttributes.ReadOnly);
                            calcFile(file, token);
                        }
                        else
                        {
                            calcFile(file, token);
                        }
                    }
                }
            }

            crawlDirectory(d, token);
        }
    }

Upvotes: 1

Ren&#233; Vogt
Ren&#233; Vogt

Reputation: 43876

You are only enumerating the root directories but no subdirectories. Instead of Directory.GetDirectories(s) use

foreach(string d in Directory.GetDirectories(s, "*", SearchOption.AllDirectories))

But be aware of infinite loops due to links in your subdirectories (see MSDN on SearchOption.AllDirectories).

And you should be aware of user access rights. You should put a try...catch around your GetFiles part in case you don't have access rights for a specific directory (See this question).

Upvotes: 1

Related Questions