小毛
Lv 5
小毛 asked in 電腦與網際網路程式設計 · 1 decade ago

vb列出某地方的全部(包含子孫)資料夾

請問一下 VB該如何做到

列出某地方的全部(包含子孫)資料夾

子孫都要做到喔...

舉例輸入C:\

C:\1

C:\1\2

C:\2

C:\2\3\4

C:\2\3\5

要可以列出

應該是遞回的方式下去寫?

方式不限 只要能列出所有的清單

我也試過用DOS

DOS的

Dir

DOS的 tree

...

等等各種 方法似乎 不太容易解決

懇請大家幫忙想想 目的是做出

隨身碟隱形檔案解除

以下為參考 僅僅差了 一個 就是列出資料夾

//================================

Private

Sub Command1_Click()

CommonDialog1.ShowOpen

aaa

End Sub

Private

Sub Command2_Click()

Shell ("cmd.exe /c attrib -r -a -s -h " &

Chr(34) & Text2 & Chr(34))

aaa

End Sub

Private Sub

Command3_Click()

For i = 0 To List1.ListCount - 1

Shell

("cmd.exe /c attrib -r -a -s -h " & Chr(34) & List1.List(i)

& Chr(34))

aaa

Next i

End Sub

Private Sub

Command4_Click()

List1.RemoveItem (List1.ListIndex)

End Sub

Private

Sub Form_Unload(Cancel As Integer)

If Dir("c:\dir.txt") <> "" Then Kill

"c:\dir.txt"

End Sub

Private Sub List1_Click()

Text2 =

Replace(List1.List(List1.ListIndex), Chr(8), "")

End Sub

Public

Sub aaa()

If Dir("c:\dir.txt") <> "" Then Kill "c:\dir.txt"

List1.Clear

delay

1

Text1 = Mid(CommonDialog1.FileName, 1, Len(CommonDialog1.FileName)

- Len(CommonDialog1.FileTitle))

If Text1 = "" Then Exit Sub

Shell

("cmd.exe /c dir " & Chr(34) & Text1 & Chr(34) & " /a:s

> c:\dir.txt")

delay 1

讀入 "c:\dir.txt", t

a =

Split(t, ";")

For i = 5 To UBound(a) - 2

If InStr(a(i),

"<DIR>") Then

b = Split(a(i), "<DIR>")

List1.AddItem LTrim(b(1))

End If

Next i

4 Answers

Rating
  • W.J.S.
    Lv 7
    1 decade ago
    Favorite Answer

    Dim fs As Object, fd As Object, sFd As Object, f As Object

    Private Sub Command1_Click()

    Dim s As String

    s = "C:\Test\"

    Set fs = CreateObject("Scripting.FileSystemObject")

    Debug.Print s

    EnumFolder s

    Set f = Nothing

    Set sFd = Nothing

    Set fd = Nothing

    Set fs = Nothing

    End Sub

    Sub EnumFolder(ByVal nPath As String)

    Set fd = fs.GetFolder(nPath)

    For Each f In fd.Files '列舉檔案

      Debug.Print " "; f.Path

    Next

    For Each sFd In fd.SubFolders '列舉子資料夾

      Debug.Print sFd.Path; "\"

      EnumFolder sFd.Path

    Next

    End Sub

    2010-05-04 15:41:22 補充:

    ㄚ旺大大:

    您過獎了,這是從書本裡學的,受之有愧XD!

  • 小毛
    Lv 5
    1 decade ago

    WJS大大 謝謝嚕

    寫好了

    使用上請小心 因為他會把 所有的 隱藏.系統 資料夾抓回

    http://www.wretch.cc/blog/babyxkimo/14963355

  • ㄚ旺
    Lv 5
    1 decade ago

    用遞迴寫比較簡單....另外就是.套用現成的磁碟檔案管理元件.

    2010-05-04 14:43:47 補充:

    W.J.S大大.

    For Each sFd In fd.SubFolders 這一招還是第一次看到.利害.+2點

  • Wu
    Lv 5
    1 decade ago

    所示的程序非常糟糕。重寫更好

Still have questions? Get your answers by asking now.