-
May 13th, 2003, 11:24 AM
#1
VB - List all subfolders in a specific folder
Add a reference to the "Microsoft Scripting Runtime":
VB Code:
Private Sub Command1_Click()
Dim strStartPath As String
strStartPath = "C:\" 'ENTER YOUR START FOLDER HERE
ListFolder strStartPath
End Sub
Private Sub ListFolder(sFolderPath As String)
Dim FS As New FileSystemObject
Dim FSfolder As Folder
Dim subfolder As Folder
Dim i As Integer
Set FSfolder = FS.GetFolder(sFolderPath)
For Each subfolder In FSfolder.SubFolders
DoEvents
i = i + 1
Debug.Print subfolder
Next subfolder
Set FSfolder = Nothing
MsgBox "Total sub folders in " & sFolderPath & " : " & i
End Sub
Last edited by manavo11; May 13th, 2003 at 03:41 PM.
Has someone helped you? Then you can Rate their helpful post.
-
Sep 15th, 2003, 08:42 AM
#2
Fanatic Member
VB Code:
Dim Mypath as string, MyName as String,iCount as integer
iCount=0
MyPath = "c:\" ' Set the path.
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
Do While MyName <> "" ' Start the loop.
' Ignore the current directory and the encompassing directory.
If MyName <> "." And MyName <> ".." Then
' Use bitwise comparison to make sure MyName is a directory.
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
Debug.Print MyName ' Display entry only if it
iCount=iCount+1
End If ' it represents a directory.
End If
MyName = Dir ' Get next entry.
Loop
debug.print "No.of Folders in the selected path : " & iCount
Last edited by faisalkm; Sep 15th, 2003 at 09:15 AM.
Faisal Muhammed
Homepage:I Started making it in 1994 ...Still Under Construction
Using
Visual Basic 6.0 Enterprise SP5
Embedded Visual Basic 3.0
SQL Server 2000
Windows 2000 Proff
Delphi 6.0
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
-
Sep 15th, 2003, 08:50 AM
#3
-
Sep 15th, 2003, 09:02 AM
#4
Fanatic Member
-
Sep 15th, 2003, 09:10 AM
#5
-
Aug 22nd, 2004, 01:59 PM
#6
Lively Member
manavo11, how would you code it to find out if the directory exists or not, other then using an error handler? I tried using some of
faisalkm's code, but it didnt work.
-
Aug 22nd, 2004, 04:11 PM
#7
To check if a dir exists use this :
VB Code:
Private Sub Form_Load()
MsgBox DirExists("C:\windows")
MsgBox DirExists("C:\testing")
End Sub
Private Function DirExists(sDir As String) As Boolean
If Len(Dir$(sDir, vbDirectory)) Then
DirExists = True
End If
End Function
Has someone helped you? Then you can Rate their helpful post.
-
Aug 25th, 2004, 04:00 PM
#8
Addicted Member
A real way:
VB Code:
Dim FS as Object
Set FS = CreateObject("Scripting.FileSystemObject")
If FS.FileExists("C:\myfile.txt") Then
'It exists!
End If
-
Aug 25th, 2004, 04:14 PM
#9
Originally posted by xjake88x
A real way:
VB Code:
Dim FS as Object
Set FS = CreateObject("Scripting.FileSystemObject")
If FS.FileExists("C:\myfile.txt") Then
'It exists!
End If
Ack...using FSO and its late bound? Bad medicine.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Aug 25th, 2004, 05:50 PM
#10
Originally posted by crptcblade
Ack...using FSO and its late bound? Bad medicine.
Plus it should be like this :
VB Code:
If FS.FolderExists("C:\some dir") Then
since the question regarded folder and not file existing...
Has someone helped you? Then you can Rate their helpful post.
-
Aug 2nd, 2005, 02:52 PM
#11
New Member
Re: VB - List all subfolders in a specific folder
VB Code:
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
Set fo = fs.GetFolder("c:\temp\)
For Each x In fo.SubFolders
Debug.Print x.Name
Next
Last edited by svOi; Aug 2nd, 2005 at 02:58 PM.
-
Feb 22nd, 2008, 01:09 PM
#12
Re: VB - List all subfolders in a specific folder
Another way
vb Code:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Const LB_DIR = &H18D
Private Const DDL_DIRECTORY = &H10
Private Const DDL_ARCHIVE = &H20
Private Const DDL_EXCLUSIVE = &H8000
Private Sub Command1_Click()
List1.Clear
SendMessage List1.hwnd, LB_DIR, DDL_EXCLUSIVE Or DDL_DIRECTORY, ByVal "C:\Windows\*.*"
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|