vb 批量修改文件夹子文件夹所有TXT文本
描述:D:abc目录下有几个TXT文本,及几个子文件夹,子文件夹部分含有TXT文本,txt及文件的名字均是不规则的,
目标:把D:\abc下所有txt文本(含子文件夹下)文本中的 yysj 修改为abcd,改写好后覆盖原文件,文件结构及文本的结构均不改变,
小小要求,给出能直接运行的代码,另,txt文本中不全都含有yysj字样。
最佳答案:
'已经调试过的了
Option Explicit
Private Sub Command1_Click()
Dim FS As Object
Set FS = CreateObject("Scripting.FileSystemObject")
FindFile FS.GetFolder("D:\abc")
Set FS = Nothing
End Sub
Private Sub FindFile(FD As Object)
Dim FS As Object
Set FS = CreateObject("Scripting.FileSystemObject")
Dim FDS As Object, FL As Object
For Each FDS In FD.SubFolders
FindFile FDS
Next
For Each FL In FD.Files
If UCase(FS.GetExtensionName(FL.Path)) = "TXT" Then
ChangeFile FL
End If
Next
Set FS = Nothing
End Sub
Private Sub ChangeFile(FL As Object)
Dim FS As Object
Set FS = CreateObject("Scripting.FileSystemObject")
Dim TXT As Object
Dim s As String
s = FS.OpenTextFile(FL.Path).ReadAll
If InStr(s, "yysj") Then
s = Replace(s, "yysj", "abcd")
Set TXT = FS.CreateTextFile(FL.Path, True)
TXT.Write s
TXT.Close
End If
Set FS = Nothing
End Sub
其 他 回 答: (共2条)
1楼
归递遍历文件夹子文件夹,网上找一下代码来参考自己编写比较好!
查看更多相关问题 >>