FileSystemObject-不支持的文件名字符 [英] FileSystemObject - Unsupported filename characters

查看:60
本文介绍了FileSystemObject-不支持的文件名字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否可以使用一个函数来将不可靠的文件名转换为具有良好文件名的文件?

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.

我正在使用VBScript的FileSystem对象遍历这些文件,类似于以下内容:

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

推荐答案

您可以创建一个函数,该函数将返回清除的"文件名,例如:

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

这篇关于FileSystemObject-不支持的文件名字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆