none
How to rename multipal of folders name in C# wfa using button ? RRS feed

  • Question

  • Hi,

    I want to rename multiple of folder name using a word from every folder name :-
    e.g.
    ORG Folders                                                          Need to Change
    D:/Joy/27344_pro_12123                                       D:/Joy/1100
    D:/Joy/33304_pro_11134                                       D:/Joy/1200
    D:/Joy/11366_pro_10154_com                               D:/Joy/1300
    D:/Joy/11404_pro_30300_com_2567_diag             D:/Joy/1400
    D:/Joy/12388_pro_10101                                        D:/Joy/1500

    Please suggest a C# WFA code for the same.
    Sunday, December 1, 2019 1:30 PM

All replies

  • Hi Enhance Technology,

    Thank you for posting here.

    For your question, you want to rename multiple of folder name.

    But I don't quite understand the example you gave, what is the relationship between ORG Folders and Need to Change?

    And what does "using a word from every folder name" mean?

    Looking forward to your reply.

    Best Regards,

    Timon


    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.

    Monday, December 2, 2019 5:12 AM
  • But I don't quite understand the example you gave, what is the relationship between ORG Folders and Need to Change?

    ORG Folders     already i have directory or folders

    Need to Change       this is a example in which type i want rename the folders

    And what does "using a word from every folder name" mean?

    1st word is fixed in folders name rest can be change so for folder name identification i need to use 1st word for rename.

     

    Monday, December 2, 2019 8:32 AM
  • Hi Enhance Technology,

    Thanks for your feedback..

    I wrote a code example to modify folder names in batches, you can refer to it.

    If you want to select a folder and change the names of all the folders in this folder, you can use this code.

    private void Button1_Click(object sender, EventArgs e)
            {
                using (CommonOpenFileDialog  openFile = new CommonOpenFileDialog())
                {
                    openFile.IsFolderPicker = true;
                    openFile.Multiselect = true;
    
                    if (openFile.ShowDialog() == CommonFileDialogResult.Ok)
                    {
                        IEnumerable<string> files = openFile.FileNames;
                        string text = "";
                        foreach (var item in files)
                        {
                            text += item;
                        }
                        textBox1.Text = text;
                    }
                }
            }
    
            private void Button2_Click(object sender, EventArgs e)
            {
                string path = textBox1.Text;
                string[] directorys = Directory.GetDirectories(path);
                for (int i = 0; i < directorys.Length; i++)
                {
                    Directory.Move(directorys[i], textBox1.Text +"\\"+ textBox2.Text + i);
                }
                MessageBox.Show("Success");
            }

    If you select the folders in batches, and then modify the names of the selected folders, you can modify the code of Button2_Click() to the following.

    private void Button2_Click(object sender, EventArgs e)
            {
                string[] directorys = textBox1.Text.Split(new char[] { ';' });
                
                for (int i = 0; i < directorys.Length-1; i++)
                {
                    string str =  directorys[i].Substring(directorys[i].LastIndexOf("\\"));
                    String newpath = directorys[i].Replace(str, "") + "\\"+textBox2.Text + i;
                    Directory.Move(directorys[i], newpath);
                }
                MessageBox.Show("Success.");
            }

    And then modify “text += item“ in Button1_Click() to “text += item+";".

    Hope this could be helpful.

    Best Regards,

    Timon


    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.



    Tuesday, December 3, 2019 9:02 AM