2015-04-20 60 views
1

好吧,我正在打一個,這裏應該是什麼簡單的VBscript函數。我的腳本的目標是獲取2個輸入值並找到匹配的子文件夾。然後我希望函數返回到該文件夾​​的路徑。以下是我所擁有的,但難以使其返回價值。它似乎沒有退出函數並返回值。任何對大腦的幫助都會很大。這是我到目前爲止。VBscript Recusive函數問題返回值

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
.... 
For Each f1 in objFolder.SubFolders 
    GetFolderName = GetFolderName(f1.Path,Computer) 
    If GetFolderName <> vbEmpty Then Exit For 
Next 
.... 
+0

正是我需要的。我太親近了! –

1

MC ND你可以嘗試這樣的:

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") 
+0

感謝您使它成爲一個簡單的複製/粘貼! –