1. 如何用 VB 獲取一個文件夾下的文件名列表
下面的別人寫的!以前寫過一個,是仿照換了時光的vbs
寫的!可惜代碼不知道放那裡了!記得那個好像比下面的方法要簡單的多
用VB函數Dir實現遞歸搜索目錄
日期:2006-03-09 來源: 作者:
我在很久以前就實現了這個方法了.它沒有採用任何的控制項形式.也沒有調用系統API函數FindFirst,FindNext進行遞歸調用,和別人有點不同的就是我用的是VB中的Dir()函數.事實上,直接採用Dir()函數是不能進行自身的遞歸的調用的,但我們可以採用一種辦法把Dir將當前搜索目錄的子目錄給保存下來,然後在自身的search(strPathName)遞歸函數中依次進行遞歸的調用,這樣就可以把指定的目錄搜索完畢.
具體代碼如下:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'函數GetExtName
'功能:得到文件後綴名(擴展名)
'輸入:文件名
'輸出:文件後綴名(擴展名)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function GetExtName(strFileName As String) As String
Dim strTmp As String
Dim strByte As String
Dim i As Long
For i = Len(strFileName) To 1 Step -1
strByte = Mid(strFileName, i, 1)
If strByte <> "." Then
strTmp = strByte + strTmp
Else
Exit For
End If
Next i
GetExtName = strTmp
End Function
Public Function search(ByVal strPath As String, Optional strSearch As String = "") As Boolean
Dim strFileDir() As String
Dim strFile As String
Dim i As Long
Dim lDirCount As Long
On Error GoTo MyErr
If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
strFile = Dir(strPath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)
While strFile <> "" '搜索當前目錄
DoEvents
If (GetAttr(strPath + strFile) And vbDirectory) = vbDirectory Then '如果找到的是目錄
If strFile <> "." And strFile <> ".." Then '排除掉父目錄(..)和當前目錄(.)
lDirCount = lDirCount + 1 '將目錄數增1
ReDim Preserve strFileDir(lDirCount) As String
strFileDir(lDirCount - 1) = strFile '用動態數組保存當前目錄名
End If
Else
If strSearch = "" Then
Form1.List1.AddItem strPath + strFile
ElseIf LCase(GetExtName(strPath + strFile)) = LCase(GetExtName(strSearch)) Then
'滿足搜索條件,則處理該文件
Form1.List1.AddItem strPath + strFile '將文件全名保存至列表框List1中
End If
End If
strFile = Dir
Wend
For i = 0 To lDirCount - 1
Form1.Label3.Caption = strPath + strFileDir(i)
Call search(strPath + strFileDir(i), strSearch) '遞歸搜索子目錄
Next
ReDim strFileDir(0) '將動態數組清空
search = True '搜索成功
Exit Function
MyErr:
search = False '搜索失敗
End Function
2. vb 獲取目錄所有文件名
1、首先,在電腦上打開VB6.0屬性窗口,新建一個EXE工程窗口。
3. 如何用VB獲得某一目錄下的所有文件和文件夾名稱
Dim a() As String
Private Sub Dir1_Change()
File1.Path = Dir1.Path
Label1.Caption = Dir1.Path
End Sub
Private Sub Drive1_Change()
On Error GoTo err1
Dir1.Path = Drive1.Drive
Exit Sub
err1:
MsgBox Err.Description, vbInformation + vbOKOnly, "提示"
End Sub
Private Sub Command1_Click()
Dim i As Integer, j As Integer
Dim ifieldcount As Integer, irecordcount As Integer
Dim wdapp As Word.Application
Dim wddoc As Word.Document
Dim atable As Word.Table
If Option2.Value = True Then
ReDim a(1 To File1.ListCount)
For i = 1 To File1.ListCount
b = File1.List(i)
a(i) = b
Next
End If
If Option1.Value = True Then
ReDim a(1 To Dir1.ListCount)
For i = 1 To Dir1.ListCount
b = Dir1.List(i)
a(i) = b
Next
End If
End Sub
這段程序 復制過去 就行了 創建的控制項有 兩個option 讓你選擇 要提取文件夾 還是文件路徑的 選中option 1 就是提取文件夾的 option2 就是提取文件的 然後再創建 Drive1 Dir1 File1 這三個 選擇文件路徑的 控制項 再加一個 command 按鈕 就行了 有疑問再聯系我!!!!!這個 只是 文件路徑 你看看 是你想要的話 我再給你寫 提取文件夾和文件名的代碼!!!!
4. VB.Net怎樣得到某個文件夾里的所有文件名
如果想要獲得當前文件中的文件名只需要String [] fileName = file.list();就可以了。如果要包括文件中的文件名就內可以用遞歸的方容式。下面是兩個具體的實現。 其中public static String [] getFileName(String path)是只得到當前文件中的文件名。
5. VB怎樣快速獲取當前文件夾下的所有文件的路徑(包括子目錄)
有兩種方法:
1、使用DIR遞歸,不過這是微軟明確不推薦的
2、最快但不太正規的
使用 Shell "cmd /k dir c:\windows /s/b/a >>d:\jg.txt"
c:\windows 目標文件夾,當前文件夾為 app.path,你可以根據要求生成命令字元串
d:\jg.txt 為臨時文件
執行這行後再讀取臨時文件即可。極快!
6. 用VB怎樣獲取一個文件夾內所有文件名
VB可以使用FileListBox 控制項來獲取指定文件夾內的所有文件名。
FileListBox 控制項,在運行時團叢彎,在 Path 屬性指定的目錄中,FileListBox 控制項將文件定位並列舉出來。該控制項用來顯示所選擇文件類型的文鄭凳件列表。例如,可以在應用程序中創建對話框,通過它選擇一個文件或者一組文件。
以下是組合使塌悶用DriveListBox 、控制項DirListBox 控制項和FileListBox 控制項來獲取硬碟下任何一個文件夾內的文件名。
PrivateSubDir1_Change()
File1.Path=Dir1.Path
EndSub
PrivateSubDrive1_Change()
Dir1.Path=Drive1.Drive
EndSub
PrivateSubFile1_Click()
DimiAsLong
Debug.Print"目錄夾內存在"&File1.ListCount&"個文件。"
EndSub