473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Programatically Get A List Of Subfolders

Tom
How do you programatically get a list of subfolders in a folder?

Thanks!

Tom
Nov 13 '05 #1
9 9381
The short answer is to use the Microsoft Scripting Runtime.

The function below has the code you're looking for, but is probably more
than you were looking for. What I did was log each subfolder name to a
table. You could use a collection, which is what I did in this function
before I switched to the Logger function - which is simply a db.Execute
(INSERT....) command.

Public Function GetPath(strRoot As String, blnDig As Boolean) As Boolean
'Required Reference: Microsoft Scripting Runtime
On Error GoTo HandleErr
Dim strJobFolder As String
Dim strJobFile As String
Dim str1 As String
Dim str2 As String
Dim fso As Scripting.FileSystemObject
Dim fld As Scripting.folder
Dim fldSub As Scripting.folder
Set fso = New Scripting.FileSystemObject
If Not fso.FolderExists(strRoot) Then Err.Raise INVALID_PATH
Set fld = fso.GetFolder(strRoot)
If blnDig Then
'================================================= =====
' process multiple Job Collections
'================================================= =====
For Each fldSub In fld.SubFolders
strJobFolder = fldSub.Path
strJobFile = fso.BuildPath(fldSub.Path, "MEAS.MDB")
If fso.FileExists(strJobFile) Then
Call basHandler.Logger(strJobFile, fldSub.Name, , , , , 140)
Else
str1 = "No Meas.mdb found in " & fldSub.Name
str2 = fso.GetParentFolderName(fldSub)
Call basHandler.Logger(str1, str2, , , , , 40)
End If
Call GetPath(strJobFolder, blnDig)
Next fldSub
Else
'================================================= =====
' process Job Files in selected directory only
'================================================= =====
For Each fldSub In fld.SubFolders
strJobFolder = fso.GetAbsolutePathName(fldSub)
strJobFile = fso.BuildPath(strJobFolder, "MEAS.MDB")
If fso.FileExists(strJobFile) Then
Call basHandler.Logger(strJobFile, fldSub.Name, , , , , 140)
Else
str1 = "No Meas.mdb found in " & strJobFolder
str2 = fldSub.Name
Call basHandler.Logger(str1, str2, , , , , 40)
End If
Next fldSub
End If
GetPath = True
Exit_Here:
On Error Resume Next
Set fld = Nothing
Set fso = Nothing
Exit Function
HandleErr:
Select Case Err.Number
Case INVALID_PATH
MsgBox "Could not validate Path to Data Files.", vbCritical, "
Invalid Path"
Case Else
MsgBox Err.Description & vbCrLf & vbCrLf & "Error Number " &
Err.Number & _
" at basJobFilePath.GetPath", vbExclamation, " Unexpected
Error"
End Select
GetPath = False
Resume Exit_Here
End Function
Nov 13 '05 #2
Tom
Will the Dir function work?

Something like ....
Dir("C:\MySubfolder\",vbDirectory)

Tom
"deko" <de**@nospam.com> wrote in message
news:mv***************@newssvr13.news.prodigy.com. ..
The short answer is to use the Microsoft Scripting Runtime.

The function below has the code you're looking for, but is probably more
than you were looking for. What I did was log each subfolder name to a
table. You could use a collection, which is what I did in this function
before I switched to the Logger function - which is simply a db.Execute
(INSERT....) command.

Public Function GetPath(strRoot As String, blnDig As Boolean) As Boolean
'Required Reference: Microsoft Scripting Runtime
On Error GoTo HandleErr
Dim strJobFolder As String
Dim strJobFile As String
Dim str1 As String
Dim str2 As String
Dim fso As Scripting.FileSystemObject
Dim fld As Scripting.folder
Dim fldSub As Scripting.folder
Set fso = New Scripting.FileSystemObject
If Not fso.FolderExists(strRoot) Then Err.Raise INVALID_PATH
Set fld = fso.GetFolder(strRoot)
If blnDig Then
'================================================= =====
' process multiple Job Collections
'================================================= =====
For Each fldSub In fld.SubFolders
strJobFolder = fldSub.Path
strJobFile = fso.BuildPath(fldSub.Path, "MEAS.MDB")
If fso.FileExists(strJobFile) Then
Call basHandler.Logger(strJobFile, fldSub.Name, , , , , 140) Else
str1 = "No Meas.mdb found in " & fldSub.Name
str2 = fso.GetParentFolderName(fldSub)
Call basHandler.Logger(str1, str2, , , , , 40)
End If
Call GetPath(strJobFolder, blnDig)
Next fldSub
Else
'================================================= =====
' process Job Files in selected directory only
'================================================= =====
For Each fldSub In fld.SubFolders
strJobFolder = fso.GetAbsolutePathName(fldSub)
strJobFile = fso.BuildPath(strJobFolder, "MEAS.MDB")
If fso.FileExists(strJobFile) Then
Call basHandler.Logger(strJobFile, fldSub.Name, , , , , 140) Else
str1 = "No Meas.mdb found in " & strJobFolder
str2 = fldSub.Name
Call basHandler.Logger(str1, str2, , , , , 40)
End If
Next fldSub
End If
GetPath = True
Exit_Here:
On Error Resume Next
Set fld = Nothing
Set fso = Nothing
Exit Function
HandleErr:
Select Case Err.Number
Case INVALID_PATH
MsgBox "Could not validate Path to Data Files.", vbCritical, "
Invalid Path"
Case Else
MsgBox Err.Description & vbCrLf & vbCrLf & "Error Number " &
Err.Number & _
" at basJobFilePath.GetPath", vbExclamation, " Unexpected
Error"
End Select
GetPath = False
Resume Exit_Here
End Function

Nov 13 '05 #3
> Will the Dir function work?

But what if there are subdirectories in your subdirectory?
Nov 13 '05 #4
Tom
I meant to write this as ......
Dir("C:\MyFolder\",vbDirectory)

and find the subfolders in MyFolder. I wouldn't expect the function to list
subfolders in the subfolders.

Tom

"deko" <de**@nospam.com> wrote in message
news:OD**************@newssvr13.news.prodigy.com.. .
Will the Dir function work?


But what if there are subdirectories in your subdirectory?

Nov 13 '05 #5
>I meant to write this as ......
Dir("C:\MyFolder\",vbDirectory)

and find the subfolders in MyFolder. I wouldn't expect the function to
list
subfolders in the subfolders.


I would still use the Scripting Runtime. But if Dir works for you...
Nov 13 '05 #6
"Tom" wrote
Dir("C:\MyFolder\",vbDirectory)

and find the subfolders in MyFolder.
I wouldn't expect the function to list
subfolders in the subfolders.


Yes, this should do what you want. You have to use a looping structure, with
some care, to find all the folders in the folder hierarchy with Dir.

However, a caution about the File System Object -- it requires the Windows
Scripting Host to execute. Some company's System Administrators remove the
WSH because it can be used as a vehicle for "mischief" by viruses or
worms -- and, if that is the case, using FSO it will just cause an error.

Larry Linson
Microsoft Access MVP


Nov 13 '05 #7
Tom
When I substitute an actual folder name for "MyFolder" and run:
MsgBox Dir("C:\MyFolder\",vbDirectory)
It comes up blank. Why is this?

Thanks!
"Larry Linson" <bo*****@localhost.not> wrote in message
news:O3lAe.1982$Gk4.457@trnddc01...
"Tom" wrote
> Dir("C:\MyFolder\",vbDirectory)
>
> and find the subfolders in MyFolder.
> I wouldn't expect the function to list
> subfolders in the subfolders.
Yes, this should do what you want. You have to use a looping structure,

with some care, to find all the folders in the folder hierarchy with Dir.

However, a caution about the File System Object -- it requires the Windows
Scripting Host to execute. Some company's System Administrators remove the
WSH because it can be used as a vehicle for "mischief" by viruses or
worms -- and, if that is the case, using FSO it will just cause an error.

Larry Linson
Microsoft Access MVP

Nov 13 '05 #8
Tom
The above was not coming up blank after all. I was seeing "." and ".." for
the current and encompassing directories. The following codes returns all
the subfolders in MyFolder:
Dim Path As String
Dim PictureSubfolder As String
Path = "C:\Housing Survey Database\Housing Survey Pictures\"
PictureSubfolder = Dir(Path, vbDirectory)
Do While Len(PictureSubfolder) > 0
‘ Ignore the current and encompassing directories
If (PictureSubfolder <> ".") And (PictureSubfolder <> "..") Then
MsgBox PictureSubfolder
End If
PictureSubfolder = Dir()
Loop

Tom
"Tom" <no********@address.net> wrote in message
news:G9*****************@newsread1.news.atl.earthl ink.net...
When I substitute an actual folder name for "MyFolder" and run:
MsgBox Dir("C:\MyFolder\",vbDirectory)
It comes up blank. Why is this?

Thanks!
"Larry Linson" <bo*****@localhost.not> wrote in message
news:O3lAe.1982$Gk4.457@trnddc01...
"Tom" wrote
> Dir("C:\MyFolder\",vbDirectory)
>
> and find the subfolders in MyFolder.
> I wouldn't expect the function to list
> subfolders in the subfolders.


Yes, this should do what you want. You have to use a looping structure,

with
some care, to find all the folders in the folder hierarchy with Dir.

However, a caution about the File System Object -- it requires the Windows Scripting Host to execute. Some company's System Administrators remove the WSH because it can be used as a vehicle for "mischief" by viruses or
worms -- and, if that is the case, using FSO it will just cause an error.
Larry Linson
Microsoft Access MVP


Nov 13 '05 #9
deko wrote:
I meant to write this as ......
Dir("C:\MyFolder\",vbDirectory)

and find the subfolders in MyFolder. I wouldn't expect the function to
list
subfolders in the subfolders.

I would still use the Scripting Runtime. But if Dir works for you...


Dir works with a bit of recursion, it also doesn't get stopped by anti
virus programs :-)

--
[OO=00=OO]
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kris | last post by:
Question: How do you create an Installer program using the Package and Deployment Wizard provided by Visual Studio Pro 6.0 (SP5) to include subfolders and their contents. I understand how to...
0
by: CoreyMas | last post by:
Hello Everyone, I have been successful in creating a template column programatically using the examples provided in VS 2003. However I have not been able to programatically set the width of a...
3
by: TK | last post by:
Excuse me for multiple posting because I've posted this message to aspnet.security NG but have not got any response yet. I'm building an ASP.NET application works in Forms Authentication mode...
1
by: Tarren | last post by:
Hi: What would be a quick way to iterate through a folder and all of its subfolders, not just its immediate folders? Is there a way to use the GetDirectories method and have it cascade through...
9
by: Bill Nguyen | last post by:
I need a VB routine to loop thru a select top folder to find all subfolders and list all subfolders/files under each of these subfolders. Any help is greatly appreciated. Bill
4
by: AAJ | last post by:
Hi all Is it possible to programatically list all the .aspx files within the currenty running website. e.g. something like foreach {aspxfile in AllaspxFilesInMyProject) { string filename =...
1
by: shank | last post by:
I borrowed the below code from http://www.brainjar.com/asp/dirlist/ and cannot get it to read my folder files. I don't get any errors or any output to screen. I have the below folder permissions...
0
by: grant | last post by:
Has anyone got a sample db that has a form to list an Outlook inbox and subfolders? I want to be able to mimic the tree style list of folders in Outlook so the user can browse the contents on...
2
by: pbrown | last post by:
Hey All, My problem appears to be pretty simple. I need a way of listing all the folders that exist in a certain directory. By all the folders, I mean ALL the folders; any file folder that exists...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.