Regex expression to replace invalid filename characters.

  • Thread starter Thread starter GregMa
  • Start date Start date
G

GregMa

Does anyone have a good regex expression to replace any invalid filename
characters in a string? Those characters are:
/, \, :, *, ?, ", <, >, |

I have it right now with string.replace for each individual character,
but would like to do it all in one step if possible. Here's what I have
now:

title = title.Replace("?", "");
title = title.Replace(":", "");
title = title.Replace("\\", "");
title = title.Replace("/", "");
title = title.Replace("*", "");
title = title.Replace("\"", "");
title = title.Replace("<", "");
title = title.Replace(">", "");
title = title.Replace("|", "");
newFileName += title + targetFile.Extension;
targetFile.MoveTo(newFileName);


Thanks!
Greg
 
Does anyone have a good regex expression to replace any invalid
filename characters in a string? Those characters are:
/, \, :, *, ?, ", <, >, |

I have it right now with string.replace for each individual
character, but would like to do it all in one step if possible.
Here's what I have now:

title = title.Replace("?", "");
title = title.Replace(":", "");
title = title.Replace("\\", "");
title = title.Replace("/", "");
title = title.Replace("*", "");
title = title.Replace("\"", "");
title = title.Replace("<", "");
title = title.Replace(">", "");
title = title.Replace("|", "");
newFileName += title + targetFile.Extension;
targetFile.MoveTo(newFileName);

Greg,

Something like this should work:

newFileName = Regex.Replace(title, @"[?:\/*""<>|]", "");

Hope this helps.

Chris.
 
Does anyone have a good regex expression to replace any invalid
filename characters in a string? Those characters are:
/, \, :, *, ?, ", <, >, |
Greg,

Something like this should work:

newFileName = Regex.Replace(title, @"[?:\/*""<>|]", "");

Hope this helps.

Worked like a charm, Thanks! I had an extra ^ at the beginning, that's
why my attempt didn't work. Many thanks!

Greg
 
Something like this should work:

newFileName = Regex.Replace(title, @"[?:\/*""<>|]", "");

Or possibly use the String.Trim function instead which can trim off a
list of chars. ... Regex may be a bit of overkill in this instance.
 
I'd also make sure you don't miss anything by using the InvalidPathChars
property of
the Path class. Some strange characters can find their way into names like
backspaces,
nulls, etc...


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers


@my.email> > said:
Something like this should work:

newFileName = Regex.Replace(title, @"[?:\/*""<>|]", "");

Or possibly use the String.Trim function instead which can trim off a
list of chars. ... Regex may be a bit of overkill in this instance.
 
I'd also make sure you don't miss anything by using the InvalidPathChars
property of
the Path class. Some strange characters can find their way into names like
backspaces,
nulls, etc...

Awesome, didn't even know that existed! I'll have to incorporate it.

Thanks!
Greg
 
Back
Top