"Reoccurring Routine" / "Recursion technique"
This is post#post23774
https://www.excelfox.com/forum/showt...age2#post23774
https://www.excelfox.com/forum/showthread.php/2936-YouTube-Video-making-and-editing-etc-coupled-to-excelfox-(-windows-Movie-Maker-)/page2#post23774
Advanced File and Folder analysis coding - Reoccurring or Recursion Codings
Here are both the previous macro and that we are considering here, together. https://www.excelfox.com/forum/showt...ll=1#post23915 Together they form the advance solution being sicudded here
The first macro in this pair, as considered in the last post cannot run on its own since it would error when trying to set off ( Call ) the second macro. The second macro is the Recursion / reoccurring coding ,which we are considering here.
The second macro which we are considering here, has arguments that must be given, so is the form of macro that generally needs another macro to set off ( Call it ) and in doing those setting off / Calling, it passes it the required argument(s) In our example it is passing the full path to our Main Movie Maker program folder. This is what is needed to create the windows shell object, ( pseudo like code line, Set WindShellobjFolder = objShell.Namespace(The full path string to the program folder here) )
Part 1 Recursion / reoccurring coding
The word Recursionor recursion techniques in coding often puts people off, but I think it’s actually straight forward and easy to understand if explained fully but simply and in particular accurately: It seems that some wording is typically used in explanations, or used by people who understand it,when talking about it, and these words are at best incorrect and at worst confusing nonsense.For example, it is often at an early stage of explaintions, said that a code using recursion techniques Calls itself. That is inaccurate nonsense, or maybe something that can be justified to say technically but in a common sense point of view is inaccurate nonsense.
So forget anything you heard before about recursions and start again:
_1) The written code you see, for example in the VB Editor is just that. It’s just written code. It never runs, walks, or drives to the local shops to buy you any Buns. It can’t do anything at all. What actually runs coding is something with perhaps some limited similarity to the written coding somewhere deep in the innards of a computer. Some compile process makes the coding that will actually be run, and it uses the written code to determine what coding you wanted.
(Something similar that is important to realise, if you are familiar with the F8 Debug step mode, is that the F8 Debug step mode is extraorganised just for our Human eye convenience. We can’t actually see the coding being run in the step mode, but some extra software is used for our convenience to show approximately what part of the actual coding is running, since the actual coding is organised into a similarly set out series of instructions.)
_2) Because the actual used coding is not that written one, but rather coding made to the specifications of the written coding, many languages, such as that (VBA), coding associated with MS Office, is happy to make a few copies of the coding you want. Furthermore, whist we can’t easily run more than one copy at a time, we can curiously start another copy before the previous copy is finished. What happens then is that at the start of another copy, the previous copy is paused, and it resumes at the point it paused at,when the new copy is finished.
That’s it. If you understood that you know all about recursion. Perhaps it is just worth notingthat you are not limited to just a couple of copies. You can have a few of them, each one setting off another copy. When a copy sets off another copy, then it is paused and has to wait until the copy it set off is finished before it can continue. In order for this to work, a paused coding needs to work a bit differently to a coding that is finished. In particular, when a copy is paused, any variables that were filled have their values stored , "stacked", somewhere, so that the variables can be refilled when the paused copy restarts. This extra process has to be done because ordinarily variables within coding would usually lose their values when a code stops in the more usual way, which is at the end, when it is finished.
As far as the typical practical coding is concerned, generally at the code line that a copy of coding sets off another copy of the same coding,some sort of conditional If statement or similar is used , so that hopefully at some point all copies started get finished allowing the previous ones also at some point to finish. Otherwise the recursion / code reoccurring goes on for ever, or would do, if something does not get too big, or something runs out of space, leading to some error such as the famous "stack overflow" error.
A pictorial explanation may help here to re iterate the Recursion / Reoccurring coding process:
Here is a quick simple explanation of recursion with some emphasis on our actual next coding, Private Sub ReoccurringFileFolderProps(ByVal Pf As String)
We remember from the last post that, the coding being discussed in this post, Private Sub ReoccurringFileFolderProps(ByVal Pf As String) gets Called by this code line in the first macro,
Call ReoccurringFileFolderProps(Parf & "\Movie Maker")
So that tells us that whenit starts, itisalso "fed" / passed the full path to the Movie Maker Folder that we want analysed
Basic Code explanation, simplified
_A) When running more usually, no recursion / reoccurring coding.
In its simplest working, which it might do, for example the case of only files and no sub folders in the main Movie Maker folder, the codingis just our basic coding using the windows shell object to loop through all the items in the Movie Maker folder. Those items are all files, no sub folders, so the condition at ' 2b ,
If objFolder.GetDetailsOf(Fil, 2) = "Dateiordner"Then Call ReoccurringFileFolderProps(Pf & "" & objFolder.GetDetailsOf(Fil, 0))
, is not met
Effectively then, it is as if the ' 2b section is not there, and we are basically doing our most basic looping of all folder Items ( files in this case ), and listing some of their properties at each loop. So the == Main Loop == :below is the only significant coding
Code:
Private Sub ReoccurringFileFolderProps(ByVal Pf As String)
Rem 0
Let Reocopy = Reocopy + 1 ' Originally the variableReocopyis zero.It will become 1 on first entering the macro.Every time we leave this macro, this number is reduced by 1 So in simple use it will be 1 or zero indicating that a copy is in use.However, should this macro "Call itself", before its finished , ( the recursion idea ) then the vallue will be 2and so on.So effectively it tells us which copy is running at any time
Rem 1
Dim objShell As Shell32.Shell: Set objShell = New Shell32.Shell'https://i.postimg.cc/Fz9zrnNm/Tools-Referrences-Microsoft-Shell-Controls-And-Automation.jpg https://i.postimg.cc/sDC9S54h/Tools-Referrences-Microsoft-Shell-Controls-And-Automation.jpg
Dim objFolder As Shell32.Folder: Set objFolder = objShell.Namespace(Pf)'
Rem 2
Dim Fil As Shell32.FolderItem
For Each Fil In objFolder.Items ' ======= Main Loop ==================================================|
' Dim Clm As Long: ' Global variable
Let Clm = Clm + 1
Dim Rw As Long: Let Rw = 1
Let ActiveCell.Offset(Rw, Clm) = objFolder.GetDetailsOf(Fil, 0)' Name
Let ActiveCell.Offset(Rw + 1, Clm) = objFolder.GetDetailsOf(Fil, 1) ' Size
Let ActiveCell.Offset(Rw + 2, Clm) = objFolder.GetDetailsOf(Fil, 2) ' Type
Let ActiveCell.Offset(Rw + 3, Clm) = Left(objFolder.GetDetailsOf(Fil, 3), InStr(1, objFolder.GetDetailsOf(Fil, 3), " ", vbBinaryCompare)) ' Chang Date
Let ActiveCell.Offset(Rw + 4, Clm) = Left(objFolder.GetDetailsOf(Fil, 4), InStr(1, objFolder.GetDetailsOf(Fil, 4), " ", vbBinaryCompare)) ' Made Date
' Let ActiveCell.Offset(Rw + 9, 0) = "Dateiversion": Let ActiveCell.Offset(Rw + 9, 1) = objFolder.GetDetailsOf(Fil, 166)
Let ActiveCell.Offset(Rw + 5, Clm) = objFolder.GetDetailsOf(Fil, 166) ' File Version
'_________________________________________________________________________________________________
' 2bHere we may pause the macro, whilst another copy of it is started
'If objFolder.GetDetailsOf(Fil, 2) = "Dateiordner" Then Call ReoccurringFileFolderProps(Pf & "\" & objFolder.GetDetailsOf(Fil, 0))
'_________________________________________________________________________________________________
' If we did pause whilst the above code line set off another copy, then when that is finished we will come here and resume the paused previous copy
Next Fil ' ============================== Main Loop=================================================|
Let Reocopy = Reocopy - 1' We are finished at this point with this running copy of the macro.This code line will reduce the value indicating the copy number is running since this copy ends in the next line
End Sub
So now consider that at some point in the looping through the folder items, it did encounter a folder, in other words one of the items was a folder. What will happen now is that after the properties of that item ( the folder ) are outputted, we will carry out this line
Code:
Private Sub ReoccurringFileFolderProps(ByVal Pf As String)
Rem 0
Let Reocopy = Reocopy + 1 ' Originally the variableReocopyis zero.It will become 1 on first entering the macro.Every time we leave this macro, this number is reduced by 1 So in simple use it will be 1 or zero indicating that a copy is in use.However, should this macro "Call itself", before its finished , ( the recursion idea ) then the vallue will be 2and so on.So effectively it tells us which copy is running at any time
Rem 1
Dim objShell As Shell32.Shell: Set objShell = New Shell32.Shell'https://i.postimg.cc/Fz9zrnNm/Tools-Referrences-Microsoft-Shell-Controls-And-Automation.jpg https://i.postimg.cc/sDC9S54h/Tools-Referrences-Microsoft-Shell-Controls-And-Automation.jpg
Dim objFolder As Shell32.Folder: Set objFolder = objShell.Namespace(Pf)'
Rem 2
Dim Fil As Shell32.FolderItem
For Each Fil In objFolder.Items ' ======= Main Loop ==================================================|
' Dim Clm As Long: ' Global variable
Let Clm = Clm + 1
Dim Rw As Long: Let Rw = 1
Let ActiveCell.Offset(Rw, Clm) = objFolder.GetDetailsOf(Fil, 0)' Name
Let ActiveCell.Offset(Rw + 1, Clm) = objFolder.GetDetailsOf(Fil, 1) ' Size
Let ActiveCell.Offset(Rw + 2, Clm) = objFolder.GetDetailsOf(Fil, 2) ' Type
Let ActiveCell.Offset(Rw + 3, Clm) = Left(objFolder.GetDetailsOf(Fil, 3), InStr(1, objFolder.GetDetailsOf(Fil, 3), " ", vbBinaryCompare)) ' Chang Date
Let ActiveCell.Offset(Rw + 4, Clm) = Left(objFolder.GetDetailsOf(Fil, 4), InStr(1, objFolder.GetDetailsOf(Fil, 4), " ", vbBinaryCompare)) ' Made Date
' Let ActiveCell.Offset(Rw + 9, 0) = "Dateiversion": Let ActiveCell.Offset(Rw + 9, 1) = objFolder.GetDetailsOf(Fil, 166)
Let ActiveCell.Offset(Rw + 5, Clm) = objFolder.GetDetailsOf(Fil, 166) ' File Version
'_________________________________________________________________________________________________
' 2bHere we may pause the macro, whilst another copy of it is started
'If objFolder.GetDetailsOf(Fil, 2) = "Dateiordner" Then Call ReoccurringFileFolderProps(Pf & "\" & objFolder.GetDetailsOf(Fil, 0))
'_________________________________________________________________________________________________
' If we did pause whilst the above code line set off another copy, then when that is finished we will come here and resume the paused previous copy
Next Fil ' ============================== Main Loop=================================================|
Let Reocopy = Reocopy - 1' We are finished at this point with this running copy of the macro.This code line will reduce the value indicating the copy number is running since this copy ends in the next line
End Sub
Let’s consider for simplicity, the found folder has only files in it. We consider this fully in the next post.