none
Replace fine name contains specific string pattern with other pattern? RRS feed

  • Question

  • Dear All,

    How to use regex to replace the string "V-A.B.C.D" to "V-A-B-C-D" for all exe file names?

    for example, there are exe files which all have string pattern "V-A.B.C.D",

    want to change to "V-A-B-C-D"

    the '.' is replaced by '-', how to do it?


    Deubg-V-0.8.1.0.exe

    Release-V-0.8.1.0.exe

    Full-Release-V-1.2.0.1.exe

    Full-Debug-V-1.2.0.1.exe

    Thanks  and Best regards,

    E-John





    • Edited by E-John Friday, April 19, 2019 8:29 AM
    Friday, April 19, 2019 8:26 AM

Answers

  • Hi

    Thank you for posting here.

    Based on your description, you want to use regex to replace string from "V-A.B.C.D" to "V-A-B-C-D" for all exe file names.

    You could try the following code.

                string file1 = "Deubg-V-0.8.1.0.exe";
                string file2 = "Release-V-0.8.1.0.exe";
                string file3 = "Full-Release-V-1.2.0.1.exe";
                string file4 = "Full-Debug-V-1.2.0.1.exe";
                string[] file = { file1, file2, file3, file4 };
                Regex re = new Regex(@"\.");
                foreach (var item in file)
                {
                    Console.WriteLine(re.Replace(item, "-", 3));
                }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Tim Roberts Friday, April 19, 2019 11:20 PM
    • Marked as answer by E-John Monday, April 22, 2019 2:13 AM
    Friday, April 19, 2019 9:11 AM
    Moderator
  • Or you try the following:
    List<string> lst = new List<string>
    {
    	"Deubg-V-0.8.1.0.exe",
    	"Release-V-0.8.1.0.exe",
    	"Full-Release-V-1.2.0.1.exe",
    	"Full-Debug-V-1.2.0.1.exe"
    };
    Regex regex = new Regex("\\.");
    foreach (var item in lst)
    {
    	FileInfo fi = new FileInfo(item);
    	string newName = regex.Replace(Path.GetFileNameWithoutExtension(fi.Name), "-") + fi.Extension;
    	Console.WriteLine(newName);
    }
    
    wizend
    • Marked as answer by E-John Monday, April 22, 2019 2:13 AM
    Friday, April 19, 2019 9:58 AM
  • Try this too:

    string example = "Full-Debug-V-1.2.0.1.exe";
    string result = Regex.Replace( example, @"\.(?=.*\.exe)", "-", RegexOptions.IgnoreCase );



    • Edited by Viorel_MVP Friday, April 19, 2019 11:18 AM
    • Marked as answer by E-John Monday, April 22, 2019 2:13 AM
    Friday, April 19, 2019 11:17 AM
  • Dear All,

    It is OK.

    Thanks for your helps.

    Best regards,

    E-John

    using System;
    using System.IO;
    using System.Text.RegularExpressions;
    
    namespace FileRename
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Get the current directory.
                string path = Directory.GetCurrentDirectory();
                Console.WriteLine("The current directory is {0}", path);
    
                // find all *.exe files in current directory
                DirectoryInfo d = new DirectoryInfo(path);
                FileInfo[] files = d.GetFiles("*.exe");
    
                // rename by regx
                // replace '.' by '-'
                Regex regVersionPattern = new Regex(@"(V-\d\.\d\.\d\.\d)");
    
                foreach (FileInfo file in files)
                {
                    if (regVersionPattern.IsMatch(file.Name) == true)
                    {
                        string newFileName;
    
                        Regex regDot = new Regex(@"\.");
                        // method 1
                        //newFileName = regDot.Replace(file.Name, "-", 3);
                        // method 2
                        //newFileName = regDot.Replace(Path.GetFileNameWithoutExtension(file.Name), "-") + file.Extension;
                        // method 3
                        newFileName = Regex.Replace(file.Name, @"\.(?=.*\.exe)", "-", RegexOptions.IgnoreCase);
    
                        if (newFileName != null)
                        {
                            // check file exist
                            if (File.Exists(newFileName) == true)
                            {
                                // delete current file, and rename the old file name to new one
                                File.Delete(newFileName);
                                Console.WriteLine("Delete file name : " + newFileName);
                            }
                            File.Move(file.Name, newFileName);
    
                            Console.WriteLine("change old file name : " + file.Name + " to new file name : " + newFileName);
                        }
                    }
                }
    
                Console.ReadLine();
            }
        }
    }
    

    • Edited by E-John Monday, April 22, 2019 2:22 AM
    • Marked as answer by E-John Monday, April 22, 2019 2:34 AM
    Monday, April 22, 2019 2:12 AM

All replies

  • Hi

    Thank you for posting here.

    Based on your description, you want to use regex to replace string from "V-A.B.C.D" to "V-A-B-C-D" for all exe file names.

    You could try the following code.

                string file1 = "Deubg-V-0.8.1.0.exe";
                string file2 = "Release-V-0.8.1.0.exe";
                string file3 = "Full-Release-V-1.2.0.1.exe";
                string file4 = "Full-Debug-V-1.2.0.1.exe";
                string[] file = { file1, file2, file3, file4 };
                Regex re = new Regex(@"\.");
                foreach (var item in file)
                {
                    Console.WriteLine(re.Replace(item, "-", 3));
                }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Tim Roberts Friday, April 19, 2019 11:20 PM
    • Marked as answer by E-John Monday, April 22, 2019 2:13 AM
    Friday, April 19, 2019 9:11 AM
    Moderator
  • Or you try the following:
    List<string> lst = new List<string>
    {
    	"Deubg-V-0.8.1.0.exe",
    	"Release-V-0.8.1.0.exe",
    	"Full-Release-V-1.2.0.1.exe",
    	"Full-Debug-V-1.2.0.1.exe"
    };
    Regex regex = new Regex("\\.");
    foreach (var item in lst)
    {
    	FileInfo fi = new FileInfo(item);
    	string newName = regex.Replace(Path.GetFileNameWithoutExtension(fi.Name), "-") + fi.Extension;
    	Console.WriteLine(newName);
    }
    
    wizend
    • Marked as answer by E-John Monday, April 22, 2019 2:13 AM
    Friday, April 19, 2019 9:58 AM
  • Try this too:

    string example = "Full-Debug-V-1.2.0.1.exe";
    string result = Regex.Replace( example, @"\.(?=.*\.exe)", "-", RegexOptions.IgnoreCase );



    • Edited by Viorel_MVP Friday, April 19, 2019 11:18 AM
    • Marked as answer by E-John Monday, April 22, 2019 2:13 AM
    Friday, April 19, 2019 11:17 AM
  • Dear All,

    It is OK.

    Thanks for your helps.

    Best regards,

    E-John

    using System;
    using System.IO;
    using System.Text.RegularExpressions;
    
    namespace FileRename
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Get the current directory.
                string path = Directory.GetCurrentDirectory();
                Console.WriteLine("The current directory is {0}", path);
    
                // find all *.exe files in current directory
                DirectoryInfo d = new DirectoryInfo(path);
                FileInfo[] files = d.GetFiles("*.exe");
    
                // rename by regx
                // replace '.' by '-'
                Regex regVersionPattern = new Regex(@"(V-\d\.\d\.\d\.\d)");
    
                foreach (FileInfo file in files)
                {
                    if (regVersionPattern.IsMatch(file.Name) == true)
                    {
                        string newFileName;
    
                        Regex regDot = new Regex(@"\.");
                        // method 1
                        //newFileName = regDot.Replace(file.Name, "-", 3);
                        // method 2
                        //newFileName = regDot.Replace(Path.GetFileNameWithoutExtension(file.Name), "-") + file.Extension;
                        // method 3
                        newFileName = Regex.Replace(file.Name, @"\.(?=.*\.exe)", "-", RegexOptions.IgnoreCase);
    
                        if (newFileName != null)
                        {
                            // check file exist
                            if (File.Exists(newFileName) == true)
                            {
                                // delete current file, and rename the old file name to new one
                                File.Delete(newFileName);
                                Console.WriteLine("Delete file name : " + newFileName);
                            }
                            File.Move(file.Name, newFileName);
    
                            Console.WriteLine("change old file name : " + file.Name + " to new file name : " + newFileName);
                        }
                    }
                }
    
                Console.ReadLine();
            }
        }
    }
    

    • Edited by E-John Monday, April 22, 2019 2:22 AM
    • Marked as answer by E-John Monday, April 22, 2019 2:34 AM
    Monday, April 22, 2019 2:12 AM