【问题标题】:Batch command for Browse Folder浏览文件夹的批处理命令
【发布时间】:2011-09-03 02:10:53
【问题描述】:

我是批处理脚本的新手。我想以编程方式浏览文件夹,然后将当前目录的内容复制到指定目录。

我有一个允许我浏览文件夹的 VBScript 脚本(如下)。如何将批处理文件与它链接?也就是说,在运行脚本并选择文件夹后,文件应移动到所选文件夹。有可能吗?

这是我用于浏览文件夹的 VBScript 代码:

Option Explicit

WScript.Echo BrowseFolder( "C:\Program Files", True )
WScript.Echo BrowseFolder( "My Computer", False )
WScript.Echo BrowseFolder( "", False )


Function BrowseFolder( myStartLocation, blnSimpleDialog )
' This function generates a Browse Folder dialog
' and returns the selected folder as a string.
'
' Arguments:
' blnSimpleDialog   [boolean] if False, an additional text field will be
'                             displayed where the folder can be selected
'                             by typing the fully qualified path
'
' Returns:          [string]  the fully qualified path to the selected folder
'
' Based on the Hey Scripting Guys article
' "How Can I Show Users a Dialog Box That Only Lets Them Select Folders?"
' http://www.microsoft.com/technet/scriptcenter/resources/qanda/jun05/hey0617.mspx
'
' Function written by Rob van der Woude
' http://www.robvanderwoude.com
    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

【问题讨论】:

标签: vbscript batch-file directory


【解决方案1】:

您想批量使用与 VBScript 相同的函数吗?如果是这样,我认为您不能将 OpenFileDialog 与批处理一起使用,使用 Visual Studio 和 C# 非常容易(值得一看)。如果你想批量做,你可以使用这个:

set /p path=Enter folder path: 
xcopy /e %cd% %path%

【讨论】:

    【解决方案2】:

    如果您真的想使用批处理,可以将值传递给环境变量或将其写入 io.out 以通过管道输出值,但这些都不可取。您还可以将值写入临时文本文件并让您的批处理将其用作输入。 最好和最简单的解决方案是在您的脚本本身中进行复制,为此提供大量示例,还有更多响应错误条件的可能性。 如果找不到,请告诉我,我有一个正在使用,但需要在发布前删除一些数据..

    【讨论】: