+ Reply to Thread
Results 1 to 16 of 16

Listing folders and subfolders

  1. #1
    Valued Forum Contributor
    Join Date
    10-13-2010
    Location
    Sunderland, England
    MS-Off Ver
    Excel 2007 (Home) / 2003 (Work)
    Posts
    740

    Listing folders and subfolders

    Hi guys, I'm after some help.

    I basically need to create a list of directories and subdirectories within a specific folder, for instance:

    C:\Folder\
    C:\Folder\Subfolder1\
    C:\Folder\Subfolder2\
    C:\Folder\Subfolder1\File1\
    C:\Folder\Subfolder1\File2\
    C:\Folder\Subfolder2\File1\
    C:\Folder\Subfolder2\File2\

    While I'm fairly competent with VBA within the confines of a spreadsheet, I don't know anything about folder structure-type programming, but I found something that kind of works (posted by Pike) here:http://www.excelforum.com/excel-prog...m-folders.html

    However this lists all files, whereas I don't need to go that deep.

    A further problem, which I found while running the above, is that I don't have access to a number of these directories. On an occasion where access is denied, this code stops. But I'd prefer if it just listed something like:

    C:\Folder\Subfolder2\File2\FORBIDDEN

    And moved onto the next folder.

    Can anyone give me something that could work, or at least point me in the direction of a good walkthrough guide for this kind of programming?
    Last edited by brokenbiscuits; 11-10-2011 at 11:33 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    10-13-2010
    Location
    Sunderland, England
    MS-Off Ver
    Excel 2007 (Home) / 2003 (Work)
    Posts
    740

    Re: Listing folders and subfolders

    Ok, I found a solution to the first part of my problem here (listing just folders instead of files):

    http://www.excelforum.com/excel-prog...r-folders.html

    However, I still have the problem of not being able to map, or at least highlight, subfolders that I do not have access to.

    I've added an On Error Resume Next to ensure the code skips to the next folder rather than stopping, but as I said earlier, I'd much rather it paste a line along the lines of:

    C:\Folder\Subfolder2\File2\FORBIDDEN

    Is this possible?

    Please Login or Register  to view this content.

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Listing folders and subfolders

    Hello brokenbiscuits,

    What do you mean by "access"? That the folder is hidden?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Valued Forum Contributor
    Join Date
    10-13-2010
    Location
    Sunderland, England
    MS-Off Ver
    Excel 2007 (Home) / 2003 (Work)
    Posts
    740

    Re: Listing folders and subfolders

    No, by this I mean that this is a shared drive at work, and some folders, when clicked on in the explorer view, will come up with a message along the lines of:

    C:\blah\blah is not accessible.

    Access is denied.


    Obviously I don't want to circumvent any privacy issues, I don't need to see what files or subfolders are in the folder, I just need to be able to see which folders I can't access so I can raise this information.

    Any help much appreciated (as was your original macro! )

  5. #5
    Valued Forum Contributor
    Join Date
    10-13-2010
    Location
    Sunderland, England
    MS-Off Ver
    Excel 2007 (Home) / 2003 (Work)
    Posts
    740

    Re: Listing folders and subfolders

    When I run in VBA without the If Error Resume Next statement, I get the following error:

    Runtime Error '70':

    Permission Denied


    So what I'm basically wanting is some code I can use instead of the If Error Resume Next , along the lines of If Error then print Folder.Path & "DENIED"

    Does that make sense?
    Last edited by brokenbiscuits; 11-10-2011 at 01:41 PM.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Listing folders and subfolders

    Hello brokenbiscuit,

    So, you want to list only the subfolders of a folder and not subfolders of those subfolders and flag any subfolder that you don't have access to, correct?

  7. #7
    Valued Forum Contributor
    Join Date
    10-13-2010
    Location
    Sunderland, England
    MS-Off Ver
    Excel 2007 (Home) / 2003 (Work)
    Posts
    740

    Re: Listing folders and subfolders

    I want to list as many layers of subfolders (subfolders of subfolders of subfolders...) that I have access to, and flag any folders or subfolders that have restricted access.

    So your original macro works perfectly for the first part.
    Last edited by brokenbiscuits; 11-10-2011 at 01:45 PM.

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Listing folders and subfolders

    Hello brokenbiscuits,

    I have been looking through my files. I have a macro to list all files in a folder and its subfolders. So, I will make some changes to make list and check just the folders.

  9. #9
    Valued Forum Contributor
    Join Date
    10-13-2010
    Location
    Sunderland, England
    MS-Off Ver
    Excel 2007 (Home) / 2003 (Work)
    Posts
    740

    Re: Listing folders and subfolders

    Well, like I say, the macro I use above is yours, I found it here:
    http://www.excelforum.com/excel-prog...r-folders.html

    and that almost works, all I'm basically wanting is some code I can use instead of the If Error Resume Next , along the lines of If Error then print Folder.Path & "DENIED"

    So I have every faith in you.

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Listing folders and subfolders

    Hello brokenbiscuits,

    Try this macro. It works for the all the folders, but I can't test the "Forbidden" part. I have access to everything on my system. This will list the folders on the ActiveSheet starting in cell "A1".
    Please Login or Register  to view this content.

    Calling the Macro
    Please Login or Register  to view this content.

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Listing folders and subfolders

    Hello brokenbiscuits,

    Okay, I tested this some more and it has problems. If you are getting errors, it isn't your fault.

  12. #12
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Listing folders and subfolders

    Hello brokenbiscuits,

    I rewrote the macro from scratch. This version is more flexible. It allows to specify the output cell in the macro call.

    Recursively List Folders and Subfolders in a Directory
    Please Login or Register  to view this content.

    Macro Example
    List the folders on "Sheet1" starting in cell "A1".
    Please Login or Register  to view this content.

  13. #13
    Valued Forum Contributor
    Join Date
    10-13-2010
    Location
    Sunderland, England
    MS-Off Ver
    Excel 2007 (Home) / 2003 (Work)
    Posts
    740

    Re: Listing folders and subfolders

    I really appreciate your help Leith, but this macro doesn't work - this stops after the first error.

    I changed the error number in the errhandler to 70, and this then works, but it brings back the same results as the previous macro, just skipping over the forbidden folders rather than highlighting them as such.

    I understand it'll be difficult to test these things with you obviously having access to all of your system, so I may have to start exploring more manual processes.

    Again, thanks for all your help so far.

  14. #14
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Listing folders and subfolders

    Please Login or Register  to view this content.
    Last edited by snb; 11-11-2011 at 05:08 AM.



  15. #15
    Valued Forum Contributor
    Join Date
    10-13-2010
    Location
    Sunderland, England
    MS-Off Ver
    Excel 2007 (Home) / 2003 (Work)
    Posts
    740

    Re: Listing folders and subfolders

    Hi Snb

    The command prompt has been disabled by my administrator, so this doesn't seem to work for me. It also seems to be reliant on a file name, whereas I just want to give a high-level directory and pull all lower-level directories from this.

    Of course, I don't really understand your code, so it may be that it DOES work and I'm not doing something right. In that case, would you maybe be able to break it down a little for me?

    Thanks for your help either way!

  16. #16
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Listing folders and subfolders

    The code only lists foldernames.

    As you can see you don't have to use the commandprompt, because the code will do that for you.

    Did you put the code in any macromodule in an Excel Workbook and run it ?
    The result of the code will be a workbook containing all foldernames on drive C.

    You may also need to adapt the path & name of the file in which the result will be put.
    You can run the code step by step alt_F8 step into...
    Last edited by snb; 11-11-2011 at 09:19 AM.

+ 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