Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / regular-expression

Remove Illegal Chars from a File and/or Folder Name

4.80/5 (2 votes)
21 Feb 2013CPOL 16.4K  
A simple regex that replaces ALL the illegal filename or path chars

Introduction

When you wish to check a new name for a folder or file, you perform a validate function. This function may vary according to the implementer.

Most of us will just write down a simple function to remove the illegal chars such as < > : " / \ | ? * (on Windows) like replacing those chars with "" e.g.:

C#
Str = Str.Replace("<", "");  

However, you are missing a lot of uncommon chars that are not allowed, (many of which you don't have on your keyboard [even in China!]) and (though somewhat unlikely) those may even vary with time. The idea is to use the .NET-provided illegal chars of the Path class, and to replace them with Regex.

Code

Here goes:

C#
public static string FixedStr(string Str)
{
    // This regex will include illegal chars you never dreamed of
    string illegalCharsPattern = new string(Path.GetInvalidFileNameChars()) 
         + new string(Path.GetInvalidPathChars());
    Regex r = new Regex(string.Format("[{0}]", Regex.Escape(illegalCharsPattern))); 
    return r.Replace(Str, ""); 
}  

NOTE: This is not for old systems with SFN 8.3 file name requirement (like FAT), for file names and some special UNIX systems (though UNIX usually allows most chars).

License

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