我正在尝试循环一个充满 .html 文件的文件夹并在文件的开头添加一些代码(尽管在我插入的代码之前我得到了一些不需要的换行符)并且还获取<title>
标签的内容并使用它用于重命名每个文件。
我用 -'s 替换空格和不需要的字符
所有这些都有效,但我也试图将现有文件(Default0010.html
是一个示例)重命名为<title>
.
这也有效,但是当我尝试将现有文件移动到新文件时,我得到了一个Bad File name or Number
但是当我明确地将目标文件名设置为一个简单的字符串时它可以工作。
这让我觉得我的字符串不干净,或者你不能为目的地使用变量。
也请忽略这些行Dim i
,i = i + 1
和If i=1 Then Exit For
。
这是在我测试脚本时添加的,然后当我很高兴它可以按照我的意愿运行时,我会在所有 HTML 文件上运行它。
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