且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取“错误的文件名或数字"使用目标文件名的变量重命名文件时

更新时间:2022-04-28 08:24:43

不要替换已知的坏字符.替换所有不是已知好的字符,例如通过使用正则表达式:

Don't replace known bad characters. Replace everything that is not a known good character, e.g. by using a regular expression:

Set re = New RegExp
re.Pattern = "[^a-z0-9+._-]+"
re.Global  = True
re.IgnoreCase = True

NewFilename = re.Replace(OldFilename, "_")

下划线 (_) 通常是这种替换的安全字符.

The underscore (_) usually is a safe character for this kind of replacement.

此外,除非万不得已,否则不要尝试手动解析 HTML 文件中的元素.在您的情况下,可以更轻松地提取标题,如下所示:

Also, don't try to manually parse elements from an HTML file unless you have to. In your case the title can be extracted far easier, like this:

Set html = CreateObject("HTMLFile")
html.Write objFso.OpenTextFile(File.Name).ReadAll
title = html.Title

它甚至会为您折叠和修剪空白.

It will even collapse and trim whitespace for you.

当您已经拥有该文件的句柄时,只需更改其 Name 属性即可重命名文件:

And a file can be renamed by simply changing its Name property when you already have a handle to that file:

objFile.Name = NewFilename

脚本的简化版本(没有修改文件内容的那些部分):

Simplified version of your script (without those parts that modify the content of the files):

Set fso = CreateObject("Scripting.FileSystemObject")

htmlFolder = "C:\My Web Sites\test\www.test.org.uk\html"

Set re = New RegExp
re.Pattern = "[^a-z0-9+._-]+"
re.Global  = True
re.IgnoreCase = True

For Each f In objFso.GetFolder(htmlFolder).Files
   data = f.OpenAsTextStream.ReadAll

   Set html = CreateObject("HTMLFile")
   html.Write data

   oldname = f.Name
   newname = re.Replace(f.Name, "_")

   f.Name = newname
Next