VBA遍歷資料夾常用有三種方法

2021-07-25 11:10:07 字數 3458 閱讀 5930

vba遍歷資料夾常用有三種方法,這三種方法中,filesearch不適合2007和2010版本,而且速度比較慢,遞迴法速度也慢。只有用dir加迴圈的方法,速度飛快。下面是三種方法的**:

1、filesearch法

sub test3()

dim wb as workbook

dim i as long

dim t

t = timer

.newsearch '開始新的搜尋

.lookin = thisworkbook.path '設定搜尋的路徑

.searchsubfolders = true '搜尋範圍包括 lookin 屬性指定的資料夾中的所有子資料夾

.filename = "*.xls" '設定搜尋的檔案型別

' .filetype = msofiletypeexcelworkbooks

if .execute() > 0 then '如果找到檔案

for i = 1 to .foundfiles.count

'on error resume next

cells(i, 1) = .foundfiles(i) '把找到的檔案放在單元格裡

next i

else

msgbox "沒找到檔案"

end if

end with

msgbox timer - t

end sub

2、遞迴法

sub test()

dim ipath as string, i as long

dim t

t = timer

.title = "請選擇要查詢的資料夾"

if .show then

ipath = .selecteditems(1)

end if

end with

if ipath = "false" or len(ipath) = 0 then exit sub

i = 1

call getfolderfile(ipath, i)

msgbox timer - t

msgbox "檔名鏈結獲取完畢。", vbokonly, "提示"

end sub

private sub getfolderfile(byval npath as string, byref icount as long)

dim ifilesys

'dim ifile as files, gfile as file

'dim ifolder as folder, sfolder as folders, nfolder as folder

set ifilesys = createobject("scripting.filesystemobject")

set ifolder = ifilesys.getfolder(npath)

set sfolder = ifolder.subfolders

set ifile = ifolder.files

with activesheet

for each gfile in ifile

' .hyperlinks.add anchor:=.cells(icount, 1), address:=gfile.path, texttodisplay:=gfile.name

icount = icount + 1

next

end with

'遞迴遍歷所有子資料夾

for each nfolder in sfolder

call getfolderfile(nfolder.path, icount)

next

end sub

3、dir迴圈法

sub test() '使用雙字典,旨在提高速度

dim myname, dic, did, i, t, f, tt, myfilename

'on error resume next

set objfolder = objshell.browseforfolder(0, "選擇資料夾", 0, 0)

if not objfolder is nothing then lj = objfolder.self.path & "\"

set objfolder = nothing

set objshell = nothing

t = time

set dic = createobject("scripting.dictionary") '建立乙個字典物件

set did = createobject("scripting.dictionary")

dic.add (lj), ""

i = 0

do while i < dic.count

ke = dic.keys '開始遍歷字典

myname = dir(ke(i)

, vbdirectory) '查詢目錄

do while myname <> ""

if myname <> "." and myname <> ".." then

if (getattr(ke(i) & myname) and vbdirectory) = vbdirectory then '如果是次級目錄

dic.add (ke(i) & myname & "\"), "" '就往字典中新增這個次級目錄名作為乙個條目

end if

end if

myname = dir '繼續遍歷尋找

loop

i = i + 1

loop

did.add ("檔案清單"), "" '以查詢d盤下所有excel檔案為例

for each ke in dic.keys

myfilename = dir(ke & "*.xls")

do while myfilename <> ""

did.add (ke & myfilename), ""

myfilename = dir

loop

next

for each sh in thisworkbook.worksheets

if sh.name = "xls檔案清單" then

sheets("xls檔案清單").cells.delete

f = true

exit for

else

f = false

end if

next

if not f then

sheets.add.name = "xls檔案清單"

end if

sheets("xls檔案清單").[a1].resize(did.count, 1) = worksheetfunction.transpose(did.keys)

tt = time - t

msgbox minute(tt) & "分" & second(tt) & "秒"

end sub

三種遍歷資料夾方法比較 PERL

一般黑客都常用遍歷方法來進行插入掛馬 等。三種遍歷資料夾方法比較 本貼對三種遍歷資料夾方法比較。1.使用file find 2.遞迴遍歷。遍歷函式為lsr 3.使用佇列或棧遍歷。遍歷函式為lsr s 1.use file find usr bin perl w file find.pl author...

C 刪除資料夾及檔案 三種方法

1這是最簡單的方法 directoryinfo di new directoryinfo string path di.delete true 注 path是你要刪除的非空目錄 true 你要刪除裡面所有的檔案,包括資料夾和子資料夾 2 刪除非空資料夾 要刪除的資料夾目錄 void deletedi...

Qt 遍歷資料夾的三種方式

github dome 今天的心態被搞崩潰了,在多年的程式設計經驗裡。普遍的經驗認為在遍歷資料夾這件事情上,肯定是佇列優於遞迴的。但在qt 由於有一些庫函式,我發現使用遞迴的速度盡然比佇列還快,而且佇列的遍歷還特別的慢,估計是我寫的佇列遍歷那個地方又問題,請大神指導一下,這是為什麼呢?我乙個qt初學...