views:

565

answers:

2

Given a filename, how do I efficiently search for that file on disk?

(Visual Studio 2005, i.e. .NET 2.0)

+1  A: 

Your question is vague, you do not specify any programming language. So, you can do this using the command prompt:

dir /s /b d:\<filename>

or use the above in a system call from whatever language you're using.

In C/C++ or any other language that uses the native Win32 APIs use:

FindFirstFileEx

FindNextFile

FindClose

and recurse through any directories you encounter. In C#/VB/other .Net language, it's:

System.IO.Directory.GetFiles

System.IO.Directory.GetDirectories

Skizz

Skizz
A: 

If you want to implement the search mechanism, I'd start with something like this (C#)

using System;
using System.Collections.Generic;
using System.IO;

namespace Samples.FileSearcher
{
    public delegate void FileFoundHandler(string fileName);
    public delegate void SearchStatChangeHandler( bool newStat);
    public class FileSearch
    {
        private bool _isSearching;
        private FileFoundHandler _fileFound;
        private SearchStatChangeHandler _searchStatusChanged;
        public bool IsSearching { get { return _isSearching; } }
        public event FileFoundHandler FileFound{add { _fileFound += value; }remove { _fileFound -= value; }}
        public event SearchStatChangeHandler SearchingStatusChanged { add { _searchStatusChanged += value; } remove { _searchStatusChanged -= value; } }

        public void Search(string rootFolder, string filePattern)
        {
            ChangeStat(true);
            Queue<string> folderList = new Queue<string>();
            folderList.Enqueue(rootFolder);

            while (folderList.Count > 0)
            {
                string currentFolder = folderList.Dequeue();
                foreach (string folder in Directory.GetDirectories(currentFolder))
                    folderList.Enqueue(folder);
                foreach (string foundFile in Directory.GetFiles(currentFolder, filePattern))
                    if (_fileFound != null)
                        _fileFound(foundFile);
            }
            ChangeStat(false);
        }
        private void ChangeStat(bool newStat)
        {
            _isSearching = newStat;
            if (_searchStatusChanged != null) _searchStatusChanged(_isSearching);
        }

    }
}

That's just a quick-class for doing it. You should implement the form using it, some error handling on the Search Method and probably some canceling flags so you won't keep searching forever when you already found what you wanted.

I implemented my Form with something like this:

private void button1_Click(object sender, EventArgs e)
    {
        listView1.Items.Clear();
        Samples.FileSearcher.FileSearch searcher = new Samples.FileSearcher.FileSearch();
        searcher.FileFound += new FileFoundHandler(searcher_FileFound);
        searcher.Search(textBox1.Text, textBox2.Text);
    }

    void searcher_FileFound(string fileName)
    {
        listView1.Items.Add(fileName);
    }

If you have more specific doubts, please post them up and we'll try to look into it and do our best to help you out.