【问题标题】:VBScript to open a dialog to select a filepathVBScript 打开一个对话框来选择一个文件路径
【发布时间】:2014-02-04 17:52:24
【问题描述】:

目前我正在用我的 vbscript 打开一个文件,如下所示:

strFile = "C:\Users\test\file.txt"
Set objFile = objFSO.OpenTextFile(strFile)

我想更改此设置,以便用户可以选择/导航到一个文件,并且该文件在脚本中使用。我怎样才能添加这个能力?我试图搜索如何加载文件对话框/提示用户输入文件等,只是不确定如何在 VBScript 中完成。

【问题讨论】:

    标签: vba vbscript filepath


    【解决方案1】:

    我从MS TechNet 中发现了另一个有趣的解决方案,减少了自定义,但得到了您想要实现的目标。这将返回所选文件的完整路径。

    Set wShell=CreateObject("WScript.Shell")
    Set oExec=wShell.Exec("mshta.exe ""about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);</script>""")
    sFileSelected = oExec.StdOut.ReadLine
    wscript.echo sFileSelected
    

    【讨论】:

    • 谢谢。在 64 位 Windows 10 Pro 上成功测试。
    • 我喜欢这个解决方案,是否可以将过滤器设置为文件类型(即 *.txt)?
    • @Erik 这个Old SO post 可以帮助你。
    • @Erik 不知何故,我不喜欢它。您需要以这种方式执行此操作似乎有点笨拙。我遇到的所有其他 VBS 解决方案都使用 Windows 资源管理器未提供的奇怪版本来选择文件。但由于它只是 HTML,因此您可以像在 HTML 文档中一样过滤掉任何文件。
    • 不错。这是一个基于相同代码的注释函数:robvanderwoude.com/vbstech_ui_fileopen.php#ExecMSHTA
    【解决方案2】:

    给你:

    http://www.robvanderwoude.com/vbstech_ui_fileopen.php

    strFile = GetFileName("C:\Users\test\", "Text files|*.txt")
    Set objFile = objFSO.OpenTextFile(strFile)
    
    Function GetFileName( myDir, myFilter )
      ' Written by Rob van der Woude
      ' http://www.robvanderwoude.com
    
      ' Standard housekeeping
      Dim objDialog
    
      ' Create a dialog object
      Set objDialog = CreateObject( "UserAccounts.CommonDialog" )
    
      ' Check arguments and use defaults when necessary
      If myDir = "" Then
        ' Default initial folder is "My Documents"
        objDialog.InitialDir = CreateObject( "WScript.Shell" ).SpecialFolders( "MyDocuments" )
      Else
        ' Use the specified initial folder
        objDialog.InitialDir = myDir
      End If
      If myFilter = "" Then
        ' Default file filter is "All files"
        objDialog.Filter = "All files|*.*"
      Else
        ' Use the specified file filter
        objDialog.Filter = myFilter
      End If
    
      ' Open the dialog and return the selected file name
      If objDialog.ShowOpen Then
        GetFileName = objDialog.FileName
      Else
        GetFileName = ""
      End If
    End Function
    

    【讨论】:

    • 有助于测试,或者至少提到 UserAccounts.CommonDialog 在任何 64 位主机上不可用的警告。
    【解决方案3】:

    VBSEdit 程序安装一个 COM 库 VBSEdit Toolkit,它允许打开文件对话框。

    来自 VBSEdit 帮助:

    Dialog boxes 
    OpenFileDialog method
    Prompt the user to open a file
    toolkit.OpenFileDialog ([initialFolder,[filters,[multiselect,[title]]]]) 
    
    'Opens a single file
    Set toolkit = CreateObject("VbsEdit.Toolkit")
    files=toolkit.OpenFileDialog("c:\scripts\","Text Files (*.txt)|*.txt",False,"Open a text file")
    If UBound(files)>=0 Then
      WScript.Echo files(0)
    Else
      Wscript.Quit
    End If
    
    'Opens multiple files
    Set toolkit = CreateObject("VbsEdit.Toolkit")
    files=toolkit.OpenFileDialog("c:\scripts\","Text Files (*.txt)|*.txt",True,"Open a text file")
    If UBound(files)>=0 Then
      For Each filepath In files
        WScript.Echo filepath
      Next
    Else
      Wscript.Quit
    End If
    
    
    
    SaveFileDialog method
    Prompt the user to save a file
    toolkit.SaveFileDialog ([initialFolder,[initialFilename,[filters,[title]]]]) 
    
    Set toolkit = CreateObject("VbsEdit.Toolkit")
    filepath = toolkit.SaveFileDialog("c:\scripts","test.txt","Text Files (*.txt)|*.txt")
    
    If Not(IsNull(filepath)) Then
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Set objFile = objFSO.CreateTextFile(filepath,True)
      objFile.WriteLine Date
      objFile.Close
    Else
      Wscript.Quit
    End If
    
    
    
    SelectFolder method
    Prompt the user to select a folder
    toolkit.SelectFolder ([initialDir,[title]]) 
    
    Set toolkit = CreateObject("VbsEdit.Toolkit")
    myfolder=toolkit.SelectFolder("c:\scripts\","Please select a folder")
    
    If Not(IsNull(myfolder)) Then
      WScript.Echo myfolder
    Else
      Wscript.Quit
    End If
    

    (其实不用VBSEdit工具包也可以打开文件夹选择对话框,用Shell.Application对象的BrowseForFolder方法。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-05
      • 2015-12-14
      • 1970-01-01
      • 1970-01-01
      • 2013-10-30
      相关资源
      最近更新 更多