如何用dir()函式來列出c下所有tmp檔案並且用文字框輸出
下面是乙個列出指定目錄下所有符合要求的檔名的函式:
public function autolistfiles(byval sdirname as string,byval filefilter as string ) as boolean
on error goto rf_error
dim sname as string, sfile as string, ***t as string
dim sdirlist() as string, idirnum as integer, i as integer
′首先列舉所有檔案
sfile = dir(sdirname + filefilter, vbnormal + vbarchive + vbhidden)
do while len(sfile) >0
sfile = ucase(trim(sfile))
′在此處可以將 sfile 加入到乙個 text 控制項...
sfile = dir ′下乙個檔案
loop
idirnum = 0
sname = dir(sdirname + ″*.*″, vbdirectory + vbnormal)
do while len(sname) >0
if sname <> ″.″ and sname <> ″..″ then
idirnum = idirnum + 1
redim preserve sdirlist(1 to idirnum)
sdirlist(idirnum) = sdirname + sname + ″/″
end if
sname = dir ′下乙個目錄
loop
for i = 1 to idirnum
autolistfiles sdirlist(i) ′遞迴呼叫
next
end if
rf_exit:
autolistfiles = true
exit function
rf_error:
msgbox err.description, vbcritical, ″″
resume rf_exit
end function
呼叫上述函式的示例:
autolistfiles(″c:/″,″*.tmp″)
之所以採用上述先處理檔案,再處理子目錄,一是思路較清晰,另外也有模仿 prolog 語言中的「尾遞迴」的想法,當然,vb中是不支援尾遞迴的。
vb 遍歷資料夾 與遍歷檔案
public sub search wenjian 遍歷檔案 獲得檔名稱 dim spath as string 查詢檔案夾 spath dir path,vbdirectory 查詢第乙個資料夾 do while len spath 迴圈到沒有資料夾為止 if left spath,1 then ...
遍歷資料夾
function search path string filename string ball boolean false string varsearchrec tsearchrec begin if findfirst path faanyfile,searchrec 0 then begin...
資料夾遍歷
c 遍歷指定資料夾中的所有檔案 directoryinfo thefolder new directoryinfo folderfullname 遍歷一層資料夾 foreach directoryinfo nextfolder in thefolder.getdirectories 遍歷多層資料夾 ...