0

所以我有以下代码来包装 Shell.Application.BrowseForFolder() 方法。它改编自http://blogs.technet.com/b/heyscriptingguy/archive/2005/06/17/how-can-i-show-users-a-dialog-box-that-only-lets-them-select -文件夹.aspx

Function BrowseFolder( myStartLocation, blnSimpleDialog, strPrompt )
    Const MY_COMPUTER   = &H11&
    Const WINDOW_HANDLE = 0 ' Must ALWAYS be 0

    Dim numOptions, objFolder, objFolderItem
    Dim objPath, objShell, strPath ', strPrompt

    ' Set the options for the dialog window
    'strPrompt = "Select a folder:"
    If blnSimpleDialog = True Then
        numOptions = 0      ' Simple dialog
    Else
        numOptions = &H10&  ' Additional text field to type folder path
    End If

    ' Create a Windows Shell object
    Set objShell = CreateObject( "Shell.Application" )

    ' If specified, convert "My Computer" to a valid
    ' path for the Windows Shell's BrowseFolder method
    If UCase( myStartLocation ) = "MY COMPUTER" Then
        Set objFolder = objShell.Namespace( MY_COMPUTER )
        Set objFolderItem = objFolder.Self
        strPath = objFolderItem.Path
    Else
        strPath = myStartLocation
    End If

    Set objFolder = objShell.BrowseForFolder( WINDOW_HANDLE, strPrompt, _
                                              numOptions, strPath )

    ' Quit if no folder was selected
    If objFolder Is Nothing Then
        BrowseFolder = ""
        Exit Function
    End If

    ' Retrieve the path of the selected folder
    Set objFolderItem = objFolder.Self
    objPath = objFolderItem.Path

    ' Return the path of the selected folder
    BrowseFolder = objPath
End Function

这是调用它的代码:

Function GetSLOCDir()
   Dim FSO
   Dim slocDir
   Dim cwd

   Set FSO    = GetFSO()

   slocDir = ""
   While (slocDir="" OR NOT FSO.FolderExists(slocDir))
      cwd        = FSO.GetAbsolutePathName(".")
      slocDir    = BrowseFolder(cwd, True, "Select the SLOC Directory.")
      WScript.Echo "slocDir: " & slocDir

      If (slocDir="") Then
         WScript.Echo "Aborted."
         WScript.Quit
      End If

   WEnd
   GetSLOCDir = slocDir
End Function

第一次循环时,会显示浏览对话框。如果我单击“新建文件夹”,它会创建一个新文件夹,允许我输入文件夹名称。但是当我按下回车键时,它会回到说“新文件夹”,但在文件系统上它会正确重命名它。但是,BrowseFolder() 函数返回“新文件夹”,这会导致循环迭代。新的浏览对话框显示正确的文件夹名称,然后我可以选择它。

然而,这似乎很笨拙。这是 BrowseForFolder 方法中的错误吗?

谢谢

4

1 回答 1

0

首先对不起我的英语。您可以使用 cscript.exe 或 wscript.exe 的 64 位版本运行此脚本。32位版本有这个bug。

于 2013-11-13T08:48:58.200 回答