Pregunta

I am having an issue with trying to make a list by searching through a file structure. Was trying to make a basic c# console program that would just run and do this.

My structure is organize like the following.

My Network \
X1 \ Users \ (many many user folders) \ Search for a specific sub folder \ make a list in a text file of any folders within this sub folder

So i have to be able to search every user folder and then check for a folder (this will be the same every time) Then make a list of the found folders within that sub folder with the following format

username (name of the user folder) >> Name of folder within the specific folder.

its been a terribly long time since i have had to try anything with searching within a file structure so blanking on this terribly.

**************** EDIT!!!!!

Thanks for the info and links. Working on this now but wondering if this makes sense and would work. Don't want to just test it before i make sure it looks like something that wouldn't just screw up.

            TextWriter outputText = new StreamWriter(@"C:\FileList.txt", true);
        outputText.WriteLine("Starting scan through user folder");
        string path = @"\\X1\users";
        string subFolder = "^^ DO NOT USE - MY DOCS - BACKUP ^^";
        string [] user_folders = Directory.GetDirectories(path);

        foreach (var folder in user_folders)
        {
            string checkDirectory = folder + "\\" + subFolder;

            if (Directory.Exists(checkDirectory) == true)
            {
                string [] inner_folders = Directory.GetDirectories(checkDirectory);
                foreach (var folder2 in inner_folders)
                {
                    outputText.WriteLine(folder2);
                }
            }
        }
        outputText.WriteLine("Finishing scan through user folder");
        outputText.Close();

Fixed and working!!!! had to change the string [] lines, to make it .GetDirectories instead of .GetFiles!!

¿Fue útil?

Solución

As Bali C mentioned, the Directory class will be your friend on this one. The following examples should get you started.

From: http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/3ea19b83-b831-4f30-9377-bc1588b94d23/

//Obviously you'll need to define the correct path.
string path = @"My Network\X1\Users\(many many user folders)\Search for a specific sub folder \";

// Will Retrieve count of all files in directry and sub directries
int fileCount = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Length; 

// Will Retrieve count of all files in directry but not sub directries
int fileCount = Directory.GetFiles(path, "*.*", SearchOption.TopDirectory).Length; 

// Will Retrieve count of files .txt extensions in directry and sub directries
int fileCount = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories).Length; 

If you need to search the /Users/ folder for certain people, or certain conditions you could do the following:

string path = @"PATH_TO_USERS_DIRECTORY";
string [] user_folders = Directory.GetFiles(path);
foreach(var folder in user_folders)
{
    if folder == "MyFolder";
        Process(folder);  //Search the directory here.
}

Otros consejos

Try the following implementation. This will just write to the console:

const string root = "<<your root path>>";
const string directoryToLookFor = "<<the folder name you are looking for>>";
foreach (var directory in Directory.EnumerateDirectories(root, "*.*", SearchOption.TopDirectoryOnly))
{
    var foundDirectory = Directory.EnumerateDirectories(directory, directoryToLookFor, SearchOption.TopDirectoryOnly).FirstOrDefault();
    if (!String.IsNullOrEmpty(foundDirectory))
    {
        var filesInside = Directory.GetFiles(foundDirectory);
        foreach (var file in filesInside)
        {
            Console.WriteLine(file);
        }
    }
}

Or you could just do:

foreach (var foundDirectory in Directory.EnumerateDirectories(root, directoryToLookFor, SearchOption.AllDirectories))
{
    var filesInside = Directory.GetFiles(foundDirectory);
    foreach (var file in filesInside)
    {
        Console.WriteLine(file);
    }
}

Which would search within all subdirectories without you having to iterate over the users' folders.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top