当前位置:首页 » 文件管理 » vs遍历文件夹

vs遍历文件夹

发布时间: 2024-06-10 14:41:53

❶ 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

详见源代码!!

热点内容
微信收藏表情文件夹 发布:2025-01-16 19:28:57 浏览:14
ra服务器搭建 发布:2025-01-16 19:28:12 浏览:17
javaftp读取 发布:2025-01-16 19:28:02 浏览:184
乐课上传作业 发布:2025-01-16 19:24:58 浏览:935
哈尔滨python培训 发布:2025-01-16 19:19:30 浏览:915
java对象与线程 发布:2025-01-16 19:14:59 浏览:896
二维码源码vc 发布:2025-01-16 19:14:59 浏览:773
单独编译内核模块 发布:2025-01-16 18:54:26 浏览:804
js解压字符串 发布:2025-01-16 18:54:17 浏览:482
php怎么开启服务器 发布:2025-01-16 18:52:53 浏览:771