1

我对 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

ETC。,

在每个 HTML 子文件夹中,test.html 和 img.html 页面将包含在哪里。在那个 html 页面中,我想找到文本="img/并需要用="/image/98/6549871/1959893/HTML/img/替换,其中="/image/对所有文件都是通用的,其余值是根据文件夹结构(即一级文件夹名称、二级文件夹名称、三级文件夹名称和四级文件夹名称)

对于我需要像上面那样做的每个单独的文件,并且花费太多时间来做这个活动。

任何人都可以帮助我在文件夹目录的基础上一次性替换所有=“img/” 。

提前致谢

4

1 回答 1

0

像这样的东西..未经测试,但应该为您指明正确的方向

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) & ")"
于 2012-11-19T12:56:59.290 回答