Hi Cheeseburger,
The Link given by MarvinP is an adoption of a very common way to Loop through a Folder and all its Subfolders. It basically uses the “SubFolders collection get at all Sub Folders in a recursion process. using Microsoft Scripting Runtime File System Object ” Way.
I have put some explaining ‘Comments in a version for you.
It is a nice demo.
In the original adaption the codes simply lists for you all Sub Folders within an initial Folder. The Initial Folder you can Pick when the code runs as it initially brings up a dialogue box (msoFileDialogFolderPicker). This is done in the First Main Procedure. As written in the Link the Sub then calls the Function which lists out all your Sub Folders within that Initial Folder, and all Sub Folders within those Sub folders, and all Sub Folders within Those Folders, and all Sub Folders within Those Folders, and all Sub Folders within Those Folders... I think you get the point!
There is also in that Link a second Function shown . This does the same but just has an extra bit in the Function to simply loop through and display out the name of each File. So it is Looping through all files in a Folder and its Subfolders. This is what you basically want to do!!
I have Just Called that Function (Function LoopThroughEachFolderAndItsFile ) in my Main Procedure (Sub GetFolderStructure AlanWonk () ), and I just use that and have modified it all a bit to maybe make it all a bit clearer
The code will usually run on the ActiveSheet, listing out all Folders in column A and B and all Files alongside the relavent Folder in Column C. So make sure you have a spare sheet Active when you run the code.
I would recommend you run the code in F8 to see what is going on. The only tricky thing to understand is line 40 in Function LoopThroughEachFolderAndItsFile.
This is an example of recursion. It is actually very simple once you understand it. But it is just incredibly difficult to put in words.
It is basically a Procedure that keeps calling itself as much as necessary as it goes "along", "down", or "to the right" of the Path "roots". Every time it goes off calling itself VBA runs a copy of that Procedure. It “Stacks” all info carefully for each “Copy” Run, in order and continues to do this "drilling" down as far as it must, in this case finding the Next Folder, and then the next Folder in that, then the next Folder in that, then the next Folder in that...I think you get the point! After “drilling down” it comes back up, in reverse order from where it currently is, carefully selecting copies of any variables taken from the appropriate stack. It may then go back down again a bit if it hits a folder “along” that has Sub Folders in it.
The code is a very nice demo actually. If you pick your sample Folders and sub Folders carefully, then the Pasted out results show nicely what is going on in the recursion process, and the Finally output has nice resemblance to a typical sketch of a Files Exploring Diagram.
You really must take the time to go through the demo codes in F8 Debug Mode, and you will soon get the point and wonder why there is any mystery to recursion as it is simple once you follow carefully through a specific example,
Your requirement comes up a lot and has been done for specific examples a lot at this Forum. You do not always catch them in a search as the title may not reflect too well the requirement. This is just one I know because i did it..
http://www.excelforum.com/excel-prog...ubfolders.html
Some where there in all the codes is an identical demo, just using Message boxes to tell you the Folder and file names
Main Procedure ___ Sub AAARRRGH()
Called Function ___ Sub DoOneFolderDemo
Further in that thread a specific example is done both involving getting stuff from the files in two File versions one which opens them files, and one which Gets the same without opening the Files – the Latter example is basically what you are doing with your GetValue Function.
At the end of the day, the adaption to any requirement is quite simple. In my codes here for example, in brief, you would replace my line 34 with your original code that does stuff to each found file. ( I give a bit more detail to that below *** )
The link given to you from “
Sixthsense
” is more of the same..Just in that Link no “demo” is done in the code, that is to say File names are not listed out, no message boxes come up etc...
It simply has a space with a comment (' Operate on each file ) saying where you would put your “doing stuff code” ( Equivalent of replacing my line 34 with your stuff )
Also in that code you are not asked for your main folder so you have to change appropriately the line
HostFolder = "C:\"
( there is also a a non-recursive method there. I have not tried that yet )
My Codes with some explanations I put here for you:
http://www.excelforum.com/developmen...ml#post4316795
*** Coming back to your requirement. You need to do approximately the following.
( p in your code is your initial main Folder, which my code asks you for. ) So you simply select that in my code and do not need to do a p anywhere - Or rather, your p is my ObjFolder.path
And your f is something like my Right(objFldLoop.path, Len(objFldLoop.path) - InStrRev(objFldLoop.path, "\")) - this latter returns the File Name including the bit after the . Dot but without all the Full Path stuff before it
You need a quick check before you do stuff to check you have a good Extension like you want.
( I made a quick start for you just after Line 34. )
Then you add the code lines to do your Get value stuff
That's it!.....
I think that should make an adaption to your code easy. It is difficult for us to do it all for you as we would have to make up a whole lot of Files to check.
I think if initially you run my code before you modify you will get a nice neat listing of all your files.
If you then get a successful adaption of your code, it would be nice if you could paste that code to us and the initial File listing. That would be a nice working example of that way of getting stuff from a file without opening it. I did that a few times in Threads and i had a feeling “not a lot of people knew about that sort of thing”
Alan
P.s.
Here are Your Codes from your uploaded text File: ( you are basically using the Dir way, which is, I think, not so easy to use as the Microsoft Scripting Runtime File System Object way when needing the extra bit of looking at Sub Folders )
Bookmarks