FileSystemObject - Unsupported filename characters

asp-classic, filesystems, vbscript

Solution

You can make a function that will return a 'cleaned' filename like:

function MakeNormal(filename)
    dim re : Set re = new regexp

    re.Pattern = "[^\w :\\\.]"
    re.Global = True

    MakeNormal = re.Replace(filename, "_")

end function

msgbox MakeNormal("C:\Temp\normal filename.txt")
msgbox MakeNormal("C:\Temp\special ~!@#$%^&*() filename.txt")

' returns: "C:\Temp\normal filename.txt" and "C:\Temp\special __________ filename.txt"

And replace the name of the file with the cleaned one. Becomes risky when you have two files that are only unique on the special characters.

Above is the 'whitelist' variant, if you prefer a 'blacklist' version, you can replace the pattern for something like `[~!@#$%^&()]`

Problem

Is there a function I can use that converts dodgy filenames with good filenames? I'm processing a large amount of photos, and very occasionally, my script stops because the uploader has put a curly symbol (~) in the filename. I'm also now wondering if there are any other bad symbols that can't be in a filename and how to escape them. I'm looping through these files using VBScript's FileSystem Object, similar to the following: ``` For Each File In Files If InStr(UCase(File.Name), ".JPG") > 0 Then '// do stuff End If Next ```

Original source