+ Reply to Thread
Results 1 to 30 of 30

List files of folder and sub folders without Loop

  1. #1
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    List files of folder and sub folders without Loop

    Hi,

    Any way to list files of a folder and its sub folder if they start with ....(say "ImranBhatti*.xlsm") without looping.

    Why I want such a way:

    Because my main folder contains minimum 35 subfolders and each of them contains nearly 110 files and it takes too much time to loop.
    Note: I am also reading at least 4 properties of these files via "Shell.Application" inside the loop.


    Help would be greatly appreciated.

    Imran Bhatti
    Last edited by ImranBhatti; 10-04-2018 at 06:24 AM.
    Teach me Excel VBA

  2. #2
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: List files of folder and sub folders without Loop

    Any one please?

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: List files of folder and sub folders without Loop

    AFAIK, a loop is needed.
    But, loops can be made more efficient. Perhaps if we saw your current code, we could speed up your process.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: List files of folder and sub folders without Loop

    Thanks @sir mikerickson

    Currently I don't have access to my office PC.
    Will post it later.

    Another option that I am currently experiment on is

    I have created a .search-ms folder via vba which is showing all files in that folder.

    Now I am thinking to read folder but the problem is ".search-ms" folder is not actually a folder (its XML file rather , that looks list folder from inside but its icon and extension is not that of a yellow folder.

  5. #5
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: List files of folder and sub folders without Loop

    Quote Originally Posted by mikerickson View Post
    if we saw your current code, we could speed up your process.
    Please Login or Register  to view this content.

    As you can see a lot of modification is required to test it on PC other than mine. you will be suggesting theoretically how this sub routine can be made more robust.

  6. #6
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: List files of folder and sub folders without Loop

    Any idea please?

  7. #7
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: List files of folder and sub folders without Loop

    A few suggestions for you to consider:
    1. From personal experience & testing, FSO is a slow method for returning file names and should be avoided where possible if speed is important.
    2. Avoid the use of Like if speed is important. For example, replace Like with Left function e.g.
      Please Login or Register  to view this content.
    3. Avoid repeated use of same calculation in your code e.g. Environ("Username"). Calculate this value once, set it to a variable and use this variable instead.
    4. Avoid repeated calculations in the Excel sheet values (e.g. the replace function). Make the calculations in the array before pasting to the sheet.


    P.S. I am guessing you have had a bad experience with loops in the past. Loops are not necessarily a bad thing (although they can be if badly coded). Sometimes a loop is the most efficient and practical solution to a problem.
    Last edited by mc84excel; 10-14-2018 at 05:50 PM.
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  8. #8
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: List files of folder and sub folders without Loop

    Thanks mc4excel
    I tried changing Like to Left and Environ to my username in actual but seems no effect.

  9. #9
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: List files of folder and sub folders without Loop

    If you time the macro before and after these two changes, there should be a minor improvement (how noticable this is will depend on the sample size being tested)

    However the most noticable time savings would be from the other two tips:
    • using Dir over FSO to return files and/or folders (tip 1) was over 3x faster (tested on my own projects) and
    • doing calculations in array instead of workbook (tip 4) a lot faster (Theoretically up to 60x faster from others feedback)

    Disclaimer: Your mileage may vary. The savings you achieve probably wont be as high as the figures I quoted. I say that because your code will still need to use Shell and with Tip 4 you don't have a lot of workbook calculations and you already switched to Manual Calculation before doing these.

  10. #10
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: List files of folder and sub folders without Loop

    I will look into DIR function

    Can you please give me your thoughts on the last 3 lines of my post#4 in this thread?


    I have created a .search-ms folder via vba which is showing all files in that folder.

    Now I am thinking to read folder but the problem is ".search-ms" folder is not actually a folder (its XML file rather , that looks list folder from inside but its icon and extension is not that of a yellow folder.

  11. #11
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,911

    Re: List files of folder and sub folders without Loop

    Have you actually timed the parts of your code to see where the bottlenecks are?
    Rory

  12. #12
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: List files of folder and sub folders without Loop

    This 2 parts take too much time.

    Please Login or Register  to view this content.

  13. #13
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,911

    Re: List files of folder and sub folders without Loop

    That's not really specific enough. Comment out the GetDetailsOf lines and see how long it takes without them compared to with them.

  14. #14
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: List files of folder and sub folders without Loop

    these 2 loops took 45 seconds for while the rest of the code took 15 seconds. This duration is for 27 files in total from mainfolder and its sub folders.

  15. #15
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,911

    Re: List files of folder and sub folders without Loop

    How long do those two loops take without the GetDetailsOf lines, and how long with them?

  16. #16
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: List files of folder and sub folders without Loop

    With GetDetailsOf = 45 seconds
    without them = 1 seconds
    but what should I use as a substitute for GetDetailsOf

    I commented out the code like this

    Please Login or Register  to view this content.

  17. #17
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,911

    Re: List files of folder and sub folders without Loop

    Well, if I remember the numbers correctly (you should really put the details in the code as you probably won't remember later what 18 means!) the first tow are the file name and modified date? If so, you don't need GetDetailsOf for either of those as you can get them from the File directly.

  18. #18
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: List files of folder and sub folders without Loop

    Here are the names of properties that I am getting with GetDetailsOf
    Please Login or Register  to view this content.

  19. #19
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: List files of folder and sub folders without Loop

    Quote Originally Posted by rorya View Post
    you don't need GetDetailsOf for either of those as you can get them from the File directly.
    means with FSO?

  20. #20
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,911

    Re: List files of folder and sub folders without Loop

    Yes - like this:

    Please Login or Register  to view this content.

  21. #21
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: List files of folder and sub folders without Loop

    Quote Originally Posted by rorya View Post
    Yes - like this:

    Please Login or Register  to view this content.
    Great piece of advice @rorya. Thanks.That reduce the time from 15 seconds to 6 seconds for 28 files

    Can this method also get the following properties as well

    Please Login or Register  to view this content.

  22. #22
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: List files of folder and sub folders without Loop

    Hi Imram,

    If you use Power Query and select a folder, it will give you all the files in that folder with all the file attributes. You can then easily filter down to what you want. See:
    https://www.mssqltips.com/sqlservert...g-power-query/
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  23. #23
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,911

    Re: List files of folder and sub folders without Loop

    No, I'm afraid not (otherwise I'd have added it ).

  24. #24
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: List files of folder and sub folders without Loop

    Alright rory thanks a lot for your nugget of wisdom. I hope it will reduce time for 100's of files as well as it reduced 9 seconds for 28 files.


    @sir MarvinP

    I don't see the "Tags" and "Title" properties in the list of attributes in the tutorial. Can get these properties? with PQ?

  25. #25
    Registered User
    Join Date
    09-16-2017
    Location
    Agra
    MS-Off Ver
    2013
    Posts
    72

    Re: List files of folder and sub folders without Loop

    Quote Originally Posted by ImranBhatti View Post
    Hi,

    Any way to list files of a folder and its sub folder if they start with ....(say "ImranBhatti*.xlsm") without looping.

    Why I want such a way:

    Because my main folder contains minimum 35 subfolders and each of them contains nearly 110 files and it takes too much time to loop.
    Note: I am also reading at least 4 properties of these files via "Shell.Application" inside the loop.


    Help would be greatly appreciated.

    Imran Bhatti
    Hi Imran,

    While I understand you are looking for code , let me share a macro based file manager which I got and use it personally. It is quite robust as per my usage and does the job pretty well. You may also refer the macro written the same.

    It can help you to build index or any folder with each file in that folder and subfolder within to last node available.
    Attached Files Attached Files

  26. #26
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: List files of folder and sub folders without Loop

    Do the Tags and Titles show in a directory or file list when you use PQ? If they do then you can filter by them before doing your next step.

    Explain what Tags and Titles are and I'll look further.

  27. #27
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: List files of folder and sub folders without Loop

    Tags and title screenshot
    Attached Images Attached Images

  28. #28
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: List files of folder and sub folders without Loop

    Sorry Imram,

    I don't know how to pull those out of a directory listing. I do use Directory Opus and see I can display them, but don't know how Power Query would pull them out.

  29. #29
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: List files of folder and sub folders without Loop

    Thanks @sir MarvinP for trying so hard to help out at least I came to know that some of them can be retrieved by PQ.

  30. #30
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: List files of folder and sub folders without Loop

    Quote Originally Posted by Ravisca2003 View Post
    Hi Imran,

    While I understand you are looking for code , let me share a macro based file manager which I got and use it personally. It is quite robust as per my usage and does the job pretty well. You may also refer the macro written the same.

    It can help you to build index or any folder with each file in that folder and subfolder within to last node available.
    Thanks Ravisca for this tool.
    But as you know I was after some of the properties of the closed books, which are not taken care of in this tool

    But it can be handy when it comes to listing files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] VBA to loop through a folder and all sub folders opening excel files.
    By Lsxtrkiller in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-07-2017, 06:18 PM
  2. Get List of files name and path from a folder including all sub folders
    By bennyys in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-06-2017, 10:57 AM
  3. [SOLVED] macro to list files in folder and sub folders has stopped working
    By Lmel0611 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-29-2015, 09:12 PM
  4. Loop through several folders and rename files using a lookup list
    By papasmurfuo9 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-23-2015, 11:20 AM
  5. [SOLVED] Recursive List Folder include hidden & system folders and files
    By CobraLAD in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-07-2015, 03:45 PM
  6. Replies: 0
    Last Post: 10-15-2014, 04:37 AM
  7. Replies: 0
    Last Post: 07-13-2012, 10:51 PM

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