vs遍历文件夹
❶ vba 遍历指定文件夹(含子目录)获取文件名,哪种方法速度最快
Sub LoopAllExcelFilesInFolder()
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'这里很关键,决定宏执行快慢的关键
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'打开目录选择框
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "请选择目录"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'取消选择
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'指定过滤的文件后缀
myExtension = "*.xls*"
'遍历全路径
myFile = Dir(myPath & myExtension)
'循环处理每一个文件
Do While myFile <> ""
'打开
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'确保工作簿被打开,在处理下一个文件时
DoEvents
'设置背景色
wb.Worksheets(1).Range("A1:Z1").Interior.Color = RGB(51, 98, 174)
'保存工作簿
wb.Close SaveChanges:=True
'确保工作簿被关闭,在处理下一个文件时
DoEvents
'接着处理下一个
myFile = Dir
Loop
'提示处理完成
MsgBox "处理完成!"
ResetSettings:
'恢复设置
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
❷ vb.net如何遍历整个文件夹,后找出某一个文件
解压密码:1299
假设路径为C:\abc
代码:
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim Files As ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles("C:\abc") Dim FileSize As Long, FileName As String = Nothing Dim oFileSize As Long For Each File As String In Files oFileSize = My.Computer.FileSystem.GetFileInfo(File).Length If oFileSize > FileSize Then FileSize = oFileSize FileName = File End If Next Dim ListN As ListViewItem = New ListViewItem(My.Computer.FileSystem.GetName(FileName)) ListN.SubItems.Add(oFileSize & " byte") ListN.SubItems.Add(My.Computer.FileSystem.GetFileInfo(FileName).Extension & " 文件") ListN.SubItems.Add(FileName) ListView1.Items.Add(ListN) End SubEnd Class
详见源代码!!