473,385 Members | 1,468 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,385 software developers and data experts.

Files in (sub)sudirectory

IS
Hi,

I am trying to list all the files in a directory and add them to a
collection. As well, I am trying to add all the files to this collection
that might be in subdirectories (if exist).

Any hints to how I do this? With Dir$ and such, I can find only single files
and directories. I guess I should be using API functions I am not aware of.

Thanks for your help,
IS
Jul 17 '05 #1
5 2981
"IS" <fi*******@yahoo.com> wrote in message news:<bs**********@shiva.neobee.net>...
Hi,

I am trying to list all the files in a directory and add them to a
collection. As well, I am trying to add all the files to this collection
that might be in subdirectories (if exist).

Any hints to how I do this? With Dir$ and such, I can find only single files
and directories. I guess I should be using API functions I am not aware of.


you can use Dir$, just use the optional vbDirectory flag and test
every file you get back using GetAttr. If it is a directory then save
the path in a collection or other list. When Dir$ indicates no more
check your list of directories and remove one to be processed next. a
simple example:

Dim sDir As String ' current directory
Dim sDirsLeft As String ' directories to be processed
Dim sFile As String ' current file
Dim x As Integer ' scratch
' Note: the starting directory path MUST end with a \ character
sDirsLeft = "C:\" & vbNullChar ' start with this directory
Do While Len(sDirsLeft) ' any directories left?
x = InStr(sDirsLeft, vbNullChar) ' get delimiter
sDir = Left$(sDirsLeft, x - 1) ' peel off directory
sDirsLeft = Mid$(sDirsLeft, x + 1) ' remove from to-do list
sFile = Dir$(sDir & "*.*", vbDirectory) ' get first file
Do While Len(sFile) ' any files left?
If sFile <> "." And sFile <> ".." Then ' skip self-refs
If GetAttr(sDir & sFile) And vbDirectory Then ' is it a
directory?
' yes -- add to to-do-list
sDirsLeft = sDir & sFile & "\" & vbNullChar & sDirsLeft
Else
If sFile Like "*.TXT" Then ' do we want this file?
Debug.Print sDir & sFile ' process this file
End If
End If
End If
sFile = Dir$ ' get next file
Loop
DoEvents ' just to be nice!
Loop

A better option is to use the FindFirstFile/FindNextFile/FindClose API
calls. I don't have an example handy but you can find samples at
http://groups.google.com

If you want the slowest possible method using the most overhead and a
chance of it failing on systems randomly then use the FileSystemObject
Jul 17 '05 #2

"Bob Butler" <bu*******@earthlink.net> wrote in message
news:fa*************************@posting.google.co m...

If you want the slowest possible method using the most overhead and a
chance of it failing on systems randomly then use the FileSystemObject


Three things to look for in every solution - if you are an IT executive,
a government bureaucrat, or work for Microsoft. :)
Jul 17 '05 #3
Hey ... I resemble that ... I'm a government bureaucrat. <g>

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.
"Steve Gerrard" <no*************@comcast.net> wrote in message
news:Fd********************@comcast.com...
:
: "Bob Butler" <bu*******@earthlink.net> wrote in message
: news:fa*************************@posting.google.co m...
: >
: > If you want the slowest possible method using the most overhead and a
: > chance of it failing on systems randomly then use the FileSystemObject
:
: Three things to look for in every solution - if you are an IT executive,
: a government bureaucrat, or work for Microsoft. :)
:
:
Jul 17 '05 #4
Present company excluded, of course...

"Randy Birch" <rg************@mvps.org> wrote in message
news:_w********************@twister01.bloor.is.net .cable.rogers.com...
Hey ... I resemble that ... I'm a government bureaucrat. <g>

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.
"Steve Gerrard" <no*************@comcast.net> wrote in message
news:Fd********************@comcast.com...
:
: "Bob Butler" <bu*******@earthlink.net> wrote in message
: news:fa*************************@posting.google.co m...
: >
: > If you want the slowest possible method using the most overhead and a : > chance of it failing on systems randomly then use the FileSystemObject :
: Three things to look for in every solution - if you are an IT executive, : a government bureaucrat, or work for Microsoft. :)
:
:

Jul 17 '05 #5
IS
Exactly what I needed!
Thank you, Bob!
Jul 17 '05 #6

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

Similar topics

2
by: m1eyp | last post by:
I have a revision question bank on Excel. Two of these questions (160 & 161) have accompanying pictures, that I want to appear if either question is randomly selected. These images have filenames...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.