-2

I've got a long set of replace statements to get rid of the following characters in filenames (from the C# source code):

RealInvalidPathChars = { '\"', '<', '>', '|', '\0', (Char)1, (Char)2, (Char)3, (Char)4, (Char)5,
    (Char)6, (Char)7, (Char)8, (Char)9, (Char)10, (Char)11, (Char)12, (Char)13, (Char)14, (Char)15,
    (Char)16, (Char)17, (Char)18, (Char)19, (Char)20, (Char)21, (Char)22, (Char)23, (Char)24, (Char)25,
    (Char)26, (Char)27, (Char)28, (Char)29, (Char)30, (Char)31 };

The "dumb" JS looks like this:

var r = s.replace('\u0001','_').replace('\u0002', '_') ....etc...;

Is there a JS way to iteratively replace these special characters in a loop? Or is there a regex can be used to deal with the '\u00xx' characters (I've never seen this done)? The first character in this C#-defined range is '\u0001' and the last is '\u001f'.

TyCobb
  • 8,909
  • 1
  • 33
  • 53
JacobIRR
  • 8,545
  • 8
  • 39
  • 68
  • Use a character class: `[all_of_your_characters_here]+`. This needs to be replaced by e.g. `_`. – Jan Dec 15 '17 at 22:29
  • Why are you doing this? Removing invalid characters [won't create valid filenames](https://stackoverflow.com/a/1976050/22437). – Dour High Arch Dec 15 '17 at 22:50
  • I'm just trying to prevent the raising of System.ArgumentException "Illegal characters in path." – JacobIRR Dec 15 '17 at 22:54
  • If that's all you are trying to do then do it on the C# side and remove based on what is returned from `Path.GetInvalidFileNameChars()` https://msdn.microsoft.com/en-us/library/system.io.path.getinvalidfilenamechars(v=vs.110).aspx – TyCobb Dec 15 '17 at 23:10
  • @JacoblRR What is the meaning of "JS way"? – Markus Dec 15 '17 at 23:13
  • @Markus - I want to do this with Javascript not C#. I'm asking how to do this with javascript – JacobIRR Dec 15 '17 at 23:13
  • 1
    In the future, please be more explicit with your question and tags. I have removed the C# tag as this has nothing to do with C# since it is looking for a solution in JavaScript. – TyCobb Dec 15 '17 at 23:17

1 Answers1

1

JavaScript: (I did not check the syntax but something like this should work for JS)

for (var i = 0; i < RealInvalidPathChars.length ; i++) {
    s = s.replace(RealInvalidPathChars[i], '_');
}

C#: I use this code:

    public static string GetValidFileName(string fileName, char replacementCharForInvalidChars = '_', int maxLength = 120, string defaultExtention = "", string defaultFileNameIfFilenameIsInvalid = "_") {
        if (string.IsNullOrWhiteSpace(fileName)) return defaultFileNameIfFilenameIsInvalid;
        var ret = fileName.Trim();
        foreach (var invalidFileNameChar in Path.GetInvalidFileNameChars()) {
            ret = ret.Replace(invalidFileNameChar, replacementCharForInvalidChars);
        }
        var newfileName = Path.GetFileNameWithoutExtension(ret);
        if (string.IsNullOrWhiteSpace(newfileName)) newfileName = defaultFileNameIfFilenameIsInvalid;
        var newExtention = Path.GetExtension(ret);
        if (string.IsNullOrWhiteSpace(newExtention)) newExtention = defaultExtention;

        if (newfileName.Length + newExtention.Length > maxLength) {
            if (newExtention.Length > maxLength - (maxLength / 10)) {
                //Ensure a minimum length of the FileName and cut Extention - which is probably not a real File Extention
                newfileName = Left(newfileName, maxLength / 10);
                newExtention = Left(newExtention, maxLength - newfileName.Length);
            } else {
                newfileName = Left(newfileName, maxLength - newExtention.Length);
            }
        }
        ret = newfileName + newExtention;

        return ret;
    }

Maybe this is enought for you:

    foreach (var invalidFileNameChar in Path.GetInvalidFileNameChars()) {
        fileName = fileName.Replace(invalidFileNameChar, '_');
    }
Markus
  • 2,184
  • 2
  • 22
  • 32