I have a directory with lots of folders, sub-folder and all with files in them. The idea of my project is to recurse through the entire directory, gather up all the names of the files and replace invalid characters (invalid for a SharePoint migration).
However, I'm completely unfamiliar with Regular Expressions. The characters i need to get rid in filenames are: ~, #, %, &, *, { } , \, /, :, <>, ?, -, |
and ""
I want to replace these characters with a blank space. I was hoping to use a string.replace()
method to look through all these file names and do the replacement.
So far, the only code I've gotten to is the recursion. I was thinking of the recursion scanning the drive, fetching the names of these files and putting them in a List<string>
.
Can anybody help me with how to find/replace invalid chars with RegEx with those specific characters?
They use a regular expression pattern to define all or part of the text that is to replace matched text in the input string. The replacement pattern can consist of one or more substitutions along with literal characters. Replacement patterns are provided to overloads of the Regex.
The Oracle REGEXP_REPLACE() function replaces a sequence of characters that matches a regular expression pattern with another string. The REGEXP_REPLACE() function is an advanced version of the REPLACE() function.
string pattern = "[\\~#%&*{}/:<>?|\"-]"; string replacement = " "; Regex regEx = new Regex(pattern); string sanitized = Regex.Replace(regEx.Replace(input, replacement), @"\s+", " ");
This will replace runs of whitespace with a single space as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With