-1

I need to change a lot of file names all in the same way. They all share a common root but are scattered through out that root in other folders. I am trying to write a program in c# that starts at the root and recursively goes through all of its children and either changes file names or goes into the next folder. Here is what I have:

public void cascade(string path)
    {
        if(IsDirectoryEmpty(path) == true) 
        { }
        else
        {
            String[] childFolders = Directory.GetDirectories(path);
            String[] childFiles = Directory.GetFiles(path);
            ChangeFileName();

            for (int p = 0; p < childFolders.Length; p++)
            {
                cascade(childFolders[p]);
            }
        }
    }

A button calls to cascade and passes in the initial root directory. I then found someones method they wrote to check if its empty:

public bool IsDirectoryEmpty(string path)
    {
        return !Directory.EnumerateFileSystemEntries(path).Any();
    }

Im trying to change the fileNames everytime I enter a new directory and if there are more folders I enter them to change more file names. It isnt working and I have no clue why. Changing the file names works but this method isnt reaching all of the folders.

  • 4
    possible duplicate of [Searching for file in directories recursively](http://stackoverflow.com/questions/9830069/searching-for-file-in-directories-recursively) – Alex A. Feb 25 '15 at 19:50

5 Answers5

0

The functionality exist, it will implement the Enum SearchOption.AllDirectories. This will trigger the recursive functionality:

var directories = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);

You can also do the same thing for DirectoryInfo with some slight modification.

searchOption Type:

System.IO.SearchOption One of the enumeration values that specifies whether the search operation should include only the current directory or all subdirectories.

More information about this can be found here.

Important note, once you've obtained the list of those directories you can loop through and modify in whatever manner you would like. You'll also want to implement the File.Move if your simply renaming your file.

Greg
  • 11,302
  • 2
  • 48
  • 79
0

Use the below method. Pass the root directory in the function as the parameter.

void DirSearch(string sDir) 
{
try 
{
   foreach (string d in Directory.GetDirectories(sDir)) 
   {
    foreach (string f in Directory.GetFiles(d, txtFile.Text)) 
    {
       ChangeFileName();
    }
    DirSearch(d);
   }
}
catch (System.Exception excpt) 
{
    Console.WriteLine(excpt.Message);
}
}
Utsav Dawn
  • 7,896
  • 2
  • 29
  • 44
0

Recursive functions are hard to debug. You can try a non-recursive version like this:

    public void ChangeAllFiles(String root)
    {
        Stack<String> searchStack = new Stack<string>();
        searchStack.Push(root);

        while (searchStack.Count > 0)
        {
            String path = searchStack.Pop();
            foreach (var file in Directory.GetFiles(path))
            {
                ChangeFileName(file);
            }

            foreach (var subDir in Directory.GetDirectories(path))
            {
                searchStack.Push(subDir);
            }
        }
    }

If that doesn't work: What folders are missing?

bernd_rausch
  • 335
  • 2
  • 9
0

this one should do the trick.

public void cascade(string path)
{
    String[] childFiles = Directory.GetFiles(path);
    if(childFiles.lenght > 0)
    {
        String[] childFolders = Directory.GetDirectories(path); 
        ChangeFileName(childFiles); //iterate and change each fileName

        for (int p = 0; p < childFolders.Length; p++)
        {
            cascade(childFolders[p]);
        }
    }
}
RobDev
  • 45
  • 1
  • 9
  • This only looks in sub-directories if there are also files present in the current directory. – juharr Feb 25 '15 at 20:37
0

I needed to change the changeFiles method to accept a path as an arguement. Then in the cascade method I passed the change files method the path that was passed to the cascade method.

public void cascade(string path)
    {
        if(IsDirectoryEmpty(path) == true) 
        { }
        else
        {
            String[] childFolders = Directory.GetDirectories(path);
            String[] childFiles = Directory.GetFiles(path);
            ChangeFileName(path);

            for (int p = 0; p < childFolders.Length; p++)
            {
                cascade(childFolders[p]);
            }
        }
    }