Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi there,

I need to know how to remove characters from folder names inside a folder.

So for example : If the folder name is MACED_Jul_2010.
Then I want to remove everything after the "MACED".

Any help?

Please, in C# programming with a console application.

Thank you in advance.
Posted
Updated 15-Nov-10 22:28pm
v2
Comments
Dalek Dave 16-Nov-10 4:28am    
Edited for Grammar and Syntax.
MCY 30-Dec-10 10:09am    
what is the renaming rule? keep only first 5 characters? remove after "_" character? remove last 9 characters?

Hi
I'm to lazy to write a console app in c# now but here is a powerful little freeware tool that I use from time to time.

Advanced Renamer: http://aren.hulubulu.net/[^]

Hope you come right ;)
 
Share this answer
 
It will recursively rename all the folders, folders in folders in folders.
static int _index = 0;
        public void RenameFolder(string sourceFolder)
        {
            try
            {
                string[] folders = Directory.GetDirectories(sourceFolder);
                foreach (string folder in folders)
                {
                    string name = Path.GetFileName(folder);
                    RenameFolder(folder);

                    if (name.ToLower().Contains("maced"))
                    {    
                        string path = Path.GetDirectoryName(folder);
                        Directory.Move(folder, path + @"\"+ name.Substring(0,5)+ _index.ToString());
                        _index++;
                    }
                }
            }
            catch (Exception p1)
            {
                MessageBox.Show("Directory is invalid!! " + p1.Message);
            }
        }


Please vote and Accept Answer if it Helped.
 
Share this answer
 
v13
Comments
Hiren solanki 10-Nov-10 4:11am    
@JFergulbops : you can improve the answer, you cant remove or clear answer
[no name] 10-Nov-10 8:39am    
i didnt remove the ANSWER - i removed a copy of the answer that was clearly written before his.
Dalek Dave 16-Nov-10 4:30am    
Now Now Children, Play Nicely!
[no name] 17-Nov-10 4:03am    
not bothered anymore cos of this kid. lol
#realJSOP 19-Nov-10 10:10am    
I deleted your comments. They were uncalled-for, outright offensive, and have no place here.
To rename a file you actually use the File.Move method of System.IO
to find the files named Maced****** you'l need to loop through your files/directories and check if the name == the name you wanna find.
for example,


Using System.IO;

  System.IO.File.Move(oldFileName, newFileName); //Renames Files
  
  //Putting All files into an array using the directory's path.

  string[] array1 = Directory.GetFiles(@"C:\Maced_Jul_2010");
   For(int i = 0; i < array1.length; i++)
   {
   if(array1[i].contains("Maced") {
      System.IO.File.Move(array1[i], indexof(0, 5)); //Renames Files
   }
}



Note:
i just noticed that this will not work - you're going to have the same file name for every file in there - and that will end up falling over.. i think you meant to have the word 'MACED' removed from the filename? in which case, same code except inside the if statement -

System.IO.File.Move(array1[i], indexof(5, array1[i].length - 5));
 
Share this answer
 
Comments
#realJSOP 19-Nov-10 14:18pm    
Do not respond to people that way. It's not acceptable. If you have a problem with someone, report them to the site admins.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900