2013-06-25 24 views
2

我試圖循環一個文件夾,並在文件開頭添加一些代碼(儘管我得到了一些不需要的行在我插入的代碼之前休息)並且還要獲取<title>標籤的內容並將其用於重命名每個文件。使用目標文件名的變量重命名文件時,獲取「錯誤的文件名或數字」

我與-'s

代替空格和不想要的字符所有這一切工作,但我也嘗試重命名現有文件(Default0010.html就是一例),從<title>文本。

這也適用,但是當我嘗試將現有文件移動到新文件時,我得到Bad File name or Number,但是當我將explicilty設置爲簡單字符串時,它的工作原理是目標文件名。

它使我的事情我的字符串不乾淨,或者你不能使用目標的變量。

也請忽略行Dim i,i = 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 

回答

1

不要替換已知的錯誤字符。取代所有而不是 a 已知好的字符,例如,通過使用正則表達式:

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 
+0

這看起來不錯,我現在正在嘗試你的建議,一旦我對他們感到滿意,我會給你一個很好的大綠剔!但也請注意,我忘了提及我需要遍歷所有文件,並用新文件名替換任何超鏈接(這將更新網站上的任何鏈接到新頁面) –

+0

我現在得到的對象不支持此屬性或方法:代碼800A01B6''用新文件名重命名舊文件' 'If objFso.FileExists(「C:\ My Web Sites \ Dodderhill \ www.dodderhillhistory.org.uk \ html \」&OldFilename)Then' '設置OBJFILE = ObjFsoFile.OpenTextFile(File.Name)'' = objFile.Name NewFilename' ''關閉file' 'ObjFile.Close' '結束If' –

+0

你讓'objFile'一個[文本流( http://msdn.microsoft。com/en-us/library/312a5kbt)對象,而不是[File](http://msdn.microsoft.com/en-us/library/1ft05taf)對象。只有後者纔有'Name'屬性。它已經正式注意到你更新了文件中的鏈接。因此,我的意見是,簡化的代碼不包括那些部分(你需要再次添加它們的IOW)。他們從示例代碼中省略,以保持示例簡單。 –