FileSystemObject - Unsupported filename characters

吃可爱长大的小学妹 提交于 2019-12-11 04:09:22

问题


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

回答1:


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 [~!@#$%^&()]



来源:https://stackoverflow.com/questions/11778282/filesystemobject-unsupported-filename-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!