你说的子目录问题,把第四行FSO.GetFolder(".")中的"."改成".\abc"就处理子目录abc中的文件了。
点“取消”这个问题确实没考虑到,修改如下:- Option Explicit
- Dim FSO,file,ofile,strText
- Set FSO=CreateObject("scripting.filesystemobject")
- For Each file In FSO.GetFolder(".\abc").Files
- If LCase(FSO.GetExtensionName(file))="txt" Then
- Set ofile=FSO.OpenTextFile(file,1)
- strText=ofile.ReadAll():ofile.Close
- Set ofile=FSO.OpenTextFile(file,2)
- ofile.Write(ShowAndReplace(strText,file.Name))
- ofile.Close
- End If
- Next
- WScript.Echo "处理结束!"
-
- Function ShowAndReplace(str,filename)
- Dim Matches,Match
- Dim i,j,intCount,dicTmp,strTmp,strShow,strSource
- With CreateObject("vbscript.regexp")
- .Global=True
- .IgnoreCase=True
- .Multiline=True
- .pattern="S[1-9]\d*(?=M03)"
- If .Test(str)=True Then
- Set Matches=.Execute(str)
- Set dicTmp=CreateObject("scripting.dictionary")
- For Each Match In Matches
- intCount=intCount+1
- dicTmp.Add intCount,Match
- Next
- '排序
- For i=1 To dicTmp.Count-1
- For j=i+1 To dicTmp.Count
- If dicTmp.Item(i)>dicTmp.Item(j) Then
- strTmp=dicTmp.Item(i)
- dicTmp.Item(i)=dicTmp.Item(j)
- dicTmp.Item(j)=strTmp
- End If
- Next
- Next
- For i=1 To dicTmp.Count
- strShow=strShow&dicTmp.Item(i)&vbCrLf
- Next
- For i=1 To dicTmp.Count
- strSource=InputBox(strShow&"请输入要替换 "&dicTmp.Item(i)&" 的值(S+数字):",filename&"的搜索结果")
- If strSource<>"" Then
- str=Replace(str,dicTmp.Item(i),strSource)
- End If
- Next
- ShowAndReplace=str
- Else
- ShowAndReplace=str
- End If
- End With
- End Function
复制代码
|