C# file renaming help

TheDude05

Limp Gawd
Joined
Jan 27, 2005
Messages
393
I cant figure out what I'm doing wrong. My replace statements are not working. The string "name" is never changed.

Code:
                if (folderBrowser.ShowDialog() == DialogResult.OK) {
                    String path = folderBrowser.SelectedPath;
                    DirectoryInfo direc = new DirectoryInfo(path);
                    FileInfo[] files = direc.GetFiles();

                    for (int i = 0; i < files.Length; i++) {
                        FileInfo file = files[i];
                        String name = file.Name;

                        // remove the crap
                        name.Replace("(Live)", "");
                        name.Replace("(Live In Stl)", "");
                        name.Replace("(Warehouse 5)", "");

                        file.MoveTo(file.DirectoryName + "\\" + name);
                    }
                    MessageBox.Show("Done!");
                }

An example file.Name would be Dave Matthews Band - Crash Into Me (Live) (Live In Stl) (Warehouse 5).mp3

I copied that straight out of the debugger. Any ideas?
 
TheDude05 said:
I cant figure out what I'm doing wrong. My replace statements are not working. The string "name" is never changed.

Code:
                if (folderBrowser.ShowDialog() == DialogResult.OK) {
                    String path = folderBrowser.SelectedPath;
                    DirectoryInfo direc = new DirectoryInfo(path);
                    FileInfo[] files = direc.GetFiles();

                    for (int i = 0; i < files.Length; i++) {
                        FileInfo file = files[i];
                        String name = file.Name;

                        // remove the crap
                        name.Replace("(Live)", "");
                        name.Replace("(Live In Stl)", "");
                        name.Replace("(Warehouse 5)", "");

                        file.MoveTo(file.DirectoryName + "\\" + name);
                    }
                    MessageBox.Show("Done!");
                }

An example file.Name would be Dave Matthews Band - Crash Into Me (Live) (Live In Stl) (Warehouse 5).mp3

I copied that straight out of the debugger. Any ideas?

String.Replace(...) returns a string that is the result of the replacement. So you need to do
Code:
name = name.Replace("(Live)", "");
 
Back
Top