2

Does VBScript have a function to get the path to the currently selected file in File Explorer? If so, what is the function? I'm looking for something like

Set fileObj = CreateObject("Scripting.FileSystemObject")
dim filepath 
filepath = fileObj.GetCurrentSelection() 'doesn´t exist
dim result
result = filepath 'communicate with LiveCode
Mark
  • 2,380
  • 11
  • 29
  • 49
  • 2
    No, it doesn't. Depending on what you actually want to achieve you could add a context menu entry that calls a VBScript with the path of the selected object. You may get better answers if you explained what you need this for. – Ansgar Wiechers Dec 02 '13 at 12:04
  • Thanks for the reply. I would like to add some scripting capabilities to my existing software and allow for getting information about the currently selected file, duplicating the selected file, and other basic operations. I'll think about using a contextual menu. – Mark Dec 02 '13 at 12:09
  • 1
    If you have selected the file/folder using Browse Folder, then it is easy to get the selected file. – Pankaj Jaju Dec 09 '13 at 14:58
  • @Pankaj you misunderstood the question. Your comment is confusing to future readers. – Mark Dec 09 '13 at 15:04
  • Apparently so ... my bad. – Pankaj Jaju Dec 09 '13 at 15:09
  • http://msdn.microsoft.com/en-us/library/windows/desktop/gg314982(v=vs.85).aspx could be helpful – TheBlastOne Jan 27 '14 at 22:23
  • Why do you need to speak to Windows explorer to get the selected file into your application? Why don't you use your existing application to have the user select a file? Or, have your application register a shell extension the way something like Winzip does. VBS isn't really capable or suitable for what you're talking about doing... – Jobbo Jan 29 '14 at 11:40
  • @Jobo because the user wants to be able to select a file in File Explorer and do something with it. – Mark Jan 29 '14 at 13:10
  • Anyone having an idea to resolve the error or perhaps a different approach? – Mark Jan 29 '14 at 20:12

2 Answers2

6

I wrote a simple example.
Keep in mind there may be more than one open windows explorer window and this will list them all.

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
Kul-Tigin
  • 16,728
  • 1
  • 35
  • 64
  • Thanks. I'm going to try this. I'll get back to you. – Mark Jan 24 '14 at 20:48
  • I get the following error: `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`. This is on Windows 8. I can't give you the bounty yet, but it is definitely an interesting script. – Mark Jan 24 '14 at 23:22
  • 1
    @Mark I should say I've never used this kind of script before, investigated for you. Frankly I don't know what causes the error under which circumstances. Just I can say that the iterated Document object has no SelectedItems method, but I don't know why yet. I tested the scipt only on Windows 2008 R2 (x64), Windows 2008 x86, Windows 8.1 Pro x64 (my dev pc) and Windows 8 Pro x86 with no errors. Apparently it's not enough. I'll try reproduce the error in my spare time. Maybe you can tell what kind of Windows or Internet Explorer instances were open while script working. It may helpful. – Kul-Tigin Jan 25 '14 at 08:01
  • I have one or two desktop windows open. This is Windows File Explorer, not Internet Explorer. I don't use Pro. – Mark Jan 25 '14 at 10:22
  • @Mark Of course it is not IE but Windows Explorer and IE shares same engine. So you can see that, there is an If condition to eliminate IE instances in the code. Just asked for that. Well, what edition you're using now. Home Premium, Home Basic or Starter (aka Bloody) Edition? I'll try to debug script in a virtual machine if necessary. And how about trying to run script after replacing the comment line `'skip IE Windows` with `On Error Resume Next`? Could you try it. – Kul-Tigin Jan 25 '14 at 10:35
  • Funny. The only clue about the edition of Windows I have is the possibility to upgrade to Pro. Nowhere I can find a reference to the edition I am currently using. In the System control panel, it just says "Windows 8.1 (c) Microsoft Coporation, Get more features...". – Mark Jan 25 '14 at 20:11
  • @Mark You can learn which edition you're on running `winver.exe` through `Win + R` keystroke. – Kul-Tigin Jan 26 '14 at 05:16
  • I know about that. I just says Windows 8, Microsoft Windows Version 6.4 (Build 9600), (c) 2013... etc. No mention of an edition. – Mark Jan 26 '14 at 13:14
  • It just says: "The Windows 8.1 operating system and..." :-) – Mark Jan 26 '14 at 17:38
  • Anyone having an idea to resolve the error or perhaps a different approach? – Mark Jan 29 '14 at 20:11
  • 1
    @Mark I even tried on Windows XP SP1 (12 years old OS) and it works, have a look at : http://i.imgur.com/BVcawLm.png My only guess is that the edition of Windows you're on does not support the interface(s) used in the script. I don't know, it is Starter or a kind of Preview edition so you know we were unable to determine. Unfortunately I have no any other suggestion or solution. Sorry. – Kul-Tigin Jan 29 '14 at 20:33
  • I guess this isn't going to be solved but hopefully your answer will eventually show me the way to a solution. I guess I should give you the reward. – Mark Jan 30 '14 at 17:52
  • Where is this `Document.SelectedItems` interface documented? I tried it and it really works, but I can't find documentation for it. – GetFree Jan 02 '15 at 18:35
  • @GetFree hey, it's here : http://msdn.microsoft.com/en-us/library/windows/desktop/bb774049%28v=vs.85%29.aspx – Kul-Tigin Jan 02 '15 at 18:55
1

try this by this you can get the path to the currently selected file.you also need to

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
Ferrakkem Bhuiyan
  • 2,741
  • 2
  • 22
  • 38