2012-11-19 24 views
0

我對VB腳本很陌生。我需要VB腳本來代替特定的文本通過使用文件夾結構替換文本的VB腳本

下面是我的文件目錄

  1. E:\ TEST \ 98 \ 6549871 \ 1959893 \ HTML
  2. E:\ TEST \ 98 \ 6549871 \ 1959793 \ HTML
  3. E:\ TEST \ 98 \ 6549876 \ 1959863 \ HTML
  4. E:\ TEST \ 96 \ 6749473 \ 6959895 \ HTML
  5. E:\ TEST \ 99 \ 2548878 \ 5959893 \ HTML

等,

在每個HTML子文件夾中,test.html和img.html頁面將包含哪裏。在該HTML頁面中,我想查找文本=「img/」,並且需要替換爲=「/ image/98/6549871/1959893/HTML/img/」,其中=「/ image/」所有文件和其餘值按照文件夾結構(即第一級文件夾名稱,第二級文件夾名稱,第三級文件夾名稱和第四級文件夾名稱)

對於每個單獨的文件,我需要像上面這樣做,很多時間來做這個活動

任何機構可以幫助我在這個基於文件夾目錄一次性替換所有=「img/」。

在此先感謝

回答

0

喜歡的東西this..not測試,但shoudl你指出正確的方向

Option Explicit 

Dim fileCount 

Sub ProcessSubDirectory(ByVal directory) 

On Error Resume Next 

Dim fso 
Dim dir 
Dim folder 
Dim file 

Set fso = CreateObject("Scripting.FileSystemObject") 
Set dir = fso.GetFolder(directory) 

If Err.Number <> 0 THen 
    MsgBox "Failed to open dir (" & directory & ")" 
    Exit Sub 
End If 

On Error Goto 0 

For Each file in dir.Files 
    Call HandleFileFix(file.Path) 
Next 

For Each folder in dir.SubFolders 
    Call ProcessSubDirectory(folder.Path) 
Next 

Set fso = Nothing 

End Sub 

Sub HandleFileFix(ByVal file) 

Dim fso 
Dim f 
Dim fo 
Dim contents 
DIm path 

Set fso = CreateObject("Scripting.FileSystemObject") 
Set fo = fso.GetFile(file) 
Set f = fso.OpenTextFile(file, 1) 

    path = fo.Path 
    path = Replace(path, fo.Drive, "") 
    path = Replace(path, fo.Name, "") 
    path = Replace(path, "\", "/") 

If f.AtEndOfStream = false then 
    contents = f.ReadAll 
End If 

f.Close 
Set f = Nothing 

contents = Replace(contents, "='img/", "='" & path & "/img/") 

' write file back to disk 
Set f = fso.OpenTextFile(file, 2) 
f.Write contents 
f.Close 
fileCount = fileCount + 1 
Set f = Nothing 

End Sub 

fileCount = 0 

Call ProcessSubDirectory("D:\TEST\") 

MsgBox "done (" & CStr(fileCount) & ")" 
+0

-0.49張貼失敗未經測試的代碼,因爲folder.Name(代替。路徑)傳遞給ProcessSubDirectory。 (這個失誤再次證明,在目錄遍歷中傳遞路徑/字符串而不是文件夾/對象是個壞主意。) –

+0

在虛擬文件夾中測試。在替換階段顯示錯誤。因爲它不接受找到=「/ img和替換=」image/98/6549871/1959893/HTML/img/ – Praveen

+0

嗨,測試,但在第64行顯示錯誤消息字符1錯誤無效的過程調用或參數 – Praveen