2

.html ファイルでいっぱいのフォルダーをループし、ファイルの先頭にいくつかのコードを追加しようとしています (ただし、挿入するコードの前に不要な改行が発生しています)。また、<title>タグの内容を取得してこれを使用します。各ファイルの名前を変更します。

スペースと不要な文字を - に置き換えています

これはすべて機能しますが、既存のファイルの名前を変更しようとしています (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
4

1 に答える 1