3

I am rename the files in a folder that contain a "." with a " ".
Expected result... Before: "I.am.testing.txt", After: "I am testing.txt"
Actual result... Before: "I.am.testing.txt", After: "I am testing txt"

The problem is that it also removes the "." for the file extension which is obviously a problem..

string folderPath = new DirectoryInfo(textBoxDir.Text).FullName; DirectoryInfo d = new DirectoryInfo(folderPath); FileInfo[] filesDot = d.GetFiles("*.*"); foreach (FileInfo fi in filesDot) { File.Move(fi.FullName, Path.Combine(fi.Directory.ToString(), fi.Name.Replace(".", " "))); } 

1 Answer 1

6

You can just use Path.GetFileNameWithoutExtension to only get the name of the file, and then just append the original extension name to the end.

File.Move(fi.FullName, Path.Combine(fi.Directory.ToString(), Path.GetFileNameWithoutExtension(fi.Name).Replace(".", " ") + fi.Extension)); 
Sign up to request clarification or add additional context in comments.

5 Comments

If you do this, you'll also need add + Path.GetFileExtension(fi.Name) as part of the 2nd parameter to Path.Combine
If you look at my edit from a couple of minutes ago, you can see that I added that. I realized after I posted it. Thank you.
Sorry I'm on the app on my cell phone and hadn't refreshed yet.
No problem @iCode I've done the same thing more than I'd like to mention.
@Conor perfect, I'm glad we could get it figured out for you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.