List files macro, need to add exclude specific subfolder in it

  • Hi,


    Here is some code to list files path, files names and date last modified from a specific folder. The folder path is on "e3" and the argument YES or NO is on e4, to include subfolders or not. I would like to add folders name on e5 (preferably separated by comma) that would be excluded from this list. Can you please help me write this code?



    Thank you!

  • Re: List files macro, need to add exclude specific subfolder in it


    For E4, use True or False or else change your If for IncludeSubfolders.


    For e5, what would the values be? e.g. t, Ken, Joe. If e3 contained x:\, would it exclude both x:\t and x:\Jerry\t and x:\Jerry\CaseFiles\t?


    I always use Option Explicit.


  • Re: List files macro, need to add exclude specific subfolder in it


    E4 is true or false you are right.


    To explain what I want, lets take an example. Lets say I have 4 subfolders in c:\test : I have folder "sub1","sub2","test" and "working files". I would like to exclude the folder named "test" and "working files". So I would like to just have to write in e5 : "test, working files" for it to exclude those 2 subfolders only. I just want the exclusion to be on the first subfolder level. I don't know if space in folders names will create an issue. Also can you explain me more about Option Explicit.


    Thank you for your help.

  • Re: List files macro, need to add exclude specific subfolder in it


    Option Explicit can be added for you in the VBE by Tools > Options > Editor > Require Variable Declaration. It forces you to Dim the variables. This also makes your variables to be the same case as specified in Dim no matter how you later type it.


    Change Split's() delimiter from "," to ", " if that is how E5 is set.


  • Re: List files macro, need to add exclude specific subfolder in it


    Thank you for your quick reply.


    I can't get this to work. How am I supposed to write the subfolders that I want to be excluded in cell e5?

  • Re: List files macro, need to add exclude specific subfolder in it


    Put the subfolder names in E5? As I said, I did it like this: Ken,temp,John,Case Files. You have to change the Split() as I explained if you want to exclude the subolder names like: Ken, temp, John, Case Files.


    But what if your E5 has mixed delimiters or you just don't know? Use the Replace function in Split. e.g.

  • Re: List files macro, need to add exclude specific subfolder in it


    Thanks again for your quick reply. For some reasons I can't get this to work.


    I have one main folder that is c:\test\


    I have 4 subfolders that have the following path c:\test\sub1, c:\test\sub2, c:\test\personal, c:\test\working files. I don't want files from folders personal and working files to be listed.


    In cell e3 I have the value : c:\test
    in cell e4 I have the value : TRUE
    in cell e5 I have the value : personal,working files


    I run the macro listfiles and the files inside folders personal and working files are still displayed. Please help.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!