2

我正在尝试循环一个充满 .html 文件的文件夹并在文件的开头添加一些代码(尽管在我插入的代码之前我得到了一些不需要的换行符)并且还获取<title>标签的内容并使用它用于重命名每个文件。

我用 -'s 替换空格和不需要的字符

所有这些都有效,但我也试图将现有文件(Default0010.html是一个示例)重命名为<title>.

这也有效,但是当我尝试将现有文件移动到新文件时,我得到了一个Bad File name or Number但是当我明确地将目标文件名设置为一个简单的字符串时它可以工作。

这让我觉得我的字符串不干净,或者你不能为目的地使用变量。

也请忽略这些行Dim ii = i + 1If 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
4

1 回答 1

1

不要替换已知的坏字符。替换所有不是已知良好字符的内容,例如使用正则表达式:

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

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

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

此外,除非必须,否则不要尝试手动解析 HTML 文件中的元素。在您的情况下,可以更容易地提取标题,如下所示:

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

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

Name当您已经拥有该文件的句柄时,可以通过简单地更改其属性来重命名文件:

objFile.Name = NewFilename

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

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
于 2013-06-25T13:17:46.947 回答