2

This code will rename all the files names :

static private void RenameFiles()
        {
            images = Directory.GetFiles(sf, "*.gif");
            foreach (string name in images)
            {
                Console.WriteLine("Working on current file: " + name);
                //string newName = name.Replace("radar_temp_directory", String.Empty);
                //string newName = Path.Combine(Path.GetFullPath(name),Path.GetFileName(name).Replace("radar_temp_directory", String.Empty));
                string newName = Path.Combine(Path.GetDirectoryName(name), Path.GetFileName(name).Replace("radar_temp_directory", String.Empty));
                File.Move(name, newName);
            }
        }

But now i want to make another method that will change each file extention from Gif to gif. Or if it will be "gIf" so all the extentions of the files will be .gIf But now i want to change it to gif. So for example if i have a file radar000005.Gif it will be radar000005.gif

user3596190
  • 257
  • 1
  • 4
  • 12

1 Answers1

2

Use the following method

Path.ChangeExtension(string path, string newExtension);

The path would be the String which would be pointing to the location of the file, second parameter would be a new String Extension that would be appeneded to the filename.

Example code provided at MSDN

string fileName = @"C:\mydir\myfile.com.extension";
string result = "";
Path.ChangeExtension(fileName, "string");

But remember, it won't save the file. You'll have to save the file as a new file. This would only change the fileExtension once at the run-time.

For more on that: http://msdn.microsoft.com/en-us/library/system.io.path.changeextension.aspx

Community
  • 1
  • 1
Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
  • 1
    That's a useful helper for finding the new filename. But it doesn't touch the file at all. Some more code will be needed. – Torino May 10 '14 at 11:09
  • 1
    Then you must save new file physically. `File.Move(path, Path.ChangeExtension(path, ".gif"));` – Farhad Jabiyev May 10 '14 at 11:09