ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Opening subfolders in sequence (https://www.excelbanter.com/excel-programming/425202-opening-subfolders-sequence.html)

Brettjg

Opening subfolders in sequence
 
Let's say I have a main folder with a path of
C:\2. SETTLED CLIENTS\

I want to loop through all the sub folders within it to extract information
from one file within each folder. I know the code for extracting the info and
opening the file.

Each subfolder is of the name "Surname, initial"

What I don't know is the code to open each subfolder in sequence, without
using a list of names.
Regards, Brett

joel

Opening subfolders in sequence
 
Once you got yher foler then this

for each fld in folders
for each fname in fld.files

next fname

next fld


If you add a watch into your VBA window for fld you can see the properties
like files.

Add the watch by highlighting any variable in your code and then right click
and select Watch. Put a break point in your code by click on a line of VBA
code and presssing F9. Step through the code by pressing F8.

"Brettjg" wrote:

Let's say I have a main folder with a path of
C:\2. SETTLED CLIENTS\

I want to loop through all the sub folders within it to extract information
from one file within each folder. I know the code for extracting the info and
opening the file.

Each subfolder is of the name "Surname, initial"

What I don't know is the code to open each subfolder in sequence, without
using a list of names.
Regards, Brett


Brettjg

Opening subfolders in sequence
 
Ok, thanks Joel, I'll do some testing on that. Brett

"Brettjg" wrote:

Let's say I have a main folder with a path of
C:\2. SETTLED CLIENTS\

I want to loop through all the sub folders within it to extract information
from one file within each folder. I know the code for extracting the info and
opening the file.

Each subfolder is of the name "Surname, initial"

What I don't know is the code to open each subfolder in sequence, without
using a list of names.
Regards, Brett


OssieMac

Opening subfolders in sequence
 
Not sure how much you are looking for here Brett. I am assuming that all of
these directors are simply attached to one directory and not sub directory of
subdirectory.

The following is extracted and modified a bit from Help in xl2002 just to
identify one level of sub directories from a given directory.

Sub test()
Dim MyPath As String
Dim MyName As String

MyPath = "C:\2. SETTLED CLIENTS\"

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

If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
MsgBox MyName ' Display entry only if it is a directory
End If

End If

MyName = Dir ' Get next entry.

Loop

End Sub
--
Regards,

OssieMac


"Brettjg" wrote:

Let's say I have a main folder with a path of
C:\2. SETTLED CLIENTS\

I want to loop through all the sub folders within it to extract information
from one file within each folder. I know the code for extracting the info and
opening the file.

Each subfolder is of the name "Surname, initial"

What I don't know is the code to open each subfolder in sequence, without
using a list of names.
Regards, Brett


Brettjg

Opening subfolders in sequence
 
Hi Ossiemac, yes that works nicely. TVM, Brett (your assumption is correct)

"OssieMac" wrote:

Not sure how much you are looking for here Brett. I am assuming that all of
these directors are simply attached to one directory and not sub directory of
subdirectory.

The following is extracted and modified a bit from Help in xl2002 just to
identify one level of sub directories from a given directory.

Sub test()
Dim MyPath As String
Dim MyName As String

MyPath = "C:\2. SETTLED CLIENTS\"

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

If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
MsgBox MyName ' Display entry only if it is a directory
End If

End If

MyName = Dir ' Get next entry.

Loop

End Sub
--
Regards,

OssieMac


"Brettjg" wrote:

Let's say I have a main folder with a path of
C:\2. SETTLED CLIENTS\

I want to loop through all the sub folders within it to extract information
from one file within each folder. I know the code for extracting the info and
opening the file.

Each subfolder is of the name "Surname, initial"

What I don't know is the code to open each subfolder in sequence, without
using a list of names.
Regards, Brett



All times are GMT +1. The time now is 12:27 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com