2015-04-20 163 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

感谢您使它成为一个简单的复制/粘贴! –