【问题标题】:How do I get the path to the currently selected file如何获取当前选定文件的路径
【发布时间】:2013-12-02 11:00:26
【问题描述】:

VBScript 是否具有获取文件资源管理器中当前选定文件的路径的功能?如果有,它的功能是什么?我正在寻找类似的东西

Set fileObj = CreateObject("Scripting.FileSystemObject")
dim filepath 
filepath = fileObj.GetCurrentSelection() 'doesn´t exist
dim result
result = filepath 'communicate with LiveCode

【问题讨论】:

  • 不,它没有。根据您实际想要实现的目标,您可以添加一个上下文菜单条目,该条目使用所选对象的路径调用 VBScript。如果你解释了你需要这个做什么,你可能会得到更好的答案。
  • 感谢您的回复。我想为我现有的软件添加一些脚本功能,并允许获取有关当前选定文件、复制选定文件和其他基本操作的信息。我会考虑使用上下文菜单。
  • 如果您已经使用浏览文件夹选择了文件/文件夹,那么很容易获得选择的文件。
  • @Pankaj 你误解了这个问题。您的评论让未来的读者感到困惑。
  • 显然……我的错。

标签: vbscript selection explorer livecode


【解决方案1】:

我写了一个简单的例子。
请记住,可能有多个打开的 Windows 资源管理器窗口,这会将它们全部列出。

Function GetSelectedFiles() 'Returns paths as array of strings
    Dim FileList, Window, SelectedItem
    'avoid duplicates by storing paths in dictionary keys
    Set FileList = CreateObject("Scripting.Dictionary")

    With CreateObject("Shell.Application")
        For Each Window In .Windows
            'skip IE Windows
            If InStr(1, Window.FullName, "iexplore.exe", vbTextCompare) = 0 Then
                For Each SelectedItem In Window.Document.SelectedItems
                    FileList(SelectedItem.Path) = Null
                Next
            End If
        Next
    End With

    GetSelectedFiles = FileList.Keys 'array of paths
End Function

MsgBox  "Click OK after selecting the items",  _
        vbOKOnly Or vbInformation, "Select a few items"

Dim SelectedFiles
    SelectedFiles =  GetSelectedFiles

MsgBox  "You selected: " & vbNewLine  & vbNewLine & _
         Join(SelectedFiles, vbNewLine), vbOKOnly Or vbInformation, "Selected Items"

'loop through array
'Dim FileItem
'For Each FileItem In SelectedFiles
'   WScript.Echo FileItem
'Next

【讨论】:

  • 谢谢。我要试试这个。我会回复你的。
  • 我收到以下错误:Script: C\Users\USER\Desktop\path.vbs, Line: 10, Char: 17, Error: Object doesn't support this property or method: 'Windows.Document.SelectedItems', Code: 800A01B6, Source: Microsoft VBScript runtime error。这是在 Windows 8 上的。我还不能给你赏金,但它绝对是一个有趣的脚本。
  • @Mark 我应该说我以前从未使用过这种脚本,为你调查过。坦率地说,我不知道在什么情况下会导致错误。只是我可以说迭代的 Document 对象没有 SelectedItems 方法,但我还不知道为什么。我只在 Windows 2008 R2 (x64)、Windows 2008 x86、Windows 8.1 Pro x64(我的开发电脑)和 Windows 8 Pro x86 上测试了 scipt,没有出现任何错误。显然这还不够。我会在业余时间尝试重现错误。也许您可以知道在脚本运行时打开了哪些类型的 Windows 或 Internet Explorer 实例。它可能会有所帮助。
  • 我打开了一两个桌面窗口。这是 Windows 文件资源管理器,而不是 Internet Explorer。我不使用专业版。
  • @Mark 我什至在 Windows XP SP1(12 岁的操作系统)上尝试过,它可以工作,看看:i.imgur.com/BVcawLm.png 我唯一的猜测是你使用的 Windows 版本没有支持脚本中使用的接口。我不知道,它是入门版还是预览版,所以您知道我们无法确定。不幸的是,我没有任何其他建议或解决方案。对不起。
【解决方案2】:

试试这个,你可以得到当前选择的文件的路径。你还需要

Set objFS=CreateObject("Scripting.FileSystemObject")
    Set objArgs = WScript.Arguments
    strFile= objArgs(0)
    Set objFile = objFS.OpenTextFile(strFile)
    Set objFile = objFS.GetFile(strFile)
    WScript.Echo objFile.Path

【讨论】:

  • 错误:下标超出范围,代码:800A0009,来源:Microsoft VBScript 运行时错误,第 3 行,字符 4。
  • 命令是c:\test> cscript //nologo myscript.vbs myfile
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多