获取“错误的文件名或数字"使用目标文件名的变量重命名文件时 [英] Getting "Bad Filename or Number" when renaming file with a variable for the destination filename
问题描述
我试图循环一个充满 .html 文件的文件夹,并在文件的开头添加一些代码(尽管在我插入的代码之前我得到了一些不需要的换行符)并且还获取了 <title>
标记并使用它来重命名每个文件.
I am trying to loop a folder full of .html files and add some code at the beginning of the files (although I am getting some unwanted line breaks before the code I am inserting) and also to grab the contents of the <title>
tag and use this for the renaming each file.
我正在用 - 替换空格和不需要的字符
I am replacing the spaces and unwanted characters with -'s
所有这些都有效,但我也在尝试将现有文件(Default0010.html
是一个示例)重命名为
中的文本.
All of this works but I am also trying to rename the existing file (Default0010.html
is one example) to the text from the <title>
.
这也有效,但是当我尝试将现有文件移动到新文件时,我得到一个 Bad File name or Number
但是当我明确地将目标文件名设置为一个简单的字符串时它可以工作.
This works too but when I am trying to move the existing file to the new file I get a Bad File name or Number
but when I explicilty set the destination file name to a simple string it works.
这让我觉得我的字符串不干净或者你不能使用目标变量.
It makes me thing my string is not clean or you cannot use a variable for the destination.
也请忽略Dim i
、i = i + 1
和If i=1 Then Exit For
行.
这是在我测试脚本时添加的,然后当我感到满意时,我会在所有 HTML 文件上运行它.
This was added whilst I test the script then when I was happy it does what I wanted I would run it on all the HTML files.
Set objFso = CreateObject("Scripting.FileSystemObject")
Set Folder = objFSO.GetFolder("C:\My Web Sites\test\www.test.org.uk\html")
Dim i
Dim ObjFsoFile
Dim ObjFile
Dim StrData
Dim StrTitleTag
Dim OldFilename
Dim NewFilename
Set ObjFsoFile = CreateObject("Scripting.FileSystemObject")
'Loop all of the files
For Each File In Folder.Files
'Get contents of the file and store in a string
'Opening the file in READ mode
Set ObjFile = ObjFsoFile.OpenTextFile(File.Name)
'Reading from the file
StrData = ObjFile.ReadAll
'Add the Perch include to the beginning
StrData = replace(StrData,"<?php include('cms/runtime.php');?>","") 'Remove the Perch include in-case we are re-running this
StrData = replace(StrData,"<!DOCTYPE HTML PUBLIC " & Chr(34) & "-//W3C//DTD HTML 4.0 Transitional//EN" & Chr(34) & ">","<?php include('cms/runtime.php');?>" & vbcrlf & "<!DOCTYPE HTML PUBLIC " & Chr(34) & "-//W3C//DTD HTML 4.0 Transitional//EN" & Chr(34) & ">")
'Msgbox StrData
'Closing the file
ObjFile.Close
'Write the changes to the current file
Set objFile = objFSO.CreateTextFile(File.Name,True)
objFile.Write StrData
objFile.Close
'Re-write the contents of the current file and replace with the StrData Above
'Grab the contents between <title> and </title>
parse_string1 = StrData 'see above post
parse_string1 = replace(parse_string1,"<title>","¦")
parse_string = split(parse_string1,"¦")
parse = parse_string(1)
parse_string1 = replace(parse,"</title>","¦")
parse_string = split(parse_string1,"¦")
parsed_string = parse_string(0)
StrTitleTag = parsed_string 'gives final result
'Save old filename of current file to a string
OldFilename = File.Name
'Msgbox OldFilename
'Rename current file to the above contents of between <title> and </title>
'Replace spaces with - characters in the filename.
Dim divider
divider = "-"
'Replace & with and
NewFilename = Replace((StrTitleTag & ".php"),"&","and")
'Replace triple space with single space
NewFilename = Replace(NewFilename," "," ")
'Replace double space with single space
NewFilename = Replace(NewFilename," "," ")
'Replace - with space
NewFilename = Replace(NewFilename," ",divider)
'Replace ---- with -
NewFilename = Replace(NewFilename,divider & "-" & divider,divider)
'Replace ---- with -
NewFilename = Replace(NewFilename,divider & divider & divider,divider)
'Replace ,- with -
NewFilename = Replace(NewFilename,"," & divider,divider)
'Replace LineBreaks with nothing (remove line breaks)
NewFilename = Replace(NewFilename,vbCrLf,"")
NewFilename = Replace(NewFilename,vbLf,"")
NewFilename = Replace(NewFilename,vbCr,"")
NewFilename = LCase(NewFilename)
'Msgbox NewFilename
'Loop through all files
For Each File2 In Folder.Files
'Opening the file in READ mode
Set ObjFile = ObjFsoFile.OpenTextFile(File2.Name)
'Get contents of the file and store in a string
'Reading from the file
StrData = ObjFile.ReadAll
'Closing the file
ObjFile.Close
'Replace all occurences of the old filename with the new filename
StrData = Replace(StrData, OldFilename, NewFilename)
'How to write file
Set objFile = objFSO.CreateTextFile(File2.Name,True)
objFile.Write StrData
objFile.Close
Next
'Rename Old file with the new filename
If objFso.FileExists("C:\My Web Sites\test\www.test.org.uk\html\" & OldFilename) Then
'NewFileName = "test.php"
'NewFileName = "test-test-test-test-test-test-test-test-test.php"
Msgbox "Renaming the file " & OldFilename & " (Length: " & Len(OldFilename) & ") with the following name: " & NewFilename & " (Length: " & Len(NewFilename) & ")"
Msgbox "Compare: test-test-test-test-test-test-test-test-test.php " & NewFilename
objFso.MoveFile "C:\My Web Sites\test\www.test.org.uk\html\" & OldFilename, "C:\My Web Sites\test\www.test.org.uk\html\" & NewFileName
End If
i = i + 1
If i=1 Then Exit For
Next
推荐答案
不要替换已知的坏字符.替换所有不是已知好的字符,例如通过使用正则表达式:
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
这篇关于获取“错误的文件名或数字"使用目标文件名的变量重命名文件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!