正在加载中...

信息详页

返回
VBA遍历文件夹内所有文本文件批量做出修改


来源:转载 浏览量:159 次 发布日期:2020-03-06

Sub OpenCloseArray()
    Dim MyFile As String
    Dim Arr(400) As String
    Dim count As Integer
   
    MyFile = Dir("D:\" & "*.txt")  '遍历D盘根目录下所有文本文件
    count = count + 1
    Arr(count) = MyFile
     
    Do While MyFile <> ""
        MyFile = Dir
        If MyFile = "" Then
            Exit Do
        End If
        count = count + 1
        Arr(count) = MyFile         '将文件的名字存在数组中
    Loop
For i = 1 To count
FileName = "D:\" & Arr(i)
If FileName = False Then Exit Sub
Set FSO = CreateObject("Scripting.FileSystemObject")
Set f = FSO.opentextfile(FileName)
s = Replace(f.readall, "”", Chr(34)) '这里是要替换的文字
s = Replace(s, "“", Chr(34))
s = Replace(s, "‘", "'")
s = Replace(s, "’", "'")
s = Replace(s, "—", "-")
f.Close
Set r = FSO.opentextfile(FileName, 2, True)
r.write s
Next
End Sub