1

I am looking for some expert insight concerning recursion within vbscript. From various examples found online I created the following code, which works by the way.

My question is: Is this a good aproach when it comes to creating a recursive function (using a parameter for storing previous results)?

I prefer putting this in a (self-contained) "function", so the procedure returns the subsubfolders as the result. But most of the examples found use a "sub" I always get confused when it comes to "sub" vs "function" (I understand when you want a procedure that needs to return something you use a function, imho this seems to be the case in this example) But I could also use a "sub" and just simple reference the output parameter (ByRef pSubfoldersList)

So what is the best practise or is it better to use a whole different approach all together? (the function is this examples is also very slow compared to [shell.exec "cmd /c dir RootFolder /s /b /a:d"], I guess this is a side effect from the recursion or maybe the FSO is just really slow?)

1 Answer 1

1

whether it is good practice to pass the result in a recursive function, i don't really know, you can test this out by doing it this way and the other and comparing the time and memory taken. Haven't tried this with your version cause i get the error "Microsoft VBScript runtime error: Permission denied" if I start from the root of the c:

The real problem with your solution is the concatenation, that takes time because the in your case BIG variable gets created every time. Better were to store the result in an array or in the case of VBscript in a dictionary. I'll post an example.

What the difference between sub and function concerns: you are right about the main difference, the returning of a result but that is optional so I always use functions, the only drawback is that if you don't assign the value to a variable and you use more than 2 parameters you have to use "call". When you use your approach with ByRef you could also define the var in the main global context, it's perhaps less encapsulated but more readable and you can more easily reuse or debug the results.

What the speed concerns: vbscript is VERY slow in file handling, if you used WMI perhaps you could speed up a bit but not much, indeed for some operations it is better to shell out and let the OS take care of it. I now program in Ruby and there most jobs like this you can write in one line of code and it is much faster.

Speaking about fast, if your only purpose is to have a list of your files, get to know the tool "search everything", in less than a second you can search millions of files, if you don't know it check it out !

Here is an example using the dictionary

set fso = CreateObject("Scripting.FileSystemObject")
set filelist = CreateObject("Scripting.Dictionary")
iCount = 0
ShowSubfolders fso.GetFolder("C:\Documents and Settings\peter")
PrintFilelist(filelist)
'--- ---
Function ShowSubFolders(Folder)
  For Each Subfolder in Folder.SubFolders
    on error resume next
    wscript.echo Subfolder.Path 'show some progress
    Set fFolder = fso.GetFolder(Subfolder.Path)
    if err.number <> 0 then wscript.echo err.description
    For Each File in fFolder.Files
      iCount = iCount+1
      filelist.add iCount, File.Path
    Next
    ShowSubFolders Subfolder
  Next
End Function
'--- ---'
Function PrintFilelist(ByRef dic)
  Dim index, allKeys, allItems, msg
  allKeys = dic.Keys
  ' allKeys is an array to all the keys
  allItems = dic.Items
  ' allItems is an array to all the items
  wscript.echo "There are " & dic.Count & " number of files in the dictionary"
  For index = 0 To dic.Count-1
    ' Notice, dictionary range goes from 0 to count-1
    wscript.echo "Key=" & allKeys(index) & " Filename=" & allItems(index)
  Next
End Function
Sign up to request clarification or add additional context in comments.

2 Comments

(mail notify doesn't seem to work :-( )Thanx for your reply, those are definitely some useful tips. I'll certainly check out "search everything" For this example it is true I simply wanted to create a list of files within a folder and all it's sub folders. But now I am intrigued by how to create a recursive function Maybe you can tell me how you would do it in Ruby? The Specific problem I have with vbscript is that it doesn't seem possible to define a public variable within a function, so what options are left to store the previous result of a (self contained) recursive functions?
well, it depends on how you define "self contained", in my answer above i use iCount as a global var which you can access in your functions, otherwise you have to pass the variable to another function (or the same in case of recursion) by reference like you did. The same applies to all languages i know of, i advise Ruby because it is also a scripting language but with all the power of compiled languages and yet easily to use and learn.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.