1

I am fighting what should be simple VBScript function here. The goal of my script is to take 2 input values and find the matching subfolder. I then want the function to return the path to that folder. Below is what I have, but am having trouble making it return the value. It doesn't seem to be exiting the function and returning the value.

Here is what I have so far.

Function GetFolderName(folderspec,Computer)
    WScript.Echo "checking: " & folderspec
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objFolder = fso.GetFolder(folderspec)
    If UCase(objFolder.Name) = UCase(Computer) Then
        GetFolderName = folderspec
        Exit Function
    End If
    Set arrSubfolders = objFolder.SubFolders
    For Each f1 in arrSubfolders
            folderspec = GetFolderName(f1.Path,Computer)
    Next
End Function

strFolder = GetFolderName("C:\Test","Trial3")

2 Answers 2

2
....
For Each f1 in objFolder.SubFolders
    GetFolderName = GetFolderName(f1.Path,Computer)
    If GetFolderName <> vbEmpty Then Exit For
Next
....
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I needed. I was so close!
1

Like MC ND you can try like this :

Function GetFolderName(folderspec,Computer)
'WScript.Echo "checking: " & folderspec
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objFolder = fso.GetFolder(folderspec)
    If UCase(objFolder.Name) = UCase(Computer) Then
        GetFolderName = folderspec
        Exit Function
    End If
    Set arrSubfolders = objFolder.SubFolders
    For Each f1 in objFolder.SubFolders
        GetFolderName = GetFolderName(f1.Path,Computer)
        If GetFolderName <> vbEmpty Then Exit For
    Next
End Function
'**********************************************************************************************
MsgBox GetFolderName("C:\Test","Trial3")

1 Comment

Thanks for making it an easy copy/paste!

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.