我們可能會經常要將一個文件夾中的所有文件都遍歷一遍,然後進行修改,下面就介紹用Dir函數實現遍歷*.xlsx文件的方法
Dir 函數
返回一個 String,用以表示一個文件名、目錄名或文件夾名稱,它必須與指定的模式或文件屬性、或磁盤卷標相匹配。
語法
Dir[(pathname[, attributes])]
第一個參數即是文件的地址,第一次引用的時候要標註,第二次用的時候就不必指出了,下面舉個例子,遍歷下面文件夾中的Excel2010文件,然後輸出文件的名字~
批量遍歷某類文件(*.xlsx)
- Sub OpenAndClose()
- Dim MyFile As String
- Dim s As String
- Dim count As Integer
- MyFile = Dir("C:\Users\McDelfino\Desktop\2.JPL_SCAT_EXCEL全\" & "*.xlsx")
- '讀入文件夾中的第一個.xlsx文件
- count = count + 1 '記錄文件的個數
- s = s & count & "、" & MyFile
- Do While MyFile <> ""
- MyFile = Dir '第二次讀入的時候不用寫參數
- If MyFile = "" Then
- Exit Do '當MyFile爲空的時候就說明已經遍歷完了,這時退出Do,否則還要運行一遍
- End If
- count = count + 1
- If count Mod 2 <> 1 Then
- s = s & vbTab & count & "、" & MyFile
- Else
- s = s & vbCrLf & count & "、" & MyFile
- End If
- Loop
- Debug.Print s
- End Sub
運行結果如下:
53、
遍歷每個文件,並且修改文件,先將文件的名字存在數組中,然後通過數組遍歷打開每個文件,修改,再關閉文件~
- Sub OpenCloseArray()
- Dim MyFile As String
- Dim Arr(100) As String
- Dim count As Integer
- MyFile = Dir("C:\Users\McDelfino\Desktop\2.JPL_SCAT_EXCEL全\" & "*.xlsx")
- 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
- Workbooks.Open Filename:="C:\Users\McDelfino\Desktop\2.JPL_SCAT_EXCEL全\" & Arr(i) '循環打開Excel文件
- Cells(1, 1) = "alex_bn_lee" '修改打開文件的內容
- ActiveWorkbook.Close savechanges = True '關閉打開的文件
- Next
- End Sub
要是想要修改每個工作簿的內容可以這樣遍歷一下,顯示將文件夾中的工作簿的名字存到一個字符串數組中,然後在用For...Next語句遍歷
批量遍歷某個文件夾中的所有文件(*.*)
注意:遍歷的時候,順序完全是按照文件名的順序排的,而不是按照文件夾中文件的順序~
- Sub dlkfjdl()
- Dim MyFile As String
- Dim count As Integer
- count = 1
- MyFile = Dir("C:\Users\McDelfino\Desktop\桌面\Excel\*.*")
- Debug.Print "1、" & MyFile
- Do While MyFile <> ""
- count = count + 1
- MyFile = Dir
- If MyFile = "" Then Exit Do
- Debug.Print count & "、" & MyFile
- Loop
- End Sub
批量建立TXT文件
批量建立,同時可以批量賦值到文本文件中~
- Sub kdjfl()
- For i = 1 To 10
- Open "C:\Users\McDelfino\Desktop\練習\" & Format(i, "00") & ".txt" For Output As #i
- Print #i, i
- Close #i
- Next
- End Sub
GetFolder方法
返回一個和指定路徑中文件夾相對應的 Folder 對象。應用於FileSystemObject對象~
遍歷文件夾內的所有文件
- Sub GetFiles()
- Dim fs, f, f1, fc
- Set fs = CreateObject("scripting.filesystemobject")
- Set f = fs.getfolder("F:\Desktop\2.wind_numerical_excello")
- Set fc = f.Files
- For Each f1 In fc
- Debug.Print f1
- Debug.Print "f1 = " & TypeName(f1)
- Next
- MsgBox "fs = " & TypeName(fs) _
- & vbCrLf & "f = " & TypeName(f) _
- & vbCrLf & "fc = " & TypeName(fc)
- End Sub
fs = FileSystemObject對象:提供對計算機文件系統的訪問。
f = Folder對象:提供對一個文件夾所有屬性的訪問。
fc = Files集合:在一個文件夾內的所有 File 對象的集合。
f1 = File對象:提供對文件所有屬性的訪問。
FileSystemObject對象及TextStream對象的方法舉例:
- Sub djkflds()
- Dim fso, fd, fs, f, ft, s
- Set fso = CreateObject("Scripting.FileSystemObject")
- fso.MoveFile "F:\Desktop\1.xlsx", "F:\Desktop\2.wind_numerical_excello\1.xlsx"
- '移動文件
- fso.MoveFolder "F:\Desktop\temp", "F:\Desktop\2.wind_numerical_excello\temp"
- '移動文件夾
- MsgBox fso.FileExists("F:\Desktop\1.xlsx")
- '判斷文件是否存在,存在返回True,否則返回False
- MsgBox fso.FolderExists("F:\Desktop\temp")
- '判斷文件夾是否存在,存在返回True,否則返回False
- Set ft = fso.OpenTextFile("F:\Desktop\1.txt", 8, -2)
- '8打開一個文件並寫到文件的尾部 -2使用系統缺省打開文件
- 'ft是TextStream對象,加快對文件的順序訪問
- ft.Write "Hello World" 'Write方法,在一行上
- For i = 1 To 10
- ft.WriteLine i 'WriteLien方法,另起一行
- Next
- ft.Close 'Close方法,關閉文件
- fso.DeleteFolder "F:\Desktop\1"
- '刪除一個文件夾,並且是不放在回收站裏面的
- End Sub
Folder對象的屬性和方法舉例:
Size方法
- Sub GetSize()
- Dim fso, fd, fs, f
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set fd = fso.GetFolder("F:\Desktop\2.wind_numerical_excello")
- Set fs = fd.SubFolders
- For Each f In fs
- Debug.Print f.Name, Format(f.Size / 1024 / 1024, "#.##") & "M"
- Next
- End Sub
File對象的屬性和方法舉例:
屬性和方法與Folder對象類似~
遍歷文件夾中的子文件夾及文件
- Sub getfiles()
- Dim fso, folder, fds, fd, folder2, fs, f
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set folder1 = fso.GetFolder("F:\Desktop\2.wind_numerical_excello") '獲得文件夾
- Set fds = folder1.subfolders '子文件夾集合
- For Each fd In fds '遍歷子文件夾
- Debug.Print fd.Name
- Set folder2 = fd '獲得文件夾2
- Set fs = folder2.Files '文件集合
- For Each f In fs '遍歷文件
- Debug.Print f.Name
- Next
- Debug.Print
- Next
- End Sub