Ⅰ vb 如何讀取某目錄下所有文件及子文件夾
查找某目錄下所有 文件 及 子文件夾
試一試不用 FileSystemObject 對象,只用基本控制項的代碼。
'例子需控制項:Command1,List1,List2,File1,Dir1,都採用默認屬性。
'例如,查找 C:\ ,帶 '** 的語可修改
Dim ctFind As Boolean
Private Sub Form_Load()
Me.Caption = "查找所有文件及文件夾"
Command1.Caption = "查找"
List2.Visible = False: File1.Visible = False: Dir1.Visible = False
Label1.Caption = "就緒"
End Sub
Private Sub Form_Unload(Cancel As Integer)
End
End Sub
Private Sub Form_Resize()
Dim W As Long
On Error Resume Next
W = 720
List1.Move 0, 0, Me.ScaleWidth - W - 120, Me.ScaleHeight - 300
Command1.Move Me.ScaleWidth - W - 60, 300, W
Label1.Move 90, Me.ScaleHeight - 255, Screen.Width, 255
End Sub
Private Sub Command1_Click()
ctFind = Not ctFind
If ctFind Then
Command1.Caption = "取消"
Call FindDirFile("C:") '**查找 C:\ 下的所有文件和目錄,或 C:\Windows 等
Command1.Caption = "查找"
Else
Command1.Caption = "查找"
End If
End Sub
Private Sub FindDirFile(ByVal nPath As String)
Dim I As Long, nDir As String, Ci As Long
ctFind = True
List1.Clear: List2.Clear
If Right(nPath, 1) <> "\" Then nPath = nPath & "\"
List1.AddItem "查找 " & nPath: List2.AddItem nPath
File1.Pattern = "*"
File1.System = True: File1.Hidden = True: File1.ReadOnly = True
On Error GoTo Cuo
Dir1.Path = nPath
On Error GoTo 0
Do
If List2.ListCount = 0 Then Exit Do
nPath = List2.List(0)
List2.RemoveItem 0
Dir1.Path = nPath
For I = 0 To Dir1.ListCount - 1
GoSub ShowGe
nDir = Dir1.List(I)
If Right(nDir, 1) <> "\" Then nDir = nDir & "\"
List1.AddItem "■" & nDir
List2.AddItem nDir
Next
File1.Path = nPath
For I = 0 To File1.ListCount - 1
GoSub ShowGe
List1.AddItem " " & nPath & File1.List(I)
Next
Loop
Label1.Caption = "查找完畢,共找到 " & List1.ListCount & " 個條目"
ctFind = False
Exit Sub
Cuo:
List1.AddItem "起始目錄不存在:" & nPath
ctFind = False
Exit Sub
ShowGe:
Ci = Ci + 1
If Ci < 99 Then Return
Ci = 0
Label1.Caption = "已找到 " & List1.ListCount & " 個:" & nPath
DoEvents
If ctFind Then Return
End Sub
Ⅱ vb2013 如何獲取指定目錄下及子目錄下所有文件
這個分兩個步驟。
第一步是利用 System.IO.DirectoryInfo 類的 GetDirectories() 方法,遍歷指定目錄下慎卜的所有子目錄。褲仿
第二步是利用 System.IO.DirectoryInfo 類的 GetFiles() 方法,遍歷這些子目錄(包括根目錄)下的所有文件。
原理就是這樣,代碼可參考:寬純穗http://blog.csdn.net/aspnet2002web/article/details/6087476 這篇博文。
Ⅲ vb 獲得一個文件夾下所有子文件夾名,並全部添加到組合框里,只要文件名,不要全部路徑
添加一個Dirlistbox,Textbox,一個按鈕,一個組合框...
按鈕的簡談單擊攔彎碰事件如下
Private Sub Command1_Click()
Dim i As Integer
Dim b As Integer
Dim a As String
a = Text1.Text
Dir1.Path = a
b = Len(a)
Combo1.Clear
For i = 0 To Dir1.ListCount - 1
Combo1.AddItem (Mid(Dir1.List(i), b + 1))
Next
End Sub
運行時候,在鬧返TextBox里輸入文件路徑,如 C:\windows\ 這里路徑後面把"\"要加上,然後,單擊按鈕,再查看組合框,就可以看到windows目錄下了。。。
以上程序在我的機子(XP,SP2版,VB6.0企業版)上測試通過
Ⅳ vb treeview 顯示指定文件夾、子文件夾及文件名 逐級顯示出來 請高手指教
vb.net。
通過指定文件夾的路徑,獲取子文件夾My.Computer.FileSystem.GetDirectories
通過指定文件夾的路徑,獲取文件名My.Computer.FileSystem.GetFiles。
將不包括路徑的文件名和文件夾名增加到treeview中。
TreeView1.Nodes.Add增加一級節點
TreeView1.Nodes(0).Nodes.Add增加第一個一級節點的子節點
TreeView1.SelectedNode.Nodes.Add(「」)增加當歷喚前選定節點的子節點
TreeView1.SelectedNode.FullPath返肢運凱回當前選定節點的路徑。
要代碼可追問,問題再詳細具悄脊體些吧。你是要做資源管理器類型的?文件夾和文件都顯示在treeview中?
Ⅳ 如何用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 按鈕 就行了 有疑問再聯系我!!!!!這個 只是 文件路徑 你看看 是你想要的話 我再給你寫 提取文件夾和文件名的代碼!!!!
Ⅵ 如何用VB自動生成Folder文件夾並在文件夾內添加固定模式的子文件夾
先在工局坦程裡面引用microsoft scripting runtime,然桐基桐後鋒毀編寫代碼
Dim fso As New FileSystemObject
Private Sub Command1_Click()
Dim s$, i%
s = "c:"
For i = 1 To 5
fso.CreateFolder s + "\folder " + CStr(i)
s = s + "\folder " + CStr(i)
Next
End Sub
Ⅶ 謝謝,又來個問題,vb6中怎麼得到一個文件夾下面的所有文件夾的名稱
'fvFolderPath表枯睜判示所給早碼文件夾
Public Sub GetSubDirs(ByRef fvFolderPath As String)
Dim oFso As New FileSystemObject
Dim oParentFolder As Folder
Dim oFolders As Folders
Dim oFolder As Folder
Dim vFolderName As String
Dim oCollection As New VBA.Collection
Set oParentFolder = oFso.GetFolder(fvFolderPath)
『得到所給文件夾的子文件夾集合
Set oFolders = oParentFolder.SubFolders
』循環,得到每個沒改子文件夾名稱,並寫入ocollection集合中
For Each oFolder In oFolders
vFolderName = oFolder.Name
Call oCollection.Add(vFolderName)
Debug.Print vFolderName
Next
End Sub