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.

2
  • 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. Commented Jun 5, 2012 at 13:38
  • I dont need to call them something specific, it could just be a blank "". Commented Jun 5, 2012 at 13:38

3 Answers 3

10

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

Here's a sample method:

public static string SanitizeFileName ( string fileName, char replacementChar = '_' ) { HashSet<char> blocked = new(System.IO.Path.GetInvalidFileNameChars()); char[] output = fileName.ToCharArray(); for (int i = 0, ln = output.Length; i < ln; i++) { if (blocked.Contains(output[i])) { output[i] = replacementChar; } } return new String(output); } 

As pointed out by pixel, this would translate

my\\\file\\\\\\\\\\name 

into

my___file__________name 

... which may be unattractive.

If this function is something you might call in a batch or if you'd like the flexibility to replace consecutive invalid characters with a single replacement, you may want something like this:

public class FileNameSanitizer { // cache the blocked characters private HashSet<char> _blocked = new(System.IO.Path.GetInvalidFileNameChars()); /// <summary>Replace individual invalid characters one-for-one.</summary> public string Sanitize ( string fileName, char replacementChar = '_' ) { char[] output = fileName.ToCharArray(); for (int i = 0, ln = output.Length; i < ln; i++) { if (_blocked.Contains(output[i])) { output[i] = replacementChar; } } return new String(output); } /// <summary>Replace consecutive invalid characters with a single replacement character.</summary> public string SanitizeCondensed ( string fileName, char replacementChar = '_' ) { char[] output = fileName.ToCharArray(); char current; bool replaced = false; int i = 0, x = 0, ln = output.Length; for (; i < ln; i++) { current = output[i]; if (_blocked.Contains(current)) { if (!replaced) { output[x++] = replacementChar; } replaced = true; } else { output[x++] = current; replaced = false; } } return new String(output, 0, x); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

This is a lovely function even in 2021 cannon
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.

1 Comment

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, "");
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.