13

I'm working on a program that reads files and saves pieces of them according to their column's title. Some of those titles have illegal characters for file names, so i've written this piece of code to handle those issues.

string headerfile = saveDir + "\\" + tVS.Nodes[r].Text.Replace("\"", "").Replace
              ("/","").Replace(":"," -").Replace(">","(Greater Than)") + ".csv";

Is there a nicer way of doing this where i don't have 4 .Replace()? or is there some sort of built in illegal character remover i don't know of?

Thanks!

EDIT: It does not need to replace the characters with anything specific. A blank space is sufficient.

Axxelsian
  • 787
  • 3
  • 9
  • 25
  • 1
    If you were just removing illegal chars you could simplify it a lot, but given that you're replacing most of them with something else, you're limiting a lot of the options. – Servy Jun 05 '12 at 13:38
  • I dont need to call them something specific, it could just be a blank "". – Axxelsian Jun 05 '12 at 13:38

4 Answers4

37

Regular expressions are generally a good way to do that, but not when you're replacing every character with something different. You might consider replacing them all with the same thing, and just using System.IO.Path.GetInvalidFileNameChars().

string filename = tVS.Nodes[r].Text;

foreach(char c in System.IO.Path.GetInvalidFileNameChars()) {
    filename = filename.Replace(c, '_');
}
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • Problem with this is that if file has more consecutive invalid characters, then multiple consecutive "_" will be inserted making for example a file "my\\\file\\\\\\\\\\name" into "my___file__________name". That is fine but a better solution would be to have it end up as "my_file_name". – pixel Nov 18 '16 at 17:58
9

System.IO.Path.GetInvalidFileNameChars() has all the invalid characters.

Here's a sample method:

public static string SanitizeFileName(string fileName, char replacementChar = '_')
{
    var blackList = new HashSet<char>(System.IO.Path.GetInvalidFileNameChars());
    var output = fileName.ToCharArray();
    for (int i = 0, ln = output.Length; i < ln; i++)
    {
        if (blackList.Contains(output[i]))
        {
            output[i] = replacementChar;
        }
    }
    return new String(output);
}
canon
  • 40,609
  • 10
  • 73
  • 97
2

Have a look at Regex.Replace here, it will do everything you desire when it comes to stripping out characters individually. Selective replacement of other strings may be trickier.

Jeff Watkins
  • 6,343
  • 16
  • 19
2
string charsToRemove = @"\/:";TODO complete this list

string filename;

string pattern = string.format("[{0}]", Regex.Escape(charsToRemove));
Regex.Replace(filename, pattern, "");

If you just want to remove illegal chars, rather than replacing them with something else you can use this.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • it will not work for lot of people. in last line you should put Regex modified value to some string variable, i.e. `filename = Regex.Replace(filename, pattern, "");` – Maciej S. Nov 10 '17 at 11:03