'检索一个文件夹里所有txt文件中,包含某一文字,并显示结果 '文件夹下有50个txt文件,从其中检索包含“苹果”的文件,并显示文件名至excel Sub t() Dim app As New Excel.Application Dim wb As Excel.Workbook Set wb = app.Workbooks.Open("c:\3gEnglish.xls") Dim fso, f, f1, fc, s, r Const ForReading = 1, ForWriting = 2 Set fso = CreateObject("Scripting.FileSystemObject") 'Set fc = fso.GetFile(WScript.ScriptFullName).ParentFolder.Files Set fc = fso.GetFolder("c:\").Files L = 1 For Each f1 In fc EXTName = UCase(fso.GetExtensionName(f1.Name)) If EXTName = "TXT" Then Set fs = fso.OpenTextFile(f1, ForReading) fb = fs.ReadAll If InStr(1, fb, "Skip") > 0 Then '文件夹下有50个txt文件,从其中检索包含“Skip”的文件,并显示文件名至excel wb.Sheets("sheet1").Cells(L, 1) = f1.Name wb.Sheets("sheet1").Cells(L, 2) = f1.Path L = L + 1 End If End If Next End Sub |
|
来自: shuaixinerwei > 《vba》