0

I have a macro that calls Chrome, however deploying this code on different machines is undermined by the full filepath for chrome being different on each machine. To mitigate this, I wanted to execute a one-time search for "chrome.exe" and to store the filepath as a variable. The below seems to work however the recursiveness generates a stack space issue (Run-time error 28).

This seems such a simple operation, and recursive searches are all over the usual forums, yet I cannot get it right!

Function Recurse(sPath As String) As String
Dim FSO As New FileSystemObject
Dim myFolder As Folder
Dim mySubFolder As Folder
Dim myFile As File
Application.ScreenUpdating = False
Set myFolder = FSO.GetFolder("C:\")
For Each mySubFolder In myFolder.SubFolders
    For Each myFile In mySubFolder.Files
        If myFile.Name = "chrome.exe" Then
            Debug.Print myFile.Name & " in " & myFile.Path
            Exit For
        End If
    Next
    Recurse = Recurse(mySubFolder.Path)
Next
Application.ScreenUpdating = True
End Function

Sub ChromeSearch()
Call Recurse("C:\")
End Sub
  • I would say that if you are using linux you could use the `which` command, but then I saw the `C:/` ... – joaumg Jan 14 '16 at 22:08
  • Just searched and found a [Windows type of which](http://stackoverflow.com/questions/304319/is-there-an-equivalent-of-which-on-the-windows-command-line) – joaumg Jan 14 '16 at 22:09

1 Answers1

0

Note you are passing in a parameter sPath to your recursive function.

You are then ignoring this parameter and searching the folder C:\ with each call. Now you have a recursive function with no terminating condition, so of course you're going to get a stack overflow exception.

Simply replace

Set myFolder = FSO.GetFolder("C:\")

with

Set myFolder = FSO.GetFolder(sPath)

And replace:

Recurse = Recurse(mySubFolder.Path)

With

Recurse = Recurse(sPath + "\\" + mySubFolder.Path);

(Or however you concatenate folders in this language)


On the topic of finding Chrome.exe, you would save a lot of time if you first looked in in two places:

  • "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
  • (The environment variable LOCALAPPDATA) + "\Google\Chrome\Application\Chrome.exe"
Andrew Shepherd
  • 40,674
  • 26
  • 128
  • 192
  • Thanks @AndrewShepherd. The suggested change FSO.GetFolder(sPath) returns a "path not found" error. – Jon Potter Jan 14 '16 at 22:23
  • @JonPotter - You must have to concatenate the sub path to the base path. I've added an extra line of code for you. (You'll probably have to modify it slightly - I don't even know what language you're working in :-) – Andrew Shepherd Jan 14 '16 at 22:32
  • Very much appreciate the help despite my school-boy errors. Its Excel VBA, which I'm reasonably adept at in most cases, however haven't touched VBA in years and this one's got right under my skin :| – Jon Potter Jan 15 '16 at 00:17