Generate dynamic expression from string

MBMSOFT

Registered User.
Local time
Today, 17:54
Joined
Jan 29, 2010
Messages
90
I'd like to generate dynamic expression from string
e.g.
from string
SF = subfolder ' varable
"or each" & SF & "in fso.folders.. "
to
Code:
For each subfolder in fso.folders ....
where subfolder is variable

Actually I need to examine contents in the parent folder which contains n not known subfolders. Is there some loop which is going to the end of folder root

Thanks
 
You might have to build a "translator" in your code. I think this is similar to building an AdHoc query builder in VBA. You probably need to build the string in steps.
 
The question in my mind is, what were you planning to do with that "FOR EACH" line when you built it? True, it is perfectly possible for you to build a whole freakin' module full of code by using appropriate VBA, but the trick is to get it to execute. Don't get me wrong, I'm not trying to rain on your idea, but I don't see where it is going. SQL and VBA are activated differently. I know how to run an SQL statement that was built dynamically, but I'm not entirely sure how to thread dynamic VBA into an execution stream unless there is more going on than just a single statement.
 
Not sure I get what you want. You want the parent folder for a file, but that folder may contain other files that you want to examine - but not the folders within that folder?

Or examine all the files in the parent folder of a file plus all the files in the child folders of that parent?

Or only examine all the files in each of the subfolders of the parent?

To do either of the latter will require a recursive function I believe. I have only done this once before, and if I recall, only to the first level (option 3). Was not fun for me.
 
Sorry. Just commenting so that I get to follow this thread. Interested to see where this is going.


Sent from my iPhone using Tapatalk
 
Actually I need to examine contents in the parent folder which contains n not known subfolders. Is there some loop which is going to the end of folder root
I am not sure of your real question, but there is plenty of recursive code out there to span a folder tree if that is what you are asking.
 
mbmsoft,

Please provide an example or two to clarify your requirement. Readers do not understand what exactly you are trying to accomplish.
 
There IS such a thing as writing recursive code in VBA. I've done it both for folder visitation and also for a family tree tracker. It absolutely IS possible to traverse a given tree, but you have to be careful with your call arguments and how they are passed (ByValue or ByRef). You also need to watch out for referencing external things like Public variables in a general module, since you only have ONE place to write back values in that case but might have lots of references to make from multiple levels of the recursion.

Definitely, if you want to do this as an "explorer" then you need to read some reference articles on recursion. It's not something for the true beginner.

https://docs.microsoft.com/en-us/of...getting-started/creating-recursive-procedures

This link gives you the barest-bones concept. But searching the web for "Recursive" or "Recursion" will give you lots of articles.
 

Users who are viewing this thread

Back
Top Bottom