C# Program For Renaming Files In Folder


Sometimes you may want to rename all the files in a directory,
e.g. If all the files are starting with some special character or string like "IMG_"

Manually this task will be very difficult. Here's a program in C# to rename all the files in a directory.

        static void Main(string[] args)
        {
            // directory_path - replace this with path of directory containing files to rename.
            DirectoryInfo dir = new DirectoryInfo("directory_path");
            FileInfo[] files = dir.GetFiles();           
            String fileName = "";

            foreach (FileInfo f in files)
            {
                fileName = f.Name;
                ChangeFileName(ref fileName);
                if (f.Name != fileName)
                    File.Move(f.FullName, f.DirectoryName + "\\" + fileName);
            }
        }

        //======================================================
        // code to change file name as per your requirement.
        //======================================================
        static void ChangeFileName(ref String fileName)
        {
            fileName = fileName.TrimStart();          
            fileName = fileName.Replace("IMG_", "");          
            fileName = fileName.TrimStart();
        }

To remove the digits in a file name you can use following Regular expression in ChangeFileName method
   
       fileName = Regex.Replace(fileName, @"\d", "");

While doing this you need to be careful. In case of file extensions like '.mp3' use of above line will remove 3 from 'mp3' as well.







aj1490

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard.

9 comments:

  1. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
    AWS TRAINING IN CHENNAI | BEST AWS TRAINING IN CHENNAI
    aws training in chennai"> aws training in chennai | Advanced AWS Training in Chennai
    AWS Training in Velachery, Chennai | Best AWS Training in Velachery, Chennai
    AWS Training in Tambaram | Best AWS Training in Tambaram

    ReplyDelete
  2. I and my friends were going through the nice, helpful tips from the blog then the sudden came up with an awful suspicion I never expressed respect to the website owner for those secrets.
    python interview questions and answers
    python tutorials
    python course institute in electronic city

    ReplyDelete

  3. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.

    AWS TRAINING IN BTM LAYOUT | AWS TRAINING IN BANGALORE
    AWS Training in Marathahalli | AWS Training in Bangalore

    ReplyDelete
  4. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    AWS Training in pune
    AWS Online Training
    AWS Training in Bangalore

    ReplyDelete