vb實現目錄下所有檔名 目錄名稱的獲取

2021-05-10 10:54:19 字數 2510 閱讀 7834

前幾日,有個哥們說有很多電影,問我要不要?我就讓他乙個乙個的寫過來,讓哥們累個不輕。所以就想簡單寫個程式,獲取一下資料夾下面的所有檔名稱,這樣就可以節省很多時間了。

軟體呢,我已經上傳到**了:http://download.csdn.net/source/1802599

vb版本目錄獲取器 1.0(龍捲風出品)

說明:1.選好目錄後,可以直接得到目錄下的資料夾和檔名稱,以及統計數目。

2."樹形結構"按鍵將會把各資料夾和檔案羅列下來。

3."樹形展開"將把目錄下的子目錄也一併展開。大家測試的時候需要注意,不要用太深的目錄來測。

下面開始動手的步驟:

vb中有個函式dir(),這個就是今天的主角了。dir()第一次呼叫的時候,必須指定路徑。然後其會返回乙個string,這就是乙個資料夾,或者乙個檔案的名稱了。具體用法,大家google一下吧。我就不多說了。

然後就是區分返回值是資料夾,還是檔案。這裡用到的就是vbdirectory(=16)這個屬性了。

首先我是將檔案顯示做了區分:資料夾名稱顯示folderlist(),檔名稱顯示filelist():

private function folderlist(pathname as string, treeflag as integer)'treeflag:0列表 1樹形 2樹形展開

'資料夾數和名稱

dim sf as string

sf = dir(pathname, vbdirectory) '獲取第乙個目錄名

foldernum = 0

do while sf <> ""

' 跳過當前的目錄及上層目錄

if sf <> "." and sf <> ".." then

if (getattr(pathname + sf) and vbdirectory) = vbdirectory then

if treeflag = 0 then

text2 = text2 & sf & vbcrlf  ' 如果它是乙個目錄,將其名稱新增到列表

elseif treeflag = 1 then

text2 = text2 & treestr & sf & vbcrlf

elseif treeflag = 2 then '樹形展開的時候,需要了解子目錄,所以還需進行遞迴呼叫

text2 = text2 & treestr & sf & vbcrlf

folderlist pathname + sf, treeflag

filelist pathname + sf, treeflag

end if

foldernum = foldernum + 1 '統計資料夾數目

end if

end if

sf = dir() ' 查詢下乙個目錄

loop

if treeflag = 0 then

if foldernum <> 0 then

text2 = text2 & vbcrlf & "*****=共有" & foldernum & "個資料夾*****=" & vbcrlf & vbcrlf

else

text2 = text2 & "*****=無子目錄*****=" & vbcrlf & vbcrlf

end if

elseif foldernum <> 0 then

text2 = text2 & nullstr & vbcrlf & nullstr & vbcrlf

end if

end function

private function filelist(pathname as string, treeflag as integer)'0列表 1樹形 2樹形展開

'檔案數和名稱

dim a as string

a = dir(pathname & "/*.*")

filenum = 0

do while a <> ""

if treeflag = 0 then

text2 = text2 & a & vbcrlf

else

text2 = text2 & treestr & a & vbcrlf

end if

filenum = filenum + 1 '統計檔案數目

a = dir

loop

if treeflag = 0 then

if filenum <> 0 then

text2 = text2 & "*****=共有" & filenum & "個檔案*****="

else

text2 = text2 & "*****=無檔案資訊*****="

end if

end if

end function

ok,現在大家都已經了解處理機制了。應該不會再管我要什麼原始碼了吧!呵呵

列出指定目錄下所有檔名

要求 絕對路徑名做輸入引數,列印輸出該路徑下所有檔名 知識點 開啟目錄函式 dir opendir const char path 讀取目錄函式 struct dirent readdir dir dir 返回下乙個目錄項的指標。include include dirent結構體定義如下 struc...

批處理獲取目錄下所有檔名

由於要處理一些檔案,找了個這樣的批處理 輸出目錄及子目錄下所有的jpg的檔名,不含副檔名 其中,delims 的含義是取消預設的分隔符,所以會把行上的內容照搬下來,而 tokens 表示獲取行上的所有內容,但是會忽略行首的所有空格。另外的引數介紹一下 0 1 2 3 4 5 6 7 8 9 命令列傳...

shell提取檔名和目錄名

用於字串的讀取,提取和替換功能,可以使用 提取字串1.提取檔名 root localhost test var mnt aaa test test.txt root localhost test echo test.txt2.提取字尾 root localhost test echo txt3.提取...