Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Experts,

Please, help me resolve an issue in Visual Studio #C.
I am creating utility for renaming of names of multiple files.
Main idea = user opens the folder with files and selects required files for renaming in listbox.
Selection can be performed via searching by special words in names of items (masks "MD").
I have completed work at filtrating names of files and changing their names in listbox. But my problem - I cannot copy name of items and save these as names of files in folder.
Have you ever been observed such problem?
Please help me in solution.

What I have tried:

C#
public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {

                listBox1.Items.Clear();

                string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
                string[] dirs = Directory.GetDirectories(folderBrowserDialog1.SelectedPath);
                SelectedPathText.Text = folderBrowserDialog1.SelectedPath;

                foreach (string file in files)
                {
                    listBox1.Items.Add(Path.GetFileName(file));
                }
                foreach (string dir in dirs)
                {
                    listBox1.Items.Add(Path.GetFileName(dir));
                    SelectedPathText.Text = Path.GetFileName(dir);
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            DialogResult result = MessageBox.Show("Are you sure ?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                timer1.Enabled = true;

                try
                {
                                
                    if (checkBox1.Checked)
                    {
                        int SelectedItem = listBox1.SelectedIndex;
                        listBox1.Items.RemoveAt(SelectedItem);
                        listBox1.Items.Insert(SelectedItem, (textBox_rig.Text + ("_" + (textBox_field.Text + ("_" + (textBox_pad.Text + ("_" + (textBox_well.Text + ("_" + (comboBox1.Text  +("_"   +("_"+ (comboBox2.Text + ("_" + ("MD"+ ("_"+ (textBox_start.Text + ("-" + (textBox_end.Text + (".PDF"))))))))))))))))))));
                        
                    }

                }
                catch
                {
                    Application.DoEvents();
                }

               
            }
        }
Posted
Updated 19-Mar-19 7:51am
v2
Comments
CHill60 19-Mar-19 13:29pm    
There is no code here that is trying to rename any files. What happened when you trying to save the file in the folder?
Also - you have "swallowed" any errors that might be thrown. Get rid of that try-catch or at the very least log or show the error message.

You should use sensible names for controls etc - we have no idea what checkBox1 indicates, nor what button1 is for. Why are you using a timer?

1 solution

The "recommended" approach is to copy while renaming to a new (empty) folder.

When certain all is ok, then "merge". See how the "pros" do it:

IrfanView - Official Homepage - One of the Most Popular Viewers Worldwide[^]
 
Share this answer
 

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