vladislavn
vladislavn

Reputation: 388

Replace part of a filename in C#

I have a folder with .pdf files. In the names of most files I want to replace specific string with another string.

Here's what I've written.

  private void btnGetFiles_Click(object sender, EventArgs e)
    {
        string dir = tbGetFIles.Text;
        List<string> FileNames = new List<string>();

        DirectoryInfo DirInfo = new DirectoryInfo(dir);

        foreach (FileInfo File in DirInfo.GetFiles())
        {
            FileNames.Add(File.Name);       
        }

        lbFileNames.DataSource = FileNames;
    }

Here I extract all file names in List Box.

    private void btnReplace_Click(object sender, EventArgs e)
    {
        string strReplace = tbReplace.Text; // The existing string
        string strWith = tbWith.Text; // The new string

        string dir = tbGetFIles.Text;
        DirectoryInfo DirInfo = new DirectoryInfo(dir);
        FileInfo[] names = DirInfo.GetFiles();


        foreach (FileInfo f in names)
        {
            if(f.Name.Contains(strReplace))
            {
                f.Name.Replace(strReplace, strWith);
            }

        }

And here I want to do the replacing, but something is going wrong. What?

Upvotes: 5

Views: 22365

Answers (5)

Steve
Steve

Reputation: 216353

Replace return another string, it doesn't change the original string.
So you need to write

string newName = f.Name.Replace(strReplace, strWith); 

of course this doesn't change the name of the file on disk.
If that was your intention then you should look at

File.Move(f.Name, newName);

also keep in mind that File.Move will fail with an exception if the destination file exists.

See here for an example

Upvotes: 1

Mark
Mark

Reputation: 3283

When you call string.Replace this doesn't alter your existing string. Instead it is returning a new string.

You need to change your code to something like this:

if(f.Name.Contains(strReplace)) 
{ 
    string newFileName = f.Name.Replace(strReplace, strWith); 
    //and work here with your new string
}

Upvotes: 0

Charles Josephs
Charles Josephs

Reputation: 1535

f.Name is a read-only property. f.Name.Replace(..) simply returns a new string with the filename you want, but never actually changes the file.
I suggest something along the following, though I haven't tested it:

File.Move(f.Name, f.Name.Replace(strReplace, strWith));

Upvotes: 2

JaredPar
JaredPar

Reputation: 755457

It sounds like you want to change the name of the file on disk. If so then you need to use the File.Move API vs. changing the actual string which is the file name.

One other mistake you are making is the Replace call itself. A string in .Net is immutable and hence all of the mutating APIs like Replace return a new string vs. changing the old one in place. To see the change you need to assign the new value back to a variable

string newName = f.Name.Replace(strReplace, strWith);
File.Move(f.Name, newName);

Upvotes: 7

Adolfo Perez
Adolfo Perez

Reputation: 2874

At a first glance, seems like you're not reassigning the replaced string to your f.Name variable. Try this:

string NewFileName = f.Name.Replace(strReplace, strWith);
File.Copy(f.Name, NewFileName);
File.Delete(f.Name);

Upvotes: 0

Related Questions