+ Reply to Thread
Results 1 to 21 of 21

Dir

  1. #1
    greasybeano
    Guest

    Dir

    I have taken the following code from the Dir help file which works ok at
    first level.
    However, I need to search for all the associated subdirectories but not too
    sure how to adapt the code. could someone be kind enough to assist please?
    Many thanks.
    --
    GB

    Sub Finddir()
    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 ' it represents
    a directory.
    End If
    End If
    myname = Dir ' Get next entry.
    Loop
    End Sub

  2. #2
    pascalv@sdf.lonestar.org
    Guest

    Re: Dir

    On Sat, 27 May 2006, greasybeano wrote:

    > I have taken the following code from the Dir help file which works ok at
    > first level.
    > However, I need to search for all the associated subdirectories but not too
    > sure how to adapt the code. could someone be kind enough to assist please?


    You may be interested by Application.FileSearch
    and fs.GetFolder(folderName)
    where fs is a FileSystemObject

    http://groups.google.com/group/micro...183f3d256617ef

  3. #3
    Ron de Bruin
    Guest

    Re: Dir

    See
    http://www.rondebruin.nl/copy3.htm

    And then click on this link
    http://www.rondebruin.nl/fso.htm

    --
    Regards Ron de Bruin
    http://www.rondebruin.nl


    "greasybeano" <nospam@farnbro.freeserve.co.uk> wrote in message news:048F5407-2EB3-425A-A9E0-891E7C1F4690@microsoft.com...
    >I have taken the following code from the Dir help file which works ok at
    > first level.
    > However, I need to search for all the associated subdirectories but not too
    > sure how to adapt the code. could someone be kind enough to assist please?
    > Many thanks.
    > --
    > GB
    >
    > Sub Finddir()
    > 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 ' it represents
    > a directory.
    > End If
    > End If
    > myname = Dir ' Get next entry.
    > Loop
    > End Sub




  4. #4
    greasybeano
    Guest

    Re: Dir

    thanks for reply but could not see anything from link provided that would
    assist me.
    --
    GB


    "pascalv@sdf.lonestar.org" wrote:

    > On Sat, 27 May 2006, greasybeano wrote:
    >
    > > I have taken the following code from the Dir help file which works ok at
    > > first level.
    > > However, I need to search for all the associated subdirectories but not too
    > > sure how to adapt the code. could someone be kind enough to assist please?

    >
    > You may be interested by Application.FileSearch
    > and fs.GetFolder(folderName)
    > where fs is a FileSystemObject
    >
    > http://groups.google.com/group/micro...183f3d256617ef
    >


  5. #5
    Ardus Petus
    Guest

    Re: Dir

    Dim fso As Scripting.FileSystemObject

    Sub test()
    Set fso = New Scripting.FileSystemObject
    Finddir ("c:\")
    End Sub

    Sub Finddir(MyPath As String)
    Dim fDir As Folder
    Dim fSubDir As Folder
    Set fDir = fso.GetFolder(MyPath)
    Debug.Print fDir.Path
    For Each fSubDir In fDir.SubFolders
    Finddir fSubDir.Path
    Next fSubDir
    End Sub

    HTH
    --
    AP

    "greasybeano" <nospam@farnbro.freeserve.co.uk> a écrit dans le message de
    news: 048F5407-2EB3-425A-A9E0-891E7C1F4690@microsoft.com...
    >I have taken the following code from the Dir help file which works ok at
    > first level.
    > However, I need to search for all the associated subdirectories but not
    > too
    > sure how to adapt the code. could someone be kind enough to assist please?
    > Many thanks.
    > --
    > GB
    >
    > Sub Finddir()
    > 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 ' it
    > represents
    > a directory.
    > End If
    > End If
    > myname = Dir ' Get next entry.
    > Loop
    > End Sub




  6. #6
    greasybeano
    Guest

    Re: Dir

    thanks for all who replied however, still not too clear what i need to do (my
    VB tad limited) I tried sample code provided by Ardus but get Error
    "user-defined type not defined" Ron's solution looks the business but again
    not too sure how I would adapt it for my need.
    I just need to show all the sub folders (from "C\" ) associated to each
    first level directory. these folders may or may not contain any files - I
    should mention i am using xl2003.

    many thanks

    --
    GB


    "Ardus Petus" wrote:

    > Dim fso As Scripting.FileSystemObject
    >
    > Sub test()
    > Set fso = New Scripting.FileSystemObject
    > Finddir ("c:\")
    > End Sub
    >
    > Sub Finddir(MyPath As String)
    > Dim fDir As Folder
    > Dim fSubDir As Folder
    > Set fDir = fso.GetFolder(MyPath)
    > Debug.Print fDir.Path
    > For Each fSubDir In fDir.SubFolders
    > Finddir fSubDir.Path
    > Next fSubDir
    > End Sub
    >
    > HTH
    > --
    > AP
    >
    > "greasybeano" <nospam@farnbro.freeserve.co.uk> a écrit dans le message de
    > news: 048F5407-2EB3-425A-A9E0-891E7C1F4690@microsoft.com...
    > >I have taken the following code from the Dir help file which works ok at
    > > first level.
    > > However, I need to search for all the associated subdirectories but not
    > > too
    > > sure how to adapt the code. could someone be kind enough to assist please?
    > > Many thanks.
    > > --
    > > GB
    > >
    > > Sub Finddir()
    > > 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 ' it
    > > represents
    > > a directory.
    > > End If
    > > End If
    > > myname = Dir ' Get next entry.
    > > Loop
    > > End Sub

    >
    >
    >


  7. #7
    Chip Pearson
    Guest

    Re: Dir

    If all you need is the top subdirectories, not all directories,
    use


    Dim FSO As Object
    Dim Fldr As Object
    Dim SubFldr As Object
    Dim Rng As Range
    Set Rng = Range("A1")

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set Fldr = FSO.getfolder("C:\")
    For Each SubFldr In Fldr.subfolders
    Rng.Value = SubFldr.Path
    Set Rng = Rng(2, 1)
    Next SubFldr



    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com


    "greasybeano" <nospam@farnbro.freeserve.co.uk> wrote in message
    news:76F27322-D907-4682-9475-A075A7FCED3C@microsoft.com...
    > thanks for all who replied however, still not too clear what i
    > need to do (my
    > VB tad limited) I tried sample code provided by Ardus but get
    > Error
    > "user-defined type not defined" Ron's solution looks the
    > business but again
    > not too sure how I would adapt it for my need.
    > I just need to show all the sub folders (from "C\" ) associated
    > to each
    > first level directory. these folders may or may not contain any
    > files - I
    > should mention i am using xl2003.
    >
    > many thanks
    >
    > --
    > GB
    >
    >
    > "Ardus Petus" wrote:
    >
    >> Dim fso As Scripting.FileSystemObject
    >>
    >> Sub test()
    >> Set fso = New Scripting.FileSystemObject
    >> Finddir ("c:\")
    >> End Sub
    >>
    >> Sub Finddir(MyPath As String)
    >> Dim fDir As Folder
    >> Dim fSubDir As Folder
    >> Set fDir = fso.GetFolder(MyPath)
    >> Debug.Print fDir.Path
    >> For Each fSubDir In fDir.SubFolders
    >> Finddir fSubDir.Path
    >> Next fSubDir
    >> End Sub
    >>
    >> HTH
    >> --
    >> AP
    >>
    >> "greasybeano" <nospam@farnbro.freeserve.co.uk> a écrit dans le
    >> message de
    >> news: 048F5407-2EB3-425A-A9E0-891E7C1F4690@microsoft.com...
    >> >I have taken the following code from the Dir help file which
    >> >works ok at
    >> > first level.
    >> > However, I need to search for all the associated
    >> > subdirectories but not
    >> > too
    >> > sure how to adapt the code. could someone be kind enough to
    >> > assist please?
    >> > Many thanks.
    >> > --
    >> > GB
    >> >
    >> > Sub Finddir()
    >> > 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
    >> > ' it
    >> > represents
    >> > a directory.
    >> > End If
    >> > End If
    >> > myname = Dir ' Get next entry.
    >> > Loop
    >> > End Sub

    >>
    >>
    >>




  8. #8
    Bob Phillips
    Guest

    Re: Dir

    Try this variation on Ardus's code

    Sub Finddir()
    Dim FSO As Object
    Dim fDir As Object
    Dim fSubDir As Object
    Dim i As Long
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set fDir = FSO.GetFolder("C:\")
    For Each fSubDir In fDir.SubFolders
    i = i + 1
    Cells(i, "A").Value = fSubDir.Name
    Next fSubDir
    End Sub



    --
    HTH

    Bob Phillips

    (replace somewhere in email address with googlemail if mailing direct)

    "greasybeano" <nospam@farnbro.freeserve.co.uk> wrote in message
    news:76F27322-D907-4682-9475-A075A7FCED3C@microsoft.com...
    > thanks for all who replied however, still not too clear what i need to do

    (my
    > VB tad limited) I tried sample code provided by Ardus but get Error
    > "user-defined type not defined" Ron's solution looks the business but

    again
    > not too sure how I would adapt it for my need.
    > I just need to show all the sub folders (from "C\" ) associated to each
    > first level directory. these folders may or may not contain any files - I
    > should mention i am using xl2003.
    >
    > many thanks
    >
    > --
    > GB
    >
    >
    > "Ardus Petus" wrote:
    >
    > > Dim fso As Scripting.FileSystemObject
    > >
    > > Sub test()
    > > Set fso = New Scripting.FileSystemObject
    > > Finddir ("c:\")
    > > End Sub
    > >
    > > Sub Finddir(MyPath As String)
    > > Dim fDir As Folder
    > > Dim fSubDir As Folder
    > > Set fDir = fso.GetFolder(MyPath)
    > > Debug.Print fDir.Path
    > > For Each fSubDir In fDir.SubFolders
    > > Finddir fSubDir.Path
    > > Next fSubDir
    > > End Sub
    > >
    > > HTH
    > > --
    > > AP
    > >
    > > "greasybeano" <nospam@farnbro.freeserve.co.uk> a écrit dans le message

    de
    > > news: 048F5407-2EB3-425A-A9E0-891E7C1F4690@microsoft.com...
    > > >I have taken the following code from the Dir help file which works ok

    at
    > > > first level.
    > > > However, I need to search for all the associated subdirectories but

    not
    > > > too
    > > > sure how to adapt the code. could someone be kind enough to assist

    please?
    > > > Many thanks.
    > > > --
    > > > GB
    > > >
    > > > Sub Finddir()
    > > > 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 ' it
    > > > represents
    > > > a directory.
    > > > End If
    > > > End If
    > > > myname = Dir ' Get next entry.
    > > > Loop
    > > > End Sub

    > >
    > >
    > >




  9. #9
    greasybeano
    Guest

    Re: Dir

    chip thanks for you kind assistance - code good but want print a list all the
    directories associated with each top level directory - sorry if did not make
    that clear.

    regards
    --
    GB


    "Chip Pearson" wrote:

    > If all you need is the top subdirectories, not all directories,
    > use
    >
    >
    > Dim FSO As Object
    > Dim Fldr As Object
    > Dim SubFldr As Object
    > Dim Rng As Range
    > Set Rng = Range("A1")
    >
    > Set FSO = CreateObject("Scripting.FileSystemObject")
    > Set Fldr = FSO.getfolder("C:\")
    > For Each SubFldr In Fldr.subfolders
    > Rng.Value = SubFldr.Path
    > Set Rng = Rng(2, 1)
    > Next SubFldr
    >
    >
    >
    > --
    > Cordially,
    > Chip Pearson
    > Microsoft MVP - Excel
    > Pearson Software Consulting, LLC
    > www.cpearson.com
    >
    >
    > "greasybeano" <nospam@farnbro.freeserve.co.uk> wrote in message
    > news:76F27322-D907-4682-9475-A075A7FCED3C@microsoft.com...
    > > thanks for all who replied however, still not too clear what i
    > > need to do (my
    > > VB tad limited) I tried sample code provided by Ardus but get
    > > Error
    > > "user-defined type not defined" Ron's solution looks the
    > > business but again
    > > not too sure how I would adapt it for my need.
    > > I just need to show all the sub folders (from "C\" ) associated
    > > to each
    > > first level directory. these folders may or may not contain any
    > > files - I
    > > should mention i am using xl2003.
    > >
    > > many thanks
    > >
    > > --
    > > GB
    > >
    > >
    > > "Ardus Petus" wrote:
    > >
    > >> Dim fso As Scripting.FileSystemObject
    > >>
    > >> Sub test()
    > >> Set fso = New Scripting.FileSystemObject
    > >> Finddir ("c:\")
    > >> End Sub
    > >>
    > >> Sub Finddir(MyPath As String)
    > >> Dim fDir As Folder
    > >> Dim fSubDir As Folder
    > >> Set fDir = fso.GetFolder(MyPath)
    > >> Debug.Print fDir.Path
    > >> For Each fSubDir In fDir.SubFolders
    > >> Finddir fSubDir.Path
    > >> Next fSubDir
    > >> End Sub
    > >>
    > >> HTH
    > >> --
    > >> AP
    > >>
    > >> "greasybeano" <nospam@farnbro.freeserve.co.uk> a écrit dans le
    > >> message de
    > >> news: 048F5407-2EB3-425A-A9E0-891E7C1F4690@microsoft.com...
    > >> >I have taken the following code from the Dir help file which
    > >> >works ok at
    > >> > first level.
    > >> > However, I need to search for all the associated
    > >> > subdirectories but not
    > >> > too
    > >> > sure how to adapt the code. could someone be kind enough to
    > >> > assist please?
    > >> > Many thanks.
    > >> > --
    > >> > GB
    > >> >
    > >> > Sub Finddir()
    > >> > 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
    > >> > ' it
    > >> > represents
    > >> > a directory.
    > >> > End If
    > >> > End If
    > >> > myname = Dir ' Get next entry.
    > >> > Loop
    > >> > End Sub
    > >>
    > >>
    > >>

    >
    >
    >


  10. #10
    greasybeano
    Guest

    Re: Dir

    Bob thanks tried like your solution but still just getting 1st level directory?

    Hope that I made clear what i am trying to achieve - for each top level
    folder, I want to print out all associated subfolders but I having some
    difficulty do this. Some solutions kindly offered by others may work but my
    VB is limited where some adapting is required.

    regards
    --
    GB


    "Bob Phillips" wrote:

    > Try this variation on Ardus's code
    >
    > Sub Finddir()
    > Dim FSO As Object
    > Dim fDir As Object
    > Dim fSubDir As Object
    > Dim i As Long
    > Set FSO = CreateObject("Scripting.FileSystemObject")
    > Set fDir = FSO.GetFolder("C:\")
    > For Each fSubDir In fDir.SubFolders
    > i = i + 1
    > Cells(i, "A").Value = fSubDir.Name
    > Next fSubDir
    > End Sub
    >
    >
    >
    > --
    > HTH
    >
    > Bob Phillips
    >
    > (replace somewhere in email address with googlemail if mailing direct)
    >
    > "greasybeano" <nospam@farnbro.freeserve.co.uk> wrote in message
    > news:76F27322-D907-4682-9475-A075A7FCED3C@microsoft.com...
    > > thanks for all who replied however, still not too clear what i need to do

    > (my
    > > VB tad limited) I tried sample code provided by Ardus but get Error
    > > "user-defined type not defined" Ron's solution looks the business but

    > again
    > > not too sure how I would adapt it for my need.
    > > I just need to show all the sub folders (from "C\" ) associated to each
    > > first level directory. these folders may or may not contain any files - I
    > > should mention i am using xl2003.
    > >
    > > many thanks
    > >
    > > --
    > > GB
    > >
    > >
    > > "Ardus Petus" wrote:
    > >
    > > > Dim fso As Scripting.FileSystemObject
    > > >
    > > > Sub test()
    > > > Set fso = New Scripting.FileSystemObject
    > > > Finddir ("c:\")
    > > > End Sub
    > > >
    > > > Sub Finddir(MyPath As String)
    > > > Dim fDir As Folder
    > > > Dim fSubDir As Folder
    > > > Set fDir = fso.GetFolder(MyPath)
    > > > Debug.Print fDir.Path
    > > > For Each fSubDir In fDir.SubFolders
    > > > Finddir fSubDir.Path
    > > > Next fSubDir
    > > > End Sub
    > > >
    > > > HTH
    > > > --
    > > > AP
    > > >
    > > > "greasybeano" <nospam@farnbro.freeserve.co.uk> a écrit dans le message

    > de
    > > > news: 048F5407-2EB3-425A-A9E0-891E7C1F4690@microsoft.com...
    > > > >I have taken the following code from the Dir help file which works ok

    > at
    > > > > first level.
    > > > > However, I need to search for all the associated subdirectories but

    > not
    > > > > too
    > > > > sure how to adapt the code. could someone be kind enough to assist

    > please?
    > > > > Many thanks.
    > > > > --
    > > > > GB
    > > > >
    > > > > Sub Finddir()
    > > > > 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 ' it
    > > > > represents
    > > > > a directory.
    > > > > End If
    > > > > End If
    > > > > myname = Dir ' Get next entry.
    > > > > Loop
    > > > > End Sub
    > > >
    > > >
    > > >

    >
    >
    >


  11. #11
    Chip Pearson
    Guest

    Re: Dir

    Try

    Sub StartHere()
    Dim FF As Object
    Dim FSO As Object
    Dim Rng As Range
    Set Rng = Range("a1") '<< CHANGE IF REQUIRED
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set FF = FSO.GetFolder("C:") ' << CHANGE IF REQUIRED

    DoFolder FF, Rng
    End Sub

    Sub DoFolder(F As Scripting.Folder, Rng As Range)
    Dim FF As Object
    Rng.Value = F.Path
    Set Rng = Rng(2, 1)
    For Each FF In F.SubFolders
    DoFolder FF, Rng
    Next FF
    End Sub



    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com

    "greasybeano" <nospam@farnbro.freeserve.co.uk> wrote in message
    news:B800F001-2181-4648-9213-C148DF25E25F@microsoft.com...
    > chip thanks for you kind assistance - code good but want print
    > a list all the
    > directories associated with each top level directory - sorry if
    > did not make
    > that clear.
    >
    > regards
    > --
    > GB
    >
    >
    > "Chip Pearson" wrote:
    >
    >> If all you need is the top subdirectories, not all
    >> directories,
    >> use
    >>
    >>
    >> Dim FSO As Object
    >> Dim Fldr As Object
    >> Dim SubFldr As Object
    >> Dim Rng As Range
    >> Set Rng = Range("A1")
    >>
    >> Set FSO = CreateObject("Scripting.FileSystemObject")
    >> Set Fldr = FSO.getfolder("C:\")
    >> For Each SubFldr In Fldr.subfolders
    >> Rng.Value = SubFldr.Path
    >> Set Rng = Rng(2, 1)
    >> Next SubFldr
    >>
    >>
    >>
    >> --
    >> Cordially,
    >> Chip Pearson
    >> Microsoft MVP - Excel
    >> Pearson Software Consulting, LLC
    >> www.cpearson.com
    >>
    >>
    >> "greasybeano" <nospam@farnbro.freeserve.co.uk> wrote in
    >> message
    >> news:76F27322-D907-4682-9475-A075A7FCED3C@microsoft.com...
    >> > thanks for all who replied however, still not too clear what
    >> > i
    >> > need to do (my
    >> > VB tad limited) I tried sample code provided by Ardus but
    >> > get
    >> > Error
    >> > "user-defined type not defined" Ron's solution looks the
    >> > business but again
    >> > not too sure how I would adapt it for my need.
    >> > I just need to show all the sub folders (from "C\" )
    >> > associated
    >> > to each
    >> > first level directory. these folders may or may not contain
    >> > any
    >> > files - I
    >> > should mention i am using xl2003.
    >> >
    >> > many thanks
    >> >
    >> > --
    >> > GB
    >> >
    >> >
    >> > "Ardus Petus" wrote:
    >> >
    >> >> Dim fso As Scripting.FileSystemObject
    >> >>
    >> >> Sub test()
    >> >> Set fso = New Scripting.FileSystemObject
    >> >> Finddir ("c:\")
    >> >> End Sub
    >> >>
    >> >> Sub Finddir(MyPath As String)
    >> >> Dim fDir As Folder
    >> >> Dim fSubDir As Folder
    >> >> Set fDir = fso.GetFolder(MyPath)
    >> >> Debug.Print fDir.Path
    >> >> For Each fSubDir In fDir.SubFolders
    >> >> Finddir fSubDir.Path
    >> >> Next fSubDir
    >> >> End Sub
    >> >>
    >> >> HTH
    >> >> --
    >> >> AP
    >> >>
    >> >> "greasybeano" <nospam@farnbro.freeserve.co.uk> a écrit dans
    >> >> le
    >> >> message de
    >> >> news: 048F5407-2EB3-425A-A9E0-891E7C1F4690@microsoft.com...
    >> >> >I have taken the following code from the Dir help file
    >> >> >which
    >> >> >works ok at
    >> >> > first level.
    >> >> > However, I need to search for all the associated
    >> >> > subdirectories but not
    >> >> > too
    >> >> > sure how to adapt the code. could someone be kind enough
    >> >> > to
    >> >> > assist please?
    >> >> > Many thanks.
    >> >> > --
    >> >> > GB
    >> >> >
    >> >> > Sub Finddir()
    >> >> > 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
    >> >> > ' it
    >> >> > represents
    >> >> > a directory.
    >> >> > End If
    >> >> > End If
    >> >> > myname = Dir ' Get next entry.
    >> >> > Loop
    >> >> > End Sub
    >> >>
    >> >>
    >> >>

    >>
    >>
    >>




  12. #12
    Bob Phillips
    Guest

    Re: Dir

    Option Explicit

    Private cnt As Long
    Private arfiles
    Private level As Long

    Sub Folders()
    Dim i As Long
    Dim sFolder As String
    Dim iStart As Long
    Dim iEnd As Long
    Dim fOutline As Boolean

    arfiles = Array()
    cnt = -1
    level = 1

    sFolder = "K:\"
    ReDim arfiles(2, 0)
    If sFolder <> "" Then
    SelectFiles sFolder
    Application.DisplayAlerts = False
    On Error Resume Next
    Worksheets("Files").Delete
    On Error GoTo 0
    Application.DisplayAlerts = True
    Worksheets.Add.Name = "Files"
    With ActiveSheet
    For i = LBound(arfiles, 2) To UBound(arfiles, 2)
    If arfiles(0, i) = "" Then
    If fOutline Then
    Rows(iStart + 1 & ":" & iEnd).Rows.Group
    End If
    With .Cells(i + 1, arfiles(2, i))
    .Value = arfiles(1, i)
    .Font.Bold = True
    End With
    iStart = i + 1
    iEnd = iStart
    fOutline = False
    End If
    Next
    .Columns("A:Z").ColumnWidth = 5
    End With
    End If
    'just in case there is another set to group
    If fOutline Then
    Rows(iStart + 1 & ":" & iEnd).Rows.Group
    End If

    Columns("A:Z").ColumnWidth = 5
    ActiveSheet.Outline.ShowLevels RowLevels:=1
    ActiveWindow.DisplayGridlines = False

    End Sub

    '-----------------------------------------------------------------------
    Sub SelectFiles(Optional sPath As String)
    '-----------------------------------------------------------------------
    Static FSO As Object
    Dim oSubFolder As Object
    Dim oFolder As Object
    Dim oFile As Object
    Dim oFiles As Object
    Dim arPath

    If FSO Is Nothing Then
    Set FSO = CreateObject("Scripting.FileSystemObject")
    End If

    If sPath = "" Then
    sPath = CurDir
    End If

    arPath = Split(sPath, "\")
    cnt = cnt + 1
    ReDim Preserve arfiles(2, cnt)
    arfiles(0, cnt) = ""
    arfiles(1, cnt) = arPath(level - 1)
    arfiles(2, cnt) = level

    Set oFolder = FSO.GetFolder(sPath)

    level = level + 1
    If Not sPath Like "*System Volume Information*" Then
    For Each oSubFolder In oFolder.subfolders
    SelectFiles oSubFolder.Path
    Next
    End If
    level = level - 1

    End Sub


    --
    HTH

    Bob Phillips

    (replace somewhere in email address with googlemail if mailing direct)

    "greasybeano" <nospam@farnbro.freeserve.co.uk> wrote in message
    news:17A8AFBA-DC4C-4620-A8D1-A501E8676D57@microsoft.com...
    > Bob thanks tried like your solution but still just getting 1st level

    directory?
    >
    > Hope that I made clear what i am trying to achieve - for each top level
    > folder, I want to print out all associated subfolders but I having some
    > difficulty do this. Some solutions kindly offered by others may work but

    my
    > VB is limited where some adapting is required.
    >
    > regards
    > --
    > GB
    >
    >
    > "Bob Phillips" wrote:
    >
    > > Try this variation on Ardus's code
    > >
    > > Sub Finddir()
    > > Dim FSO As Object
    > > Dim fDir As Object
    > > Dim fSubDir As Object
    > > Dim i As Long
    > > Set FSO = CreateObject("Scripting.FileSystemObject")
    > > Set fDir = FSO.GetFolder("C:\")
    > > For Each fSubDir In fDir.SubFolders
    > > i = i + 1
    > > Cells(i, "A").Value = fSubDir.Name
    > > Next fSubDir
    > > End Sub
    > >
    > >
    > >
    > > --
    > > HTH
    > >
    > > Bob Phillips
    > >
    > > (replace somewhere in email address with googlemail if mailing direct)
    > >
    > > "greasybeano" <nospam@farnbro.freeserve.co.uk> wrote in message
    > > news:76F27322-D907-4682-9475-A075A7FCED3C@microsoft.com...
    > > > thanks for all who replied however, still not too clear what i need to

    do
    > > (my
    > > > VB tad limited) I tried sample code provided by Ardus but get Error
    > > > "user-defined type not defined" Ron's solution looks the business but

    > > again
    > > > not too sure how I would adapt it for my need.
    > > > I just need to show all the sub folders (from "C\" ) associated to

    each
    > > > first level directory. these folders may or may not contain any

    files - I
    > > > should mention i am using xl2003.
    > > >
    > > > many thanks
    > > >
    > > > --
    > > > GB
    > > >
    > > >
    > > > "Ardus Petus" wrote:
    > > >
    > > > > Dim fso As Scripting.FileSystemObject
    > > > >
    > > > > Sub test()
    > > > > Set fso = New Scripting.FileSystemObject
    > > > > Finddir ("c:\")
    > > > > End Sub
    > > > >
    > > > > Sub Finddir(MyPath As String)
    > > > > Dim fDir As Folder
    > > > > Dim fSubDir As Folder
    > > > > Set fDir = fso.GetFolder(MyPath)
    > > > > Debug.Print fDir.Path
    > > > > For Each fSubDir In fDir.SubFolders
    > > > > Finddir fSubDir.Path
    > > > > Next fSubDir
    > > > > End Sub
    > > > >
    > > > > HTH
    > > > > --
    > > > > AP
    > > > >
    > > > > "greasybeano" <nospam@farnbro.freeserve.co.uk> a écrit dans le

    message
    > > de
    > > > > news: 048F5407-2EB3-425A-A9E0-891E7C1F4690@microsoft.com...
    > > > > >I have taken the following code from the Dir help file which works

    ok
    > > at
    > > > > > first level.
    > > > > > However, I need to search for all the associated subdirectories

    but
    > > not
    > > > > > too
    > > > > > sure how to adapt the code. could someone be kind enough to assist

    > > please?
    > > > > > Many thanks.
    > > > > > --
    > > > > > GB
    > > > > >
    > > > > > Sub Finddir()
    > > > > > 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 ' it
    > > > > > represents
    > > > > > a directory.
    > > > > > End If
    > > > > > End If
    > > > > > myname = Dir ' Get next entry.
    > > > > > Loop
    > > > > > End Sub
    > > > >
    > > > >
    > > > >

    > >
    > >
    > >




  13. #13
    greasybeano
    Guest

    Re: Dir

    Bob - many many thanks for doing this for me but I did not realise this would
    be so complicated! - despite your kind efforts - I am still only getting the
    tope level directories - Am I doing something wrong?

    As a thought - if I just wanted a to search for a single folder in "C\"
    which may or may not have files contained in it - would that approach be
    easier?

    Again - many thanks to all for your assistance - very much appreciated.

    Regards
    --
    GB


    "Bob Phillips" wrote:

    > Option Explicit
    >
    > Private cnt As Long
    > Private arfiles
    > Private level As Long
    >
    > Sub Folders()
    > Dim i As Long
    > Dim sFolder As String
    > Dim iStart As Long
    > Dim iEnd As Long
    > Dim fOutline As Boolean
    >
    > arfiles = Array()
    > cnt = -1
    > level = 1
    >
    > sFolder = "K:\"
    > ReDim arfiles(2, 0)
    > If sFolder <> "" Then
    > SelectFiles sFolder
    > Application.DisplayAlerts = False
    > On Error Resume Next
    > Worksheets("Files").Delete
    > On Error GoTo 0
    > Application.DisplayAlerts = True
    > Worksheets.Add.Name = "Files"
    > With ActiveSheet
    > For i = LBound(arfiles, 2) To UBound(arfiles, 2)
    > If arfiles(0, i) = "" Then
    > If fOutline Then
    > Rows(iStart + 1 & ":" & iEnd).Rows.Group
    > End If
    > With .Cells(i + 1, arfiles(2, i))
    > .Value = arfiles(1, i)
    > .Font.Bold = True
    > End With
    > iStart = i + 1
    > iEnd = iStart
    > fOutline = False
    > End If
    > Next
    > .Columns("A:Z").ColumnWidth = 5
    > End With
    > End If
    > 'just in case there is another set to group
    > If fOutline Then
    > Rows(iStart + 1 & ":" & iEnd).Rows.Group
    > End If
    >
    > Columns("A:Z").ColumnWidth = 5
    > ActiveSheet.Outline.ShowLevels RowLevels:=1
    > ActiveWindow.DisplayGridlines = False
    >
    > End Sub
    >
    > '-----------------------------------------------------------------------
    > Sub SelectFiles(Optional sPath As String)
    > '-----------------------------------------------------------------------
    > Static FSO As Object
    > Dim oSubFolder As Object
    > Dim oFolder As Object
    > Dim oFile As Object
    > Dim oFiles As Object
    > Dim arPath
    >
    > If FSO Is Nothing Then
    > Set FSO = CreateObject("Scripting.FileSystemObject")
    > End If
    >
    > If sPath = "" Then
    > sPath = CurDir
    > End If
    >
    > arPath = Split(sPath, "\")
    > cnt = cnt + 1
    > ReDim Preserve arfiles(2, cnt)
    > arfiles(0, cnt) = ""
    > arfiles(1, cnt) = arPath(level - 1)
    > arfiles(2, cnt) = level
    >
    > Set oFolder = FSO.GetFolder(sPath)
    >
    > level = level + 1
    > If Not sPath Like "*System Volume Information*" Then
    > For Each oSubFolder In oFolder.subfolders
    > SelectFiles oSubFolder.Path
    > Next
    > End If
    > level = level - 1
    >
    > End Sub
    >
    >
    > --
    > HTH
    >
    > Bob Phillips
    >
    > (replace somewhere in email address with googlemail if mailing direct)
    >
    > "greasybeano" <nospam@farnbro.freeserve.co.uk> wrote in message
    > news:17A8AFBA-DC4C-4620-A8D1-A501E8676D57@microsoft.com...
    > > Bob thanks tried like your solution but still just getting 1st level

    > directory?
    > >
    > > Hope that I made clear what i am trying to achieve - for each top level
    > > folder, I want to print out all associated subfolders but I having some
    > > difficulty do this. Some solutions kindly offered by others may work but

    > my
    > > VB is limited where some adapting is required.
    > >
    > > regards
    > > --
    > > GB
    > >
    > >
    > > "Bob Phillips" wrote:
    > >
    > > > Try this variation on Ardus's code
    > > >
    > > > Sub Finddir()
    > > > Dim FSO As Object
    > > > Dim fDir As Object
    > > > Dim fSubDir As Object
    > > > Dim i As Long
    > > > Set FSO = CreateObject("Scripting.FileSystemObject")
    > > > Set fDir = FSO.GetFolder("C:\")
    > > > For Each fSubDir In fDir.SubFolders
    > > > i = i + 1
    > > > Cells(i, "A").Value = fSubDir.Name
    > > > Next fSubDir
    > > > End Sub
    > > >
    > > >
    > > >
    > > > --
    > > > HTH
    > > >
    > > > Bob Phillips
    > > >
    > > > (replace somewhere in email address with googlemail if mailing direct)
    > > >
    > > > "greasybeano" <nospam@farnbro.freeserve.co.uk> wrote in message
    > > > news:76F27322-D907-4682-9475-A075A7FCED3C@microsoft.com...
    > > > > thanks for all who replied however, still not too clear what i need to

    > do
    > > > (my
    > > > > VB tad limited) I tried sample code provided by Ardus but get Error
    > > > > "user-defined type not defined" Ron's solution looks the business but
    > > > again
    > > > > not too sure how I would adapt it for my need.
    > > > > I just need to show all the sub folders (from "C\" ) associated to

    > each
    > > > > first level directory. these folders may or may not contain any

    > files - I
    > > > > should mention i am using xl2003.
    > > > >
    > > > > many thanks
    > > > >
    > > > > --
    > > > > GB
    > > > >
    > > > >
    > > > > "Ardus Petus" wrote:
    > > > >
    > > > > > Dim fso As Scripting.FileSystemObject
    > > > > >
    > > > > > Sub test()
    > > > > > Set fso = New Scripting.FileSystemObject
    > > > > > Finddir ("c:\")
    > > > > > End Sub
    > > > > >
    > > > > > Sub Finddir(MyPath As String)
    > > > > > Dim fDir As Folder
    > > > > > Dim fSubDir As Folder
    > > > > > Set fDir = fso.GetFolder(MyPath)
    > > > > > Debug.Print fDir.Path
    > > > > > For Each fSubDir In fDir.SubFolders
    > > > > > Finddir fSubDir.Path
    > > > > > Next fSubDir
    > > > > > End Sub
    > > > > >
    > > > > > HTH
    > > > > > --
    > > > > > AP
    > > > > >
    > > > > > "greasybeano" <nospam@farnbro.freeserve.co.uk> a écrit dans le

    > message
    > > > de
    > > > > > news: 048F5407-2EB3-425A-A9E0-891E7C1F4690@microsoft.com...
    > > > > > >I have taken the following code from the Dir help file which works

    > ok
    > > > at
    > > > > > > first level.
    > > > > > > However, I need to search for all the associated subdirectories

    > but
    > > > not
    > > > > > > too
    > > > > > > sure how to adapt the code. could someone be kind enough to assist
    > > > please?
    > > > > > > Many thanks.
    > > > > > > --
    > > > > > > GB
    > > > > > >
    > > > > > > Sub Finddir()
    > > > > > > 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 ' it
    > > > > > > represents
    > > > > > > a directory.
    > > > > > > End If
    > > > > > > End If
    > > > > > > myname = Dir ' Get next entry.
    > > > > > > Loop
    > > > > > > End Sub
    > > > > >
    > > > > >
    > > > > >
    > > >
    > > >
    > > >

    >
    >
    >


  14. #14
    Tom Ogilvy
    Guest

    Re: Dir

    I think he meant this:

    Public Sub test2()


    Dim fs As Object
    Set fs = CreateObject("Scripting.FileSystemObject")
    ActiveCell.Formula = nbFiles("C:\users", fs)


    End Sub


    Private Function nbFiless(folderName As String, ByRef fs As Object)


    Dim f As Object


    nbFiles = fs.GetFolder(folderName).Files.Count


    If Not (fs Is Nothing) Then
    If fs.GetFolder(folderName).SubFolders.Count > 0 Then
    For Each f In fs.GetFolder(folderName).SubFolders
    nbFiles = nbFiles _
    + nbFiles(f.Path, fs)
    Next
    End If
    End If
    End Function




    --
    Regards,
    Tom Ogilvy

    "greasybeano" <nospam@farnbro.freeserve.co.uk> wrote in message
    news:A997DA4E-0016-4380-9E9B-A40AC8ABF7B8@microsoft.com...
    > thanks for reply but could not see anything from link provided that would
    > assist me.
    > --
    > GB
    >
    >
    > "pascalv@sdf.lonestar.org" wrote:
    >
    > > On Sat, 27 May 2006, greasybeano wrote:
    > >
    > > > I have taken the following code from the Dir help file which works ok

    at
    > > > first level.
    > > > However, I need to search for all the associated subdirectories but

    not too
    > > > sure how to adapt the code. could someone be kind enough to assist

    please?
    > >
    > > You may be interested by Application.FileSearch
    > > and fs.GetFolder(folderName)
    > > where fs is a FileSystemObject
    > >
    > >

    http://groups.google.com/group/micro...183f3d256617ef
    > >




  15. #15
    Ardus Petus
    Guest

    Re: Dir

    Ooops: I forgot:
    In VBE, Tools>References
    tick Microsoft Scripting Runtime

    HTH
    --
    AP

    "greasybeano" <nospam@farnbro.freeserve.co.uk> a écrit dans le message de
    news: 76F27322-D907-4682-9475-A075A7FCED3C@microsoft.com...
    > thanks for all who replied however, still not too clear what i need to do
    > (my
    > VB tad limited) I tried sample code provided by Ardus but get Error
    > "user-defined type not defined" Ron's solution looks the business but
    > again
    > not too sure how I would adapt it for my need.
    > I just need to show all the sub folders (from "C\" ) associated to each
    > first level directory. these folders may or may not contain any files - I
    > should mention i am using xl2003.
    >
    > many thanks
    >
    > --
    > GB
    >
    >
    > "Ardus Petus" wrote:
    >
    >> Dim fso As Scripting.FileSystemObject
    >>
    >> Sub test()
    >> Set fso = New Scripting.FileSystemObject
    >> Finddir ("c:\")
    >> End Sub
    >>
    >> Sub Finddir(MyPath As String)
    >> Dim fDir As Folder
    >> Dim fSubDir As Folder
    >> Set fDir = fso.GetFolder(MyPath)
    >> Debug.Print fDir.Path
    >> For Each fSubDir In fDir.SubFolders
    >> Finddir fSubDir.Path
    >> Next fSubDir
    >> End Sub
    >>
    >> HTH
    >> --
    >> AP
    >>
    >> "greasybeano" <nospam@farnbro.freeserve.co.uk> a écrit dans le message de
    >> news: 048F5407-2EB3-425A-A9E0-891E7C1F4690@microsoft.com...
    >> >I have taken the following code from the Dir help file which works ok at
    >> > first level.
    >> > However, I need to search for all the associated subdirectories but not
    >> > too
    >> > sure how to adapt the code. could someone be kind enough to assist
    >> > please?
    >> > Many thanks.
    >> > --
    >> > GB
    >> >
    >> > Sub Finddir()
    >> > 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 ' it
    >> > represents
    >> > a directory.
    >> > End If
    >> > End If
    >> > myname = Dir ' Get next entry.
    >> > Loop
    >> > End Sub

    >>
    >>
    >>




  16. #16
    Tom Ogilvy
    Guest

    Re: Dir

    If I change K:\ to C:\

    It lists every directory in the C drive for me.

    --
    Regards,
    Tom Ogilvy

    "greasybeano" <nospam@farnbro.freeserve.co.uk> wrote in message
    news:D04E5436-2CBA-4336-B258-01753936755F@microsoft.com...
    > Bob - many many thanks for doing this for me but I did not realise this

    would
    > be so complicated! - despite your kind efforts - I am still only getting

    the
    > tope level directories - Am I doing something wrong?
    >
    > As a thought - if I just wanted a to search for a single folder in "C\"
    > which may or may not have files contained in it - would that approach be
    > easier?
    >
    > Again - many thanks to all for your assistance - very much appreciated.
    >
    > Regards
    > --
    > GB
    >
    >
    > "Bob Phillips" wrote:
    >
    > > Option Explicit
    > >
    > > Private cnt As Long
    > > Private arfiles
    > > Private level As Long
    > >
    > > Sub Folders()
    > > Dim i As Long
    > > Dim sFolder As String
    > > Dim iStart As Long
    > > Dim iEnd As Long
    > > Dim fOutline As Boolean
    > >
    > > arfiles = Array()
    > > cnt = -1
    > > level = 1
    > >
    > > sFolder = "K:\"
    > > ReDim arfiles(2, 0)
    > > If sFolder <> "" Then
    > > SelectFiles sFolder
    > > Application.DisplayAlerts = False
    > > On Error Resume Next
    > > Worksheets("Files").Delete
    > > On Error GoTo 0
    > > Application.DisplayAlerts = True
    > > Worksheets.Add.Name = "Files"
    > > With ActiveSheet
    > > For i = LBound(arfiles, 2) To UBound(arfiles, 2)
    > > If arfiles(0, i) = "" Then
    > > If fOutline Then
    > > Rows(iStart + 1 & ":" & iEnd).Rows.Group
    > > End If
    > > With .Cells(i + 1, arfiles(2, i))
    > > .Value = arfiles(1, i)
    > > .Font.Bold = True
    > > End With
    > > iStart = i + 1
    > > iEnd = iStart
    > > fOutline = False
    > > End If
    > > Next
    > > .Columns("A:Z").ColumnWidth = 5
    > > End With
    > > End If
    > > 'just in case there is another set to group
    > > If fOutline Then
    > > Rows(iStart + 1 & ":" & iEnd).Rows.Group
    > > End If
    > >
    > > Columns("A:Z").ColumnWidth = 5
    > > ActiveSheet.Outline.ShowLevels RowLevels:=1
    > > ActiveWindow.DisplayGridlines = False
    > >
    > > End Sub
    > >
    > > '-----------------------------------------------------------------------
    > > Sub SelectFiles(Optional sPath As String)
    > > '-----------------------------------------------------------------------
    > > Static FSO As Object
    > > Dim oSubFolder As Object
    > > Dim oFolder As Object
    > > Dim oFile As Object
    > > Dim oFiles As Object
    > > Dim arPath
    > >
    > > If FSO Is Nothing Then
    > > Set FSO = CreateObject("Scripting.FileSystemObject")
    > > End If
    > >
    > > If sPath = "" Then
    > > sPath = CurDir
    > > End If
    > >
    > > arPath = Split(sPath, "\")
    > > cnt = cnt + 1
    > > ReDim Preserve arfiles(2, cnt)
    > > arfiles(0, cnt) = ""
    > > arfiles(1, cnt) = arPath(level - 1)
    > > arfiles(2, cnt) = level
    > >
    > > Set oFolder = FSO.GetFolder(sPath)
    > >
    > > level = level + 1
    > > If Not sPath Like "*System Volume Information*" Then
    > > For Each oSubFolder In oFolder.subfolders
    > > SelectFiles oSubFolder.Path
    > > Next
    > > End If
    > > level = level - 1
    > >
    > > End Sub
    > >
    > >
    > > --
    > > HTH
    > >
    > > Bob Phillips
    > >
    > > (replace somewhere in email address with googlemail if mailing direct)
    > >
    > > "greasybeano" <nospam@farnbro.freeserve.co.uk> wrote in message
    > > news:17A8AFBA-DC4C-4620-A8D1-A501E8676D57@microsoft.com...
    > > > Bob thanks tried like your solution but still just getting 1st level

    > > directory?
    > > >
    > > > Hope that I made clear what i am trying to achieve - for each top

    level
    > > > folder, I want to print out all associated subfolders but I having

    some
    > > > difficulty do this. Some solutions kindly offered by others may work

    but
    > > my
    > > > VB is limited where some adapting is required.
    > > >
    > > > regards
    > > > --
    > > > GB
    > > >
    > > >
    > > > "Bob Phillips" wrote:
    > > >
    > > > > Try this variation on Ardus's code
    > > > >
    > > > > Sub Finddir()
    > > > > Dim FSO As Object
    > > > > Dim fDir As Object
    > > > > Dim fSubDir As Object
    > > > > Dim i As Long
    > > > > Set FSO = CreateObject("Scripting.FileSystemObject")
    > > > > Set fDir = FSO.GetFolder("C:\")
    > > > > For Each fSubDir In fDir.SubFolders
    > > > > i = i + 1
    > > > > Cells(i, "A").Value = fSubDir.Name
    > > > > Next fSubDir
    > > > > End Sub
    > > > >
    > > > >
    > > > >
    > > > > --
    > > > > HTH
    > > > >
    > > > > Bob Phillips
    > > > >
    > > > > (replace somewhere in email address with googlemail if mailing

    direct)
    > > > >
    > > > > "greasybeano" <nospam@farnbro.freeserve.co.uk> wrote in message
    > > > > news:76F27322-D907-4682-9475-A075A7FCED3C@microsoft.com...
    > > > > > thanks for all who replied however, still not too clear what i

    need to
    > > do
    > > > > (my
    > > > > > VB tad limited) I tried sample code provided by Ardus but get

    Error
    > > > > > "user-defined type not defined" Ron's solution looks the business

    but
    > > > > again
    > > > > > not too sure how I would adapt it for my need.
    > > > > > I just need to show all the sub folders (from "C\" ) associated to

    > > each
    > > > > > first level directory. these folders may or may not contain any

    > > files - I
    > > > > > should mention i am using xl2003.
    > > > > >
    > > > > > many thanks
    > > > > >
    > > > > > --
    > > > > > GB
    > > > > >
    > > > > >
    > > > > > "Ardus Petus" wrote:
    > > > > >
    > > > > > > Dim fso As Scripting.FileSystemObject
    > > > > > >
    > > > > > > Sub test()
    > > > > > > Set fso = New Scripting.FileSystemObject
    > > > > > > Finddir ("c:\")
    > > > > > > End Sub
    > > > > > >
    > > > > > > Sub Finddir(MyPath As String)
    > > > > > > Dim fDir As Folder
    > > > > > > Dim fSubDir As Folder
    > > > > > > Set fDir = fso.GetFolder(MyPath)
    > > > > > > Debug.Print fDir.Path
    > > > > > > For Each fSubDir In fDir.SubFolders
    > > > > > > Finddir fSubDir.Path
    > > > > > > Next fSubDir
    > > > > > > End Sub
    > > > > > >
    > > > > > > HTH
    > > > > > > --
    > > > > > > AP
    > > > > > >
    > > > > > > "greasybeano" <nospam@farnbro.freeserve.co.uk> a écrit dans le

    > > message
    > > > > de
    > > > > > > news: 048F5407-2EB3-425A-A9E0-891E7C1F4690@microsoft.com...
    > > > > > > >I have taken the following code from the Dir help file which

    works
    > > ok
    > > > > at
    > > > > > > > first level.
    > > > > > > > However, I need to search for all the associated

    subdirectories
    > > but
    > > > > not
    > > > > > > > too
    > > > > > > > sure how to adapt the code. could someone be kind enough to

    assist
    > > > > please?
    > > > > > > > Many thanks.
    > > > > > > > --
    > > > > > > > GB
    > > > > > > >
    > > > > > > > Sub Finddir()
    > > > > > > > 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 '

    it
    > > > > > > > represents
    > > > > > > > a directory.
    > > > > > > > End If
    > > > > > > > End If
    > > > > > > > myname = Dir ' Get next entry.
    > > > > > > > Loop
    > > > > > > > End Sub
    > > > > > >
    > > > > > >
    > > > > > >
    > > > >
    > > > >
    > > > >

    > >
    > >
    > >




  17. #17
    Bob Phillips
    Guest

    Re: Dir

    The complexity is mainly due to the formatting that I apply to the list of
    folders, the main code is relatively simple.

    It does work as Tom verified, so you need to check what you did.

    --
    HTH

    Bob Phillips

    (replace somewhere in email address with googlemail if mailing direct)

    "greasybeano" <nospam@farnbro.freeserve.co.uk> wrote in message
    news:D04E5436-2CBA-4336-B258-01753936755F@microsoft.com...
    > Bob - many many thanks for doing this for me but I did not realise this

    would
    > be so complicated! - despite your kind efforts - I am still only getting

    the
    > tope level directories - Am I doing something wrong?
    >
    > As a thought - if I just wanted a to search for a single folder in "C\"
    > which may or may not have files contained in it - would that approach be
    > easier?
    >
    > Again - many thanks to all for your assistance - very much appreciated.
    >
    > Regards
    > --
    > GB
    >
    >
    > "Bob Phillips" wrote:
    >
    > > Option Explicit
    > >
    > > Private cnt As Long
    > > Private arfiles
    > > Private level As Long
    > >
    > > Sub Folders()
    > > Dim i As Long
    > > Dim sFolder As String
    > > Dim iStart As Long
    > > Dim iEnd As Long
    > > Dim fOutline As Boolean
    > >
    > > arfiles = Array()
    > > cnt = -1
    > > level = 1
    > >
    > > sFolder = "K:\"
    > > ReDim arfiles(2, 0)
    > > If sFolder <> "" Then
    > > SelectFiles sFolder
    > > Application.DisplayAlerts = False
    > > On Error Resume Next
    > > Worksheets("Files").Delete
    > > On Error GoTo 0
    > > Application.DisplayAlerts = True
    > > Worksheets.Add.Name = "Files"
    > > With ActiveSheet
    > > For i = LBound(arfiles, 2) To UBound(arfiles, 2)
    > > If arfiles(0, i) = "" Then
    > > If fOutline Then
    > > Rows(iStart + 1 & ":" & iEnd).Rows.Group
    > > End If
    > > With .Cells(i + 1, arfiles(2, i))
    > > .Value = arfiles(1, i)
    > > .Font.Bold = True
    > > End With
    > > iStart = i + 1
    > > iEnd = iStart
    > > fOutline = False
    > > End If
    > > Next
    > > .Columns("A:Z").ColumnWidth = 5
    > > End With
    > > End If
    > > 'just in case there is another set to group
    > > If fOutline Then
    > > Rows(iStart + 1 & ":" & iEnd).Rows.Group
    > > End If
    > >
    > > Columns("A:Z").ColumnWidth = 5
    > > ActiveSheet.Outline.ShowLevels RowLevels:=1
    > > ActiveWindow.DisplayGridlines = False
    > >
    > > End Sub
    > >
    > > '-----------------------------------------------------------------------
    > > Sub SelectFiles(Optional sPath As String)
    > > '-----------------------------------------------------------------------
    > > Static FSO As Object
    > > Dim oSubFolder As Object
    > > Dim oFolder As Object
    > > Dim oFile As Object
    > > Dim oFiles As Object
    > > Dim arPath
    > >
    > > If FSO Is Nothing Then
    > > Set FSO = CreateObject("Scripting.FileSystemObject")
    > > End If
    > >
    > > If sPath = "" Then
    > > sPath = CurDir
    > > End If
    > >
    > > arPath = Split(sPath, "\")
    > > cnt = cnt + 1
    > > ReDim Preserve arfiles(2, cnt)
    > > arfiles(0, cnt) = ""
    > > arfiles(1, cnt) = arPath(level - 1)
    > > arfiles(2, cnt) = level
    > >
    > > Set oFolder = FSO.GetFolder(sPath)
    > >
    > > level = level + 1
    > > If Not sPath Like "*System Volume Information*" Then
    > > For Each oSubFolder In oFolder.subfolders
    > > SelectFiles oSubFolder.Path
    > > Next
    > > End If
    > > level = level - 1
    > >
    > > End Sub
    > >
    > >
    > > --
    > > HTH
    > >
    > > Bob Phillips
    > >
    > > (replace somewhere in email address with googlemail if mailing direct)
    > >
    > > "greasybeano" <nospam@farnbro.freeserve.co.uk> wrote in message
    > > news:17A8AFBA-DC4C-4620-A8D1-A501E8676D57@microsoft.com...
    > > > Bob thanks tried like your solution but still just getting 1st level

    > > directory?
    > > >
    > > > Hope that I made clear what i am trying to achieve - for each top

    level
    > > > folder, I want to print out all associated subfolders but I having

    some
    > > > difficulty do this. Some solutions kindly offered by others may work

    but
    > > my
    > > > VB is limited where some adapting is required.
    > > >
    > > > regards
    > > > --
    > > > GB
    > > >
    > > >
    > > > "Bob Phillips" wrote:
    > > >
    > > > > Try this variation on Ardus's code
    > > > >
    > > > > Sub Finddir()
    > > > > Dim FSO As Object
    > > > > Dim fDir As Object
    > > > > Dim fSubDir As Object
    > > > > Dim i As Long
    > > > > Set FSO = CreateObject("Scripting.FileSystemObject")
    > > > > Set fDir = FSO.GetFolder("C:\")
    > > > > For Each fSubDir In fDir.SubFolders
    > > > > i = i + 1
    > > > > Cells(i, "A").Value = fSubDir.Name
    > > > > Next fSubDir
    > > > > End Sub
    > > > >
    > > > >
    > > > >
    > > > > --
    > > > > HTH
    > > > >
    > > > > Bob Phillips
    > > > >
    > > > > (replace somewhere in email address with googlemail if mailing

    direct)
    > > > >
    > > > > "greasybeano" <nospam@farnbro.freeserve.co.uk> wrote in message
    > > > > news:76F27322-D907-4682-9475-A075A7FCED3C@microsoft.com...
    > > > > > thanks for all who replied however, still not too clear what i

    need to
    > > do
    > > > > (my
    > > > > > VB tad limited) I tried sample code provided by Ardus but get

    Error
    > > > > > "user-defined type not defined" Ron's solution looks the business

    but
    > > > > again
    > > > > > not too sure how I would adapt it for my need.
    > > > > > I just need to show all the sub folders (from "C\" ) associated to

    > > each
    > > > > > first level directory. these folders may or may not contain any

    > > files - I
    > > > > > should mention i am using xl2003.
    > > > > >
    > > > > > many thanks
    > > > > >
    > > > > > --
    > > > > > GB
    > > > > >
    > > > > >
    > > > > > "Ardus Petus" wrote:
    > > > > >
    > > > > > > Dim fso As Scripting.FileSystemObject
    > > > > > >
    > > > > > > Sub test()
    > > > > > > Set fso = New Scripting.FileSystemObject
    > > > > > > Finddir ("c:\")
    > > > > > > End Sub
    > > > > > >
    > > > > > > Sub Finddir(MyPath As String)
    > > > > > > Dim fDir As Folder
    > > > > > > Dim fSubDir As Folder
    > > > > > > Set fDir = fso.GetFolder(MyPath)
    > > > > > > Debug.Print fDir.Path
    > > > > > > For Each fSubDir In fDir.SubFolders
    > > > > > > Finddir fSubDir.Path
    > > > > > > Next fSubDir
    > > > > > > End Sub
    > > > > > >
    > > > > > > HTH
    > > > > > > --
    > > > > > > AP
    > > > > > >
    > > > > > > "greasybeano" <nospam@farnbro.freeserve.co.uk> a écrit dans le

    > > message
    > > > > de
    > > > > > > news: 048F5407-2EB3-425A-A9E0-891E7C1F4690@microsoft.com...
    > > > > > > >I have taken the following code from the Dir help file which

    works
    > > ok
    > > > > at
    > > > > > > > first level.
    > > > > > > > However, I need to search for all the associated

    subdirectories
    > > but
    > > > > not
    > > > > > > > too
    > > > > > > > sure how to adapt the code. could someone be kind enough to

    assist
    > > > > please?
    > > > > > > > Many thanks.
    > > > > > > > --
    > > > > > > > GB
    > > > > > > >
    > > > > > > > Sub Finddir()
    > > > > > > > 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 '

    it
    > > > > > > > represents
    > > > > > > > a directory.
    > > > > > > > End If
    > > > > > > > End If
    > > > > > > > myname = Dir ' Get next entry.
    > > > > > > > Loop
    > > > > > > > End Sub
    > > > > > >
    > > > > > >
    > > > > > >
    > > > >
    > > > >
    > > > >

    > >
    > >
    > >




  18. #18
    greasybeano
    Guest

    Re: Dir

    it was that simple! works as intended! many thanks to you all.

    regards
    --
    GB


    "Ardus Petus" wrote:

    > Ooops: I forgot:
    > In VBE, Tools>References
    > tick Microsoft Scripting Runtime
    >
    > HTH
    > --
    > AP
    >
    > "greasybeano" <nospam@farnbro.freeserve.co.uk> a écrit dans le message de
    > news: 76F27322-D907-4682-9475-A075A7FCED3C@microsoft.com...
    > > thanks for all who replied however, still not too clear what i need to do
    > > (my
    > > VB tad limited) I tried sample code provided by Ardus but get Error
    > > "user-defined type not defined" Ron's solution looks the business but
    > > again
    > > not too sure how I would adapt it for my need.
    > > I just need to show all the sub folders (from "C\" ) associated to each
    > > first level directory. these folders may or may not contain any files - I
    > > should mention i am using xl2003.
    > >
    > > many thanks
    > >
    > > --
    > > GB
    > >
    > >
    > > "Ardus Petus" wrote:
    > >
    > >> Dim fso As Scripting.FileSystemObject
    > >>
    > >> Sub test()
    > >> Set fso = New Scripting.FileSystemObject
    > >> Finddir ("c:\")
    > >> End Sub
    > >>
    > >> Sub Finddir(MyPath As String)
    > >> Dim fDir As Folder
    > >> Dim fSubDir As Folder
    > >> Set fDir = fso.GetFolder(MyPath)
    > >> Debug.Print fDir.Path
    > >> For Each fSubDir In fDir.SubFolders
    > >> Finddir fSubDir.Path
    > >> Next fSubDir
    > >> End Sub
    > >>
    > >> HTH
    > >> --
    > >> AP
    > >>
    > >> "greasybeano" <nospam@farnbro.freeserve.co.uk> a écrit dans le message de
    > >> news: 048F5407-2EB3-425A-A9E0-891E7C1F4690@microsoft.com...
    > >> >I have taken the following code from the Dir help file which works ok at
    > >> > first level.
    > >> > However, I need to search for all the associated subdirectories but not
    > >> > too
    > >> > sure how to adapt the code. could someone be kind enough to assist
    > >> > please?
    > >> > Many thanks.
    > >> > --
    > >> > GB
    > >> >
    > >> > Sub Finddir()
    > >> > 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 ' it
    > >> > represents
    > >> > a directory.
    > >> > End If
    > >> > End If
    > >> > myname = Dir ' Get next entry.
    > >> > Loop
    > >> > End Sub
    > >>
    > >>
    > >>

    >
    >
    >


  19. #19
    Ardus Petus
    Guest

    Re: Dir

    Glad I could help!

    --
    AP

    "greasybeano" <nospam@farnbro.freeserve.co.uk> a écrit dans le message de
    news: E41F036B-B3F2-4433-ACAB-E6FD3083766C@microsoft.com...
    > it was that simple! works as intended! many thanks to you all.
    >
    > regards
    > --
    > GB
    >
    >
    > "Ardus Petus" wrote:
    >
    >> Ooops: I forgot:
    >> In VBE, Tools>References
    >> tick Microsoft Scripting Runtime
    >>
    >> HTH
    >> --
    >> AP
    >>
    >> "greasybeano" <nospam@farnbro.freeserve.co.uk> a écrit dans le message de
    >> news: 76F27322-D907-4682-9475-A075A7FCED3C@microsoft.com...
    >> > thanks for all who replied however, still not too clear what i need to
    >> > do
    >> > (my
    >> > VB tad limited) I tried sample code provided by Ardus but get Error
    >> > "user-defined type not defined" Ron's solution looks the business but
    >> > again
    >> > not too sure how I would adapt it for my need.
    >> > I just need to show all the sub folders (from "C\" ) associated to each
    >> > first level directory. these folders may or may not contain any files -
    >> > I
    >> > should mention i am using xl2003.
    >> >
    >> > many thanks
    >> >
    >> > --
    >> > GB
    >> >
    >> >
    >> > "Ardus Petus" wrote:
    >> >
    >> >> Dim fso As Scripting.FileSystemObject
    >> >>
    >> >> Sub test()
    >> >> Set fso = New Scripting.FileSystemObject
    >> >> Finddir ("c:\")
    >> >> End Sub
    >> >>
    >> >> Sub Finddir(MyPath As String)
    >> >> Dim fDir As Folder
    >> >> Dim fSubDir As Folder
    >> >> Set fDir = fso.GetFolder(MyPath)
    >> >> Debug.Print fDir.Path
    >> >> For Each fSubDir In fDir.SubFolders
    >> >> Finddir fSubDir.Path
    >> >> Next fSubDir
    >> >> End Sub
    >> >>
    >> >> HTH
    >> >> --
    >> >> AP
    >> >>
    >> >> "greasybeano" <nospam@farnbro.freeserve.co.uk> a écrit dans le message
    >> >> de
    >> >> news: 048F5407-2EB3-425A-A9E0-891E7C1F4690@microsoft.com...
    >> >> >I have taken the following code from the Dir help file which works ok
    >> >> >at
    >> >> > first level.
    >> >> > However, I need to search for all the associated subdirectories but
    >> >> > not
    >> >> > too
    >> >> > sure how to adapt the code. could someone be kind enough to assist
    >> >> > please?
    >> >> > Many thanks.
    >> >> > --
    >> >> > GB
    >> >> >
    >> >> > Sub Finddir()
    >> >> > 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 ' it
    >> >> > represents
    >> >> > a directory.
    >> >> > End If
    >> >> > End If
    >> >> > myname = Dir ' Get next entry.
    >> >> > Loop
    >> >> > End Sub
    >> >>
    >> >>
    >> >>

    >>
    >>
    >>




  20. #20
    Antonio
    Guest

    Re: Dir

    Hi Bob,

    I take my hat off. This piece of code is high quality.

    Thanks,

    Antonio

    "Bob Phillips" wrote:

    > Option Explicit
    >
    > Private cnt As Long
    > Private arfiles
    > Private level As Long
    >
    > Sub Folders()
    > Dim i As Long
    > Dim sFolder As String
    > Dim iStart As Long
    > Dim iEnd As Long
    > Dim fOutline As Boolean
    >
    > arfiles = Array()
    > cnt = -1
    > level = 1
    >
    > sFolder = "K:\"
    > ReDim arfiles(2, 0)
    > If sFolder <> "" Then
    > SelectFiles sFolder
    > Application.DisplayAlerts = False
    > On Error Resume Next
    > Worksheets("Files").Delete
    > On Error GoTo 0
    > Application.DisplayAlerts = True
    > Worksheets.Add.Name = "Files"
    > With ActiveSheet
    > For i = LBound(arfiles, 2) To UBound(arfiles, 2)
    > If arfiles(0, i) = "" Then
    > If fOutline Then
    > Rows(iStart + 1 & ":" & iEnd).Rows.Group
    > End If
    > With .Cells(i + 1, arfiles(2, i))
    > .Value = arfiles(1, i)
    > .Font.Bold = True
    > End With
    > iStart = i + 1
    > iEnd = iStart
    > fOutline = False
    > End If
    > Next
    > .Columns("A:Z").ColumnWidth = 5
    > End With
    > End If
    > 'just in case there is another set to group
    > If fOutline Then
    > Rows(iStart + 1 & ":" & iEnd).Rows.Group
    > End If
    >
    > Columns("A:Z").ColumnWidth = 5
    > ActiveSheet.Outline.ShowLevels RowLevels:=1
    > ActiveWindow.DisplayGridlines = False
    >
    > End Sub
    >
    > '-----------------------------------------------------------------------
    > Sub SelectFiles(Optional sPath As String)
    > '-----------------------------------------------------------------------
    > Static FSO As Object
    > Dim oSubFolder As Object
    > Dim oFolder As Object
    > Dim oFile As Object
    > Dim oFiles As Object
    > Dim arPath
    >
    > If FSO Is Nothing Then
    > Set FSO = CreateObject("Scripting.FileSystemObject")
    > End If
    >
    > If sPath = "" Then
    > sPath = CurDir
    > End If
    >
    > arPath = Split(sPath, "\")
    > cnt = cnt + 1
    > ReDim Preserve arfiles(2, cnt)
    > arfiles(0, cnt) = ""
    > arfiles(1, cnt) = arPath(level - 1)
    > arfiles(2, cnt) = level
    >
    > Set oFolder = FSO.GetFolder(sPath)
    >
    > level = level + 1
    > If Not sPath Like "*System Volume Information*" Then
    > For Each oSubFolder In oFolder.subfolders
    > SelectFiles oSubFolder.Path
    > Next
    > End If
    > level = level - 1
    >
    > End Sub
    >
    >
    > --
    > HTH
    >
    > Bob Phillips
    >
    > (replace somewhere in email address with googlemail if mailing direct)
    >
    > "greasybeano" <nospam@farnbro.freeserve.co.uk> wrote in message
    > news:17A8AFBA-DC4C-4620-A8D1-A501E8676D57@microsoft.com...
    > > Bob thanks tried like your solution but still just getting 1st level

    > directory?
    > >
    > > Hope that I made clear what i am trying to achieve - for each top level
    > > folder, I want to print out all associated subfolders but I having some
    > > difficulty do this. Some solutions kindly offered by others may work but

    > my
    > > VB is limited where some adapting is required.
    > >
    > > regards
    > > --
    > > GB
    > >
    > >
    > > "Bob Phillips" wrote:
    > >
    > > > Try this variation on Ardus's code
    > > >
    > > > Sub Finddir()
    > > > Dim FSO As Object
    > > > Dim fDir As Object
    > > > Dim fSubDir As Object
    > > > Dim i As Long
    > > > Set FSO = CreateObject("Scripting.FileSystemObject")
    > > > Set fDir = FSO.GetFolder("C:\")
    > > > For Each fSubDir In fDir.SubFolders
    > > > i = i + 1
    > > > Cells(i, "A").Value = fSubDir.Name
    > > > Next fSubDir
    > > > End Sub
    > > >
    > > >
    > > >
    > > > --
    > > > HTH
    > > >
    > > > Bob Phillips
    > > >
    > > > (replace somewhere in email address with googlemail if mailing direct)
    > > >
    > > > "greasybeano" <nospam@farnbro.freeserve.co.uk> wrote in message
    > > > news:76F27322-D907-4682-9475-A075A7FCED3C@microsoft.com...
    > > > > thanks for all who replied however, still not too clear what i need to

    > do
    > > > (my
    > > > > VB tad limited) I tried sample code provided by Ardus but get Error
    > > > > "user-defined type not defined" Ron's solution looks the business but
    > > > again
    > > > > not too sure how I would adapt it for my need.
    > > > > I just need to show all the sub folders (from "C\" ) associated to

    > each
    > > > > first level directory. these folders may or may not contain any

    > files - I
    > > > > should mention i am using xl2003.
    > > > >
    > > > > many thanks
    > > > >
    > > > > --
    > > > > GB
    > > > >
    > > > >
    > > > > "Ardus Petus" wrote:
    > > > >
    > > > > > Dim fso As Scripting.FileSystemObject
    > > > > >
    > > > > > Sub test()
    > > > > > Set fso = New Scripting.FileSystemObject
    > > > > > Finddir ("c:\")
    > > > > > End Sub
    > > > > >
    > > > > > Sub Finddir(MyPath As String)
    > > > > > Dim fDir As Folder
    > > > > > Dim fSubDir As Folder
    > > > > > Set fDir = fso.GetFolder(MyPath)
    > > > > > Debug.Print fDir.Path
    > > > > > For Each fSubDir In fDir.SubFolders
    > > > > > Finddir fSubDir.Path
    > > > > > Next fSubDir
    > > > > > End Sub
    > > > > >
    > > > > > HTH
    > > > > > --
    > > > > > AP
    > > > > >
    > > > > > "greasybeano" <nospam@farnbro.freeserve.co.uk> a écrit dans le

    > message
    > > > de
    > > > > > news: 048F5407-2EB3-425A-A9E0-891E7C1F4690@microsoft.com...
    > > > > > >I have taken the following code from the Dir help file which works

    > ok
    > > > at
    > > > > > > first level.
    > > > > > > However, I need to search for all the associated subdirectories

    > but
    > > > not
    > > > > > > too
    > > > > > > sure how to adapt the code. could someone be kind enough to assist
    > > > please?
    > > > > > > Many thanks.
    > > > > > > --
    > > > > > > GB
    > > > > > >
    > > > > > > Sub Finddir()
    > > > > > > 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 ' it
    > > > > > > represents
    > > > > > > a directory.
    > > > > > > End If
    > > > > > > End If
    > > > > > > myname = Dir ' Get next entry.
    > > > > > > Loop
    > > > > > > End Sub
    > > > > >
    > > > > >
    > > > > >
    > > >
    > > >
    > > >

    >
    >
    >


  21. #21
    Bob Phillips
    Guest

    Re: Dir

    It's better in its full version Antonio, I include the files in that and
    hyperlink them

    Bob

    "Antonio" <Antonio@discussions.microsoft.com> wrote in message
    news:43A94D4F-4D20-4EA5-AFD3-4DB5C3E1CEEF@microsoft.com...
    > Hi Bob,
    >
    > I take my hat off. This piece of code is high quality.
    >
    > Thanks,
    >
    > Antonio
    >
    > "Bob Phillips" wrote:
    >
    > > Option Explicit
    > >
    > > Private cnt As Long
    > > Private arfiles
    > > Private level As Long
    > >
    > > Sub Folders()
    > > Dim i As Long
    > > Dim sFolder As String
    > > Dim iStart As Long
    > > Dim iEnd As Long
    > > Dim fOutline As Boolean
    > >
    > > arfiles = Array()
    > > cnt = -1
    > > level = 1
    > >
    > > sFolder = "K:\"
    > > ReDim arfiles(2, 0)
    > > If sFolder <> "" Then
    > > SelectFiles sFolder
    > > Application.DisplayAlerts = False
    > > On Error Resume Next
    > > Worksheets("Files").Delete
    > > On Error GoTo 0
    > > Application.DisplayAlerts = True
    > > Worksheets.Add.Name = "Files"
    > > With ActiveSheet
    > > For i = LBound(arfiles, 2) To UBound(arfiles, 2)
    > > If arfiles(0, i) = "" Then
    > > If fOutline Then
    > > Rows(iStart + 1 & ":" & iEnd).Rows.Group
    > > End If
    > > With .Cells(i + 1, arfiles(2, i))
    > > .Value = arfiles(1, i)
    > > .Font.Bold = True
    > > End With
    > > iStart = i + 1
    > > iEnd = iStart
    > > fOutline = False
    > > End If
    > > Next
    > > .Columns("A:Z").ColumnWidth = 5
    > > End With
    > > End If
    > > 'just in case there is another set to group
    > > If fOutline Then
    > > Rows(iStart + 1 & ":" & iEnd).Rows.Group
    > > End If
    > >
    > > Columns("A:Z").ColumnWidth = 5
    > > ActiveSheet.Outline.ShowLevels RowLevels:=1
    > > ActiveWindow.DisplayGridlines = False
    > >
    > > End Sub
    > >
    > > '-----------------------------------------------------------------------
    > > Sub SelectFiles(Optional sPath As String)
    > > '-----------------------------------------------------------------------
    > > Static FSO As Object
    > > Dim oSubFolder As Object
    > > Dim oFolder As Object
    > > Dim oFile As Object
    > > Dim oFiles As Object
    > > Dim arPath
    > >
    > > If FSO Is Nothing Then
    > > Set FSO = CreateObject("Scripting.FileSystemObject")
    > > End If
    > >
    > > If sPath = "" Then
    > > sPath = CurDir
    > > End If
    > >
    > > arPath = Split(sPath, "\")
    > > cnt = cnt + 1
    > > ReDim Preserve arfiles(2, cnt)
    > > arfiles(0, cnt) = ""
    > > arfiles(1, cnt) = arPath(level - 1)
    > > arfiles(2, cnt) = level
    > >
    > > Set oFolder = FSO.GetFolder(sPath)
    > >
    > > level = level + 1
    > > If Not sPath Like "*System Volume Information*" Then
    > > For Each oSubFolder In oFolder.subfolders
    > > SelectFiles oSubFolder.Path
    > > Next
    > > End If
    > > level = level - 1
    > >
    > > End Sub
    > >
    > >
    > > --
    > > HTH
    > >
    > > Bob Phillips
    > >
    > > (replace somewhere in email address with googlemail if mailing direct)
    > >
    > > "greasybeano" <nospam@farnbro.freeserve.co.uk> wrote in message
    > > news:17A8AFBA-DC4C-4620-A8D1-A501E8676D57@microsoft.com...
    > > > Bob thanks tried like your solution but still just getting 1st level

    > > directory?
    > > >
    > > > Hope that I made clear what i am trying to achieve - for each top

    level
    > > > folder, I want to print out all associated subfolders but I having

    some
    > > > difficulty do this. Some solutions kindly offered by others may work

    but
    > > my
    > > > VB is limited where some adapting is required.
    > > >
    > > > regards
    > > > --
    > > > GB
    > > >
    > > >
    > > > "Bob Phillips" wrote:
    > > >
    > > > > Try this variation on Ardus's code
    > > > >
    > > > > Sub Finddir()
    > > > > Dim FSO As Object
    > > > > Dim fDir As Object
    > > > > Dim fSubDir As Object
    > > > > Dim i As Long
    > > > > Set FSO = CreateObject("Scripting.FileSystemObject")
    > > > > Set fDir = FSO.GetFolder("C:\")
    > > > > For Each fSubDir In fDir.SubFolders
    > > > > i = i + 1
    > > > > Cells(i, "A").Value = fSubDir.Name
    > > > > Next fSubDir
    > > > > End Sub
    > > > >
    > > > >
    > > > >
    > > > > --
    > > > > HTH
    > > > >
    > > > > Bob Phillips
    > > > >
    > > > > (replace somewhere in email address with googlemail if mailing

    direct)
    > > > >
    > > > > "greasybeano" <nospam@farnbro.freeserve.co.uk> wrote in message
    > > > > news:76F27322-D907-4682-9475-A075A7FCED3C@microsoft.com...
    > > > > > thanks for all who replied however, still not too clear what i

    need to
    > > do
    > > > > (my
    > > > > > VB tad limited) I tried sample code provided by Ardus but get

    Error
    > > > > > "user-defined type not defined" Ron's solution looks the business

    but
    > > > > again
    > > > > > not too sure how I would adapt it for my need.
    > > > > > I just need to show all the sub folders (from "C\" ) associated to

    > > each
    > > > > > first level directory. these folders may or may not contain any

    > > files - I
    > > > > > should mention i am using xl2003.
    > > > > >
    > > > > > many thanks
    > > > > >
    > > > > > --
    > > > > > GB
    > > > > >
    > > > > >
    > > > > > "Ardus Petus" wrote:
    > > > > >
    > > > > > > Dim fso As Scripting.FileSystemObject
    > > > > > >
    > > > > > > Sub test()
    > > > > > > Set fso = New Scripting.FileSystemObject
    > > > > > > Finddir ("c:\")
    > > > > > > End Sub
    > > > > > >
    > > > > > > Sub Finddir(MyPath As String)
    > > > > > > Dim fDir As Folder
    > > > > > > Dim fSubDir As Folder
    > > > > > > Set fDir = fso.GetFolder(MyPath)
    > > > > > > Debug.Print fDir.Path
    > > > > > > For Each fSubDir In fDir.SubFolders
    > > > > > > Finddir fSubDir.Path
    > > > > > > Next fSubDir
    > > > > > > End Sub
    > > > > > >
    > > > > > > HTH
    > > > > > > --
    > > > > > > AP
    > > > > > >
    > > > > > > "greasybeano" <nospam@farnbro.freeserve.co.uk> a écrit dans le

    > > message
    > > > > de
    > > > > > > news: 048F5407-2EB3-425A-A9E0-891E7C1F4690@microsoft.com...
    > > > > > > >I have taken the following code from the Dir help file which

    works
    > > ok
    > > > > at
    > > > > > > > first level.
    > > > > > > > However, I need to search for all the associated

    subdirectories
    > > but
    > > > > not
    > > > > > > > too
    > > > > > > > sure how to adapt the code. could someone be kind enough to

    assist
    > > > > please?
    > > > > > > > Many thanks.
    > > > > > > > --
    > > > > > > > GB
    > > > > > > >
    > > > > > > > Sub Finddir()
    > > > > > > > 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 '

    it
    > > > > > > > represents
    > > > > > > > a directory.
    > > > > > > > End If
    > > > > > > > End If
    > > > > > > > myname = Dir ' Get next entry.
    > > > > > > > Loop
    > > > > > > > End Sub
    > > > > > >
    > > > > > >
    > > > > > >
    > > > >
    > > > >
    > > > >

    > >
    > >
    > >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1