Remove part of filename of files that are in different folders

43 views Asked by At

I want to remove part of a filename of several files, with different extensions, that are in separate folders, i.e., I have a 'master folder' that has 90 folders, and the files are in these folders. How could I remove just part of all files, in all folders, automaticaly.

Regards

1

There are 1 answers

0
BVintila On

What you can do is create a recursive method that renames all files in a directory and then calls the method for other directories in that one. The code below should serve you as guideline (it cuts the first 5 chars of the filename):

    public void RenameFiles(DirectoryInfo dir)
    {
        foreach (var file in dir.GetFiles())
        {
            file.MoveTo(Path.Combine(file.Directory.FullName, file.Name.Substring(5)));
        }
        foreach(var directory in dir.GetDirectories())
        {
            RenameFiles(directory);
        }
    }

I assumed you use C# because this is my main language. The mechanism is the same regardless the language.