|
当前文件夹内有若干个子文件夹,每个子文件夹内有不等个文件,求各位大侠赐VB代码,把文件夹内的子文件夹名显示在一级目录里,打开一级目录,显示每个文件夹内指定格式的文件名
本帖最后由 hwc2ycy 于 2013-2-19 14:33 编辑
改了下。 - Option Explicit
- Private Sub CommandButton1_Click()
- Dim Path$
- Me.TreeView1.Visible = False
- With Application.FileDialog(msoFileDialogFolderPicker)
- .AllowMultiSelect = False
- .InitialFileName = ThisWorkbook.Path
- .Show
- If .SelectedItems.Count = 1 Then Path = .SelectedItems(1)
- End With
- If Len(Path) Then
- With Me.TreeView1
- .Visible = False
- .Nodes.Clear
- .ImageList = Me.ImageList1
- Call ListDir(Path)
- .Visible = True
- End With
- End If
- End Sub
- Sub ListDir(ByVal Path As String)
- Dim filename$, root As Node
- Dim arrPath()
- Dim sPath$
- Dim i&, j&
- i = 1
- j = 1
-
- ReDim arrPath(1 To 1)
- arrPath(i) = Path & Application.PathSeparator
- 'On Error Resume Next
-
- With Me.TreeView1
- sPath = arrPath(j)
- Debug.Print sPath
- Set root = .Nodes.Add(, , sPath, sPath, 1)
- Do While Len(sPath)
- filename = Dir(sPath & "*.*", vbDirectory + vbNormal)
- Do While Len(filename)
-
- If Not (filename = "." Or filename = "..") Then
- If (GetAttr(sPath & "" & filename) And vbDirectory) = 16 Then
- '避免读取错误
- If Err.Number <> 0 Then Err.Clear: GoTo End1If
- .Nodes.Add sPath, 4, sPath & filename & Application.PathSeparator, filename, 1
- i = i + 1
- ReDim Preserve arrPath(1 To i)
- arrPath(i) = sPath & filename & Application.PathSeparator
- Else
- .Nodes.Add sPath, 4, sPath & filename, filename, 2
- End If
- End If
- End1If:
- filename = Dir
- Loop
- j = j + 1
- If j > i Then Exit Do
- sPath = arrPath(j)
- Loop
- End With
- End Sub
- Private Sub TreeView1_BeforeLabelEdit(Cancel As Integer)
- Cancel = True
- End Sub
复制代码
|
|