Here are several tips that might help write better/cleaner code. Note that my tips or help might not be perfect but it gives you an idea on how to write better code.
Using variable names like k
and n
are very bad practice. Not only will other people not understand your code, if you haven't looked at your code for a while, you might have forgotten what each variable does/stands for. Give your variables a meaningful name. So after applying this, your code looks like this:
string currentName = fi.Name.Replace(fi.Extension, "");
int nameAsNumber = Int32.Parse(currentName);
string newName = nameAsNumber.ToString("D4");
newName = newName + fi.Extension;
string targetFile = Path.Combine(fi.DirectoryName, newName);
File.Move(fi.FullName, targetFile );
Storing operations/calculations on variables in new variables can be handy and even necessary in certain situations. But in this situation where you take a string, parse it to an int and make it a string again, I suggest you do this in one line. You can even add the extension in the same line:
string currentName = fi.Name.Replace(fi.Extension, "");
string newName = int.Parse(currentName).ToString("D4") + fi.Extension;
This does not mean you have to write all your code in one line, this an example of doing too much in one line:
string targetFile = Path.Combine(fi.DirectoryName, int.Parse(Path.GetFileNameWithoutExtension(fi.FullName)).ToString("D4") + fi.Extension);
The above line does exactly the same but it's not easy to read and/or maintain.
- Re-inventing the wheel (part 1)
Why create methods/functions/lines of code that do something that's already been made for you. If you want to get the filename without the extension, use the Path.GetFileNameWithoutExtension() method.
string currentName = Path.GetFileNameWithoutExtension(fi.FullName);
- Re-inventing the wheel (part 2)
Remember, the .NET framework contains classes and methods for almost anything. And this also goes for adding zeros (or other characters) to a string when they do not yet meet a desired length. This is done with the Pad()
method, and in your case the String.PadLeft(Int32, Char) method. You simply provide the desired length and the character that will be put in place. Your code will become:
string currentName = Path.GetFileNameWithoutExtension(fi.FullName);
string newName = currentName.PadLeft(4, '0') + fi.Extension;
Your complete inside-loop code will now look like:
string currentName = Path.GetFileNameWithoutExtension(fi.FullName);
string newName = currentName .PadLeft(4, '0') + fi.Extension;
string targetFile = Path.Combine(fi.DirectoryName, newName);
File.Move(fi.FullName, targetFile);
I hope this helps! :)
Pad
method available to strings, rather than converting the string to an integer and formatting it back into a string. \$\endgroup\$