Click here to Skip to main content
15,893,668 members
Articles / Programming Languages / C#
Tip/Trick

Rename a directory or file name to lower case with C#

Rate me:
Please Sign up or sign in to vote.
4.85/5 (5 votes)
13 Apr 2012CPOL1 min read 42.1K   1   6
Change a directory or file name to lower case.

Introduction

As the title suggests, at first I thought it is an easy task. I mean to rename a file/directory from uppercase or any case to lower case form, how hard can that be?

Obviously I am wrong, it is not very difficult but not very trivial either. So here I am ...

Background 

The first approach I come up with (I think a lot of us will do):

C#
DirectoryInfo _dirInfo = new DirectoryInfo("Your Path");
_dirInfo.MoveTo( _dirInfo.Fullname.ToLower() ); 

Voila, I think I am done then, when I run the program, I get an Exception.

Image 1

The source and destination path must be different. That make sense, as I only change the directory name to its lower case form.

So to overcome this problem, I revise the MoveTo method of the DirectoryInfo class and come up with the following:

  1. If I can't change to lower case, how about I append something to the end?
  2. Use the newly created path from (1) and re-instantiate DirectoryInfo.
  3. Use (2) to call MoveTo again and obtain the result.
C#
static DirectoryInfo RenameDirectoryToLower(DirectoryInfo dirInfo)
{
  string strLower = dirInfo.FullName.ToLower();
  // (1)
  string strTmpName = dirInfo.FullName + "_temp";
  dirInfo.MoveTo(strTmpName);
  // (2)
  dirInfo = new DirectoryInfo(strTmpName);
  dirInfo.MoveTo(strLower);
  // (3)
  return new DirectoryInfo(strLower);
}

A similar approach can be used for renaming a file to its lower case. Since it is a very small program, I just attached the code below:

C#
//
// The full list for the Console application
//
class Program
{
    static void Main(string[] args)
    {
        var _directoryInfo = new DirectoryInfo(@"C:\testpath");
        ParseDirectory(_directoryInfo);
        Console.ReadLine();
    }
    static void ParseDirectory(DirectoryInfo directoryInfo)
    {
        foreach (var objInfo in directoryInfo.GetDirectories())
        {
            ParseDirectory(RenameDirectoryToLower(objInfo));
        }
        // Files
        foreach (var objFileInfo in directoryInfo.GetFiles())
        {
            RenameFileToLower(objFileInfo);
        }
    }
    static DirectoryInfo RenameDirectoryToLower(DirectoryInfo dirInfo)
    {
        string strLower = dirInfo.FullName.ToLower();
        string strTmpName = dirInfo.FullName + "_temp";
        dirInfo.MoveTo(strTmpName);
        dirInfo = new DirectoryInfo(strTmpName);
        dirInfo.MoveTo(strLower);
        return new DirectoryInfo(strLower);
    }
    static FileInfo RenameFileToLower(FileInfo fileInfo)
    {
        string strLower = fileInfo.FullName.ToLower();
        string strTmpName = fileInfo.FullName + "_temp";
        fileInfo.MoveTo(strTmpName);
        fileInfo = new FileInfo(strTmpName);
        fileInfo.MoveTo(strLower);
        return new FileInfo(strLower);
    }
}

That is it! I have managed to change both the directory and file name into lower case form.

Hope you enjoy this small tip. Happy programming.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Suggestiondoesn't check if temp file exists Pin
Ivan Ičin22-Apr-12 12:06
Ivan Ičin22-Apr-12 12:06 
QuestionWhat about using the command line or WinAPI Pin
perilbrain16-Apr-12 7:26
perilbrain16-Apr-12 7:26 
AnswerRe: What about using the command line or WinAPI Pin
D Y Chan17-Apr-12 3:30
D Y Chan17-Apr-12 3:30 
AnswerRe: What about using the command line or WinAPI Pin
Ivan Ičin22-Apr-12 12:18
Ivan Ičin22-Apr-12 12:18 
Questionjust cause i had time to burn. Pin
cechode13-Apr-12 14:39
cechode13-Apr-12 14:39 
not better just different. ( still felt like posting Smile | :) )

C#
static bool ConvertDirAndFilesToLower(string basedir)
{
    var moved = new Func<string, string, byte>((a, b) => { Directory.Move(a, b); Directory.Move(b, a.ToLower()); return 1; });
    var movef = new Func<string, string, byte>((a, b) => { File.Move(a, b); File.Move(b, a.ToLower()); return 1; });

    var these = Directory.GetFiles(basedir, "*.*", SearchOption.AllDirectories).Union(Directory.GetDirectories(basedir, "*.*", SearchOption.AllDirectories));
    var x = (from item in these
                let a = Path.Combine(Path.GetDirectoryName(item), Guid.NewGuid().ToString())
                select new { a = File.Exists(item) ? movef(item, a) : moved(item, a) });
    return x.Count().Equals(these.Count());
}

QuestionTime can give the same problem Pin
OriginalGriff13-Apr-12 4:54
mveOriginalGriff13-Apr-12 4:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.