简体   繁体   中英

to get the number of files recursively

I'm trying to count the number of *.tmp files in the computer. When I click on a button the labelbox should start increasing the count number for each search of .tmp file. my below coding does not seems to be working.

Any help is much appreciated. Also when I click on button to get the number of tmp files; the lable box should start showing the increase in count like 1,2,,,,,56,,89,,,etc rather than showing just the total number of files.

namespace finalclean {

public partial class FinalClean : Form {

    public FinalClean() {

        InitializeComponent();
    }

    int count = 0; 

    private void button1_Click(object sender, EventArgs e) {

       String path = Environment.ExpandEnvironmentVariables(@"c:\windows");

       try {

          foreach (string dirPath in Directory.GetDirectories(path)) {

               foreach (string filePath in Directory.GetFiles(dirPath)) {

                   string filename = Path.GetFileName(filePath);

                   if (filename.Equals("*.tmp")) {
                       count++;
                   }
               }
           }
       }
         catch (System.Exception excpt) {
             //Console.WriteLine(excpt.Message);
         }
       textBox1.Text = (count.ToString());
    }

  }

}

You could just use:

var count = Directory.EnumerateFiles(@"C:\Windows\", "*.tmp", 
                      SearchOption.AllDirectories).Count();

Your code doesn't work because you're not visiting all the directories, but only the fist one (C:\\Windows).

If you make it recursive it can do the trick for you.

internal class Program
{
    private static void Main(string[] args)
    {
        var myClass = new Visitor();
        myClass.OnNewFileFound +=Progress;
        myClass.CountTmpFiles();
        Console.Read();
    }

    private static void Progress(int i)
    {
        Console.WriteLine(i); //update textbox here
    }
}

public class Visitor
{
    public event Action<int> OnNewFileFound;
    private int count = 0;

    public int CountTmpFiles()
    {
        var path = Environment.ExpandEnvironmentVariables(@"c:\windows");
        VisitDir(path);
        return count;
    }

    private void VisitDir(string path)
    {
        try
        {
            foreach (var directory in Directory.GetDirectories(path))
            {
                VisitDir(directory); //recursive call here
            }
            foreach (var filePath in Directory.GetFiles(path, "*.tmp", SearchOption.TopDirectoryOnly))
            {
                count++;
                if (OnNewFileFound != null)
                {
                    OnNewFileFound(count);
                }
            }
        }
        catch (System.Exception excpt)
        {
            //Console.WriteLine(excpt.Message);
        }
    }
}

And yes, if you're only counting files, then I doesn't make sense to write your own code. You just use Directory.EnumerateFiles as Andrew and Hamlet Hakobyan suggested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM