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.
thank you for any help
-InkedGFX
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