Introduction
Today I got scenario where I have to rename 100+ files with their names starting with XYZ_ and my task is to replace that XYZ_ with ABC_. But I don't have that luxury to spend around 1-2 hours on renaming the files. So I find a way to rename the files through codding.
I made an application that is capable of replacing some text with your desired text.
Find the patten in the below image which thing I want to replace.
Files wth a prifix of XYZ_ |
This is a folder present in "E:\Test\Files" directory of my computer. As you can see the files present there in the above image you can find that all the files have a prefix of XYZ_ that needs to be changed to ABC_.
So if I go to change this by selecting all for renaming then it does not work it renames all the files with what I typed.
I tried to find out a standard solutions from the web but did not find one suitable and efficient one. So I developed a windows application to do the job. And here it is.
Find the below image how I want the output.
When "XYZ_" replaced with "ABC_" |
In order to do this I choose Windows application where I took two Text Boxes, those take the input from the user for replacement work. Find the below image and then I will explain what control does what ?
The Form with all the controls |
* Find Text will take the text which you want to replace.
* Replace Text will take the text which will be replaced.
* Select File Type will give you to the option to select the type of file you want to rename.
* Rename Files button will open a folder selection dialog to select the respective folder you want.
Folder selection Dialog |
OK, now we are done with the design part. lets move to the codding part. Once you click on the Rename Files button it will invoke the btnRename_Click event.
private void btnRename_Click(object sender, EventArgs e) { { // // This event handler was created by double-clicking the window in the designer. // It runs on the program's startup routine. // DialogResult result = folderBrowserDialog1.ShowDialog(); if (result == DialogResult.OK) { int count = 0; lblError.Text = string.Empty; // This will get the folder you have selected. DirectoryInfo d = new DirectoryInfo(folderBrowserDialog1.SelectedPath); // Check if the Dropdown has some value or not. if (cmbFileType.SelectedText != "Select") { try { // get only those files with an extension of the selected one. FileInfo[] infos = d.GetFiles("*" + cmbFileType.SelectedItem.ToString()); // Make a loop to replace and rename the files. foreach (FileInfo f in infos) { // Do the renaming here File.Move(f.FullName, Path.Combine(f.DirectoryName, txtReplaceText.Text.Trim() + f.Name.Replace(txtFindText.Text.Trim(),""))); count++; } // Show alert if no file with the selected extension is present. if (infos.Count() == 0) { lblError.Text = "Destination folder has no " + " ' " + cmbFileType.SelectedItem.ToString() + " ' files."; } //Show success alert. else if (count == infos.Count()) { lblError.Text = "All ' " + cmbFileType.SelectedItem.ToString() + " ' files in '" + d + " ' folder whose name contains a text of ' " + txtFindText.Text.Trim() + " ' has been replaced with ' " + txtReplaceText.Text.Trim() + " '."; } } catch (Exception ex) { lblError.Text = "Some error occured"; } } else { lblError.Text = "Please select a file type first."; } } } }
What I am doing in the above code ???
- In the above method I am getting the folder name from the the folder selection dialog and creating a DirectoryInfo .
- Selecting all the files with the extension selected from the combo box.
- Making a loop to replace the intended text with the desired text.
- Then show appropriate alert message to the user.
Happy Coding...
2 comments:
Lets say some of the files are open in another application. I think they will not get renamed. How can I be sure that the file is not open and rename that file?
Yeah it will be able to rename the opened file too.... I have checked it :)
Post a Comment