Go Back   Forum Care Forums > Development Reference Area > Visual Basic

Reply
 
LinkBack Thread Tools Display Modes
Dir Function Problems
Old
  (#1)
Pat
Guest
 
Posts: n/a
Default Dir Function Problems - 06-04-2007, 09:46 AM

Hi,

I'm pulling my hair out here over this one...

The following line of code:

Msgbox Dir("c:\test\", vbDirectory)

Is returning the names of files in the directory instead of other directories.

Any ideas?

Thanks
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: Dir Function Problems
Old
  (#2)
Eddie B
Guest
 
Posts: n/a
Default Re: Dir Function Problems - 06-04-2007, 09:46 AM

The Dir function will only check to see if the folder exists. To list
the folders within a folder, try this...

Dim oFso As Object
Dim oFolder
Dim oSubFolder

Set oFso = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFso.getfolder("c:\test\")

For Each oSubFolder In oFolder.subfolders
List1.AddItem oSubFolder.Name
Next

This will only go 1 level deep, so if you need to go deeper, you will
have to modify the code.


On 16 Aug 2004 18:38:04 -0700, EMAIL REMOVED (Pat) wrote:

>Hi,
>
>I'm pulling my hair out here over this one...
>
>The following line of code:
>
>Msgbox Dir("c:\test\", vbDirectory)
>
>Is returning the names of files in the directory instead of other directories.
>
>Any ideas?
>
>Thanks


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Dir Function Problems
Old
  (#3)
Eddie B
Guest
 
Posts: n/a
Default Re: Dir Function Problems - 06-04-2007, 09:46 AM

This may also help...

How to find all the files and sub-folders under a folder
faq222-24

If you've loaded the VB Sample programs, look at Winseek.vpb - it
contains a subroutine "DirDriver" which recursively works its way
through all the levels of sub-folder below the start folder and
returns all the file and folder names

On 16 Aug 2004 18:38:04 -0700, EMAIL REMOVED (Pat) wrote:

>Hi,
>
>I'm pulling my hair out here over this one...
>
>The following line of code:
>
>Msgbox Dir("c:\test\", vbDirectory)
>
>Is returning the names of files in the directory instead of other directories.
>
>Any ideas?
>
>Thanks


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Dir Function Problems
Old
  (#4)
J French
Guest
 
Posts: n/a
Default Re: Dir Function Problems - 06-04-2007, 09:46 AM

On 16 Aug 2004 18:38:04 -0700, EMAIL REMOVED (Pat) wrote:

>Hi,
>
>I'm pulling my hair out here over this one...
>
>The following line of code:
>
>Msgbox Dir("c:\test\", vbDirectory)
>
>Is returning the names of files in the directory instead of other directories.


It returns Files /as well as/ Directories

Here is the example from the Help file :-

' Display the names in C:\ that represent directories.
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 encomp***ing 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
End If ' it represents a directory.
End If
MyName = Dir ' Get next entry.
Loop

Ignore that nonsense about the FSO - it is a crock of sch**t
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Dir Function Problems
Old
  (#5)
Eddie B
Guest
 
Posts: n/a
Default Re: Dir Function Problems - 06-04-2007, 09:46 AM

Yes, you can get a list of files and directories with Dir, but you
have much more control with FSO.

This is from Microsoft:
The FileSystemObject cl*** gives better performance than using such
Visual Basic intrinsic functions as Dir and GetAttr, and is much
simpler to implement.

In addition, you can get a lot of information using the FSO, as in
another Microsoft example...

Sub ShowFolderInfo()
Dim fso, fldr, s
' Get instance of FileSystemObject.
Set fso = CreateObject("Scripting.FileSystemObject")
' Get Drive object.
Set fldr = fso.GetFolder("c:")
' Print parent folder name.
MsgBox ("Parent folder name is: " & fldr)
' Print drive name.
MsgBox ("Contained on drive " & fldr.Drive)
' Print root file name.
If fldr.IsRootFolder = True Then
MsgBox ("This is the root folder.")
Else
MsgBox ("This folder isn't a root folder.")
End If
' Create a new folder with the FileSystemObject object.
fso.CreateFolder ("C:\Bogus")
MsgBox ("Created folder C:\Bogus")
' Print the base name of the folder.
MsgBox ("Basename = " & fso.GetBaseName("c:\bogus"))
' Delete the newly created folder.
fso.DeleteFolder ("C:\Bogus")
MsgBox ("Deleted folder C:\Bogus")

End Sub


Do some research before you condem the FSO, then if you don't like it,
don;t use it.

J French, why the big hang up?

-Eddie

On Tue, 17 Aug 2004 06:45:16 +0000 (UTC), EMAIL REMOVED (J
French) wrote:

>Ignore that nonsense about the FSO - it is a crock of sch**t


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Dir Function Problems
Old
  (#6)
J French
Guest
 
Posts: n/a
Default Re: Dir Function Problems - 06-04-2007, 09:46 AM

On Tue, 17 Aug 2004 20:07:43 -0400, Eddie B <> wrote:

>Yes, you can get a list of files and directories with Dir, but you
>have much more control with FSO.
>
>This is from Microsoft:
>The FileSystemObject cl*** gives better performance than using such
>Visual Basic intrinsic functions as Dir and GetAttr, and is much
>simpler to implement.


If you believe everything that MS says, then you are in for a hard
time.

Actually the most efficient way of scanning directories is to use the
FindFirstFile, FindNextFile APIs directly
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Dir Function Problems
Old
  (#7)
Rick Rothstein
Guest
 
Posts: n/a
Default Re: Dir Function Problems - 06-04-2007, 09:46 AM

> Yes, you can get a list of files and directories with Dir,
> but you have much more control with FSO.


That is not true, per se. If all you are comparing it to is Dir, you
might be right; but you have far more control using straight VB and some
API functions than an add-on scripting interpretter will ever give you.
The FSO **might** give you a way of using less lines of code for
**some** of its functionality, but that small gain comes at a price. The
FSO is slow (contrary to your quoted section from Microsoft), adds
overhead, requires an extra DLL in distribution, can exist in multiple
non-compatible versions on user systems, can be disabled on a computer
as part of anti-virus security measures (that last one is a biggie) and,
to reiterate, offers nothing that can't be done with plain VB statements
and/or a couple of API calls.

> This is from Microsoft:
> The FileSystemObject cl*** gives better performance
> than using such Visual Basic intrinsic functions as Dir
> and GetAttr, and is much simpler to implement.


By the way, where did you get this quote from? I just did a search on
all of Microsoft's sites and came up with no match for it.

> Do some research before you condem the FSO, then
> if you don't like it, don;t use it.


Many of us have tried it and found it lacking for just those reasons I
listed above.

Rick - MVP

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Dir Function Problems
Old
  (#8)
Eddie B
Guest
 
Posts: n/a
Default Re: Dir Function Problems - 06-04-2007, 09:46 AM

I am not going to argue about the advantages of the API, I totally
agree, but I have always had better results with the FSO than with
Dir. I guess that is why it's good that more than one person answers
these posts.

>By the way, where did you get this quote from? I just did a search on
>all of Microsoft's sites and came up with no match for it.


HOWTO: Recursively Search Directories Using FileSystemObject
Q185601
http://support.microsoft.com/default...n-us%3Bq185601

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Dir Function Problems
Old
  (#9)
Rick Rothstein
Guest
 
Posts: n/a
Default Re: Dir Function Problems - 06-04-2007, 09:46 AM

> I am not going to argue about the advantages of the API, I totally
> agree, but I have always had better results with the FSO than with
> Dir. I guess that is why it's good that more than one person answers
> these posts.


No question about that last statement... I think you will find that most
of the volunteers are happy to see the different ways in which problems
can be solved presented. True, we have our own prejudices regarding some
of the possible approaches, and sometimes we will express them
(sometimes you need thick skin to survive here<g>), but presenting the
variety of approaches is still a good thing overall.

> >By the way, where did you get this quote from? I just did a search on
> >all of Microsoft's sites and came up with no match for it.

>
> HOWTO: Recursively Search Directories Using FileSystemObject
> Q185601
> http://support.microsoft.com/default...n-us%3Bq185601


Thanks for the citation. I'm a little surprised Microsoft's search
engine didn't report that one back to me.

Rick - MVP

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top