Rename file with "+" in filename

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
I am trying to rename files in a directory, my method works until the filename has a "+" sign in the filename.
is there a way to replace the "+" with a "_" and then rename the file...the code I wrote doesnt work. I commented out the part that checks if the txtnewName text box is blank.

if (txtDir.Text != "" && txtNameList.Text != "")
            {
                Load_NameList(txtNameList.Text);


                string[] ext = { "*.jpg", "*.jpeg", "*.bmp", "*.png" };
                DirectoryInfo dir = new DirectoryInfo(txtDir.Text);


                foreach (string item in ext)
                {
                    FileInfo[] files = dir.GetFiles(item);


                    if (files.Length > 0)
                    {
                        //if (txtNewName.Text != "")
                        //{


                            foreach (FileInfo file in files)
                            {


                                lblProgress.Text = "Progressing - ";
                                lblProgress.Refresh();
                                pos++;
                                if (file.Name == oldNameList[pos])
                                {
                                    string filename = dir.FullName + "\\" + file.Name.ToString();
                                    string newFileName = newNameList[pos] + ".jpg";
                                   
                                    lblProgress.Text = lblProgress.Text + " [" + Path.GetFileName(filename) + "]";
                                    lblProgress.Refresh();


                                    Thread.Sleep(100);
                                    try
                                    {
                                        //-rename file here! then move the new file to the directory-
                                    if (filename.Contains("+"))
                                    {
                                        string cleanFileName = String.Join("_", filename.Split('+'));
                                        lstFiles.Items.Add(renameDir + "\\" + newFileName);
                                        File.Move(filename, renameDir + "\\" + newFileName);
                                    }
                                    else
                                         
                                    {
                                        lstFiles.Items.Add(renameDir + "\\" + newFileName);
                                        File.Move(filename, renameDir + "\\" + newFileName);
                                    }




                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show("-ERROR-\r\n" + ex.Message, "File ReNamer",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    }
                                    //Update the progressbar to show the progress
                                    if (progressBar1.Value < 100)
                                    {
                                        progressBar1.Value = (pos * 100) / files.Length;
                                    }
                                    else
                                        progressBar1.Value = 100;
                                }


                            }
                            MessageBox.Show("Process Complete [" + files.Length + " ] files renamed", "File Renamer",
                             MessageBoxButtons.OK, MessageBoxIcon.Information);


                        //}
                        //else
                        //    MessageBox.Show("ReName Field Must Not Be Blank", "File ReNamer",
                        //        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }


                }


                lblProgress.Text = "Process Complete!";
                progressBar1.Value = 0;
               
            }
            else
                MessageBox.Show("Source Directory and/or Name List must not be blank!", "File ReNamer",
                   MessageBoxButtons.OK, MessageBoxIcon.Information);


thank you for any help

-InkedGFX
 
Given that a file name or path is a String, I can't see why String.Replace wouldn't work. It seems the obvious choice if you want to replace something in a String. You don't even have to bother with an `if` statement. You simply call it and, if there are no "+" symbols in the String then nothing will happen.
 
actually , what I am trying to do is rename a file..which this code does until it reaches a file with a "+" sign in the filename, then I get a "cannot find part of the path " exception.
looking at the code , do you see anything that would cause this error? correct me if I am wrong , but a "+" sign is allowed in a file name. am wondering why it would error on this.

thank you for your help

-InkedGFX
 
This code worked perfectly for me on files with or without "+" symbols in the name:
var rootFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var sourceFolder = new DirectoryInfo(Path.Combine(rootFolderPath, "Source"));
string[] extensions = {"*.txt", "*.bmp", "*.zip"};
IEnumerable<FileInfo> fileList = new FileInfo[] {};

fileList = extensions.Aggregate(fileList, (current, extension) => current.Concat(sourceFolder.GetFiles(extension)));

var destinationFolderPath = Path.Combine(rootFolderPath, "Destination");

foreach (var file in fileList)
{
    file.MoveTo(Path.Combine(destinationFolderPath, file.Name));
}
You can change the extensions and the paths as required and see if the same thing works for you.
 
thank you for the code...it worked perfectly for me as well.....but there is more...when I implimented your code and ran it..the program still broke at (what I thought) was the "+" sign but in fact the code was breaking on a forward slash in the file name..I didnt see this earlier....I caught it just now as I stepped thru the code line by line....si I replace the forward slash with an underscore and your code worked without error!

thank you for the help, I can always count on the c# forum to get me out of a jam!

-InkedGFX
 
Back
Top Bottom