Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2005

Tip: Looking for answers? Try searching our database.

The Dir$() function and searching files under a directory

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
andywirtanen@gmail.com - 19 Oct 2005 19:08 GMT
Hello,

I was wondering if it was possible to search filenames under a
directory using Dir$() or if I would have to use If and Instr
statements to search individual filenames.

In other words, I have a directory, in which there are a bunch of files
with filenames that all begin with the same pattern. I want to grab
only those
files and not have to search the entire directory.

Here's what I tried:

While Dir$("C:\theDirectory\" & theSearchTerm & "*.*") <> ""
' Do stuff
Wend

Thanks,

Andy
andywirtanen@gmail.com - 19 Oct 2005 21:12 GMT
Nevermind, that works.

> Hello,
>
[quoted text clipped - 16 lines]
>
> Andy
Karl E. Peterson - 19 Oct 2005 22:34 GMT
>> Here's what I tried:
>>
[quoted text clipped - 3 lines]
>
> Nevermind, that works.

Be very careful with that "Do stuff" part, though!  Most folks would agree (for a
variety of hard-earned knocks) that the best approach is to first build an array of
filenames you want to operate on, then iterate the array.

Also, looking at what you're doing, that's gonna cause issues, anyway.  Cleaning it
up a bit, you'd really want something more like:

 Dim f As String

 f = Dir$("C:\theDirectory\" & theSearchTerm & "*.*")
 Do While Len(f)
   ' Stuff
   f = Dir$()
 Loop

Later...   Karl
Signature

Working Without a .NET?
http://classicvb.org/petition

Jean-Guy Marcil - 19 Oct 2005 22:50 GMT
Karl E. Peterson was telling us:
Karl E. Peterson nous racontait que :

>>> Here's what I tried:
>>>
[quoted text clipped - 8 lines]
> to first build an array of filenames you want to operate on, then
> iterate the array.

Yeah, been there.
I once had  a "do stuff" that ended up processing each file twice... Not
what I wanted!

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org 

Jezebel - 20 Oct 2005 00:33 GMT
The other trap is is trying to use Dir$() recursively, to process the
sub-folders. oh dear ...

> Karl E. Peterson was telling us:
> Karl E. Peterson nous racontait que :
[quoted text clipped - 15 lines]
> I once had  a "do stuff" that ended up processing each file twice... Not
> what I wanted!
Karl E. Peterson - 20 Oct 2005 01:20 GMT
> The other trap is is trying to use Dir$() recursively, to process the
> sub-folders. oh dear ...

That _can_ be nasty, if the recursive call is part of "do stuff", yeah.

But(!), if you understand how Dir works, you most certainly can use it recusively.

See: http://vb.mvps.org/samples/DirDrill
Signature

Working Without a .NET?
http://classicvb.org/petition

Jonathan West - 20 Oct 2005 10:07 GMT
Hi Andy,

For the various reason referred to in this thread, you might want to
consider not using Dir() at all for this. You might want to take a look at
the following article for an alternative approach

http://vbnet.mvps.org/code/fileapi/recursivefiles_minimal.htm

The code is VB6 rather than VBA, but will only require minimal modification
to work in a VBA UserForm instead of a VBA form.

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org

> Hello,
>
[quoted text clipped - 16 lines]
>
> Andy
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.