Dir Function Problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pat

    #1

    Dir Function Problems

    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
  • Eddie B

    #2
    Re: Dir Function Problems

    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("S cripting.FileSy stemObject")
    Set oFolder = oFso.getfolder( "c:\test\")

    For Each oSubFolder In oFolder.subfold ers
    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, patkay_156@yaho o.co.uk (Pat) wrote:
    [color=blue]
    >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[/color]

    Comment

    • Eddie B

      #3
      Re: Dir Function Problems

      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, patkay_156@yaho o.co.uk (Pat) wrote:
      [color=blue]
      >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[/color]

      Comment

      • J French

        #4
        Re: Dir Function Problems

        On 16 Aug 2004 18:38:04 -0700, patkay_156@yaho o.co.uk (Pat) wrote:
        [color=blue]
        >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.[/color]

        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 encompassing 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

        Comment

        • Eddie B

          #5
          Re: Dir Function Problems

          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 FileSystemObjec t class 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 FileSystemObjec t.
          Set fso = CreateObject("S cripting.FileSy stemObject")
          ' 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.IsRootFold er = 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 FileSystemObjec t object.
          fso.CreateFolde r ("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.DeleteFolde r ("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), erewhon@nowhere .com (J
          French) wrote:
          [color=blue]
          >Ignore that nonsense about the FSO - it is a crock of sch**t[/color]

          Comment

          • J French

            #6
            Re: Dir Function Problems

            On Tue, 17 Aug 2004 20:07:43 -0400, Eddie B <> wrote:
            [color=blue]
            >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 FileSystemObjec t class gives better performance than using such
            >Visual Basic intrinsic functions as Dir and GetAttr, and is much
            >simpler to implement.[/color]

            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

            Comment

            • Rick Rothstein

              #7
              Re: Dir Function Problems

              > Yes, you can get a list of files and directories with Dir,[color=blue]
              > but you have much more control with FSO.[/color]

              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.
              [color=blue]
              > This is from Microsoft:
              > The FileSystemObjec t class gives better performance
              > than using such Visual Basic intrinsic functions as Dir
              > and GetAttr, and is much simpler to implement.[/color]

              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.
              [color=blue]
              > Do some research before you condem the FSO, then
              > if you don't like it, don;t use it.[/color]

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

              Rick - MVP

              Comment

              • Eddie B

                #8
                Re: Dir Function Problems

                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.
                [color=blue]
                >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.[/color]

                HOWTO: Recursively Search Directories Using FileSystemObjec t
                Q185601


                Comment

                • Rick Rothstein

                  #9
                  Re: Dir Function Problems

                  > I am not going to argue about the advantages of the API, I totally[color=blue]
                  > 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.[/color]

                  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.
                  [color=blue][color=green]
                  > >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.[/color]
                  >
                  > HOWTO: Recursively Search Directories Using FileSystemObjec t
                  > Q185601
                  > http://support.microsoft.com/default...n-us%3Bq185601[/color]

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

                  Rick - MVP

                  Comment

                  Working...