Getting the current selection from an Explorer window

Today's Little Program prints the current selection in all open Explorer windows. (This is an alternative to the C++ version that involves a ridiculous amount of typing.)

var shellWindows = new ActiveXObject("Shell.Application").Windows();
for (var i = 0; i < shellWindows.Count; i++) {
 var w = shellWindows.Item(i);
 var sel = w.Document.SelectedItems();
 for (var j = 0; j < sel.Count; j++) {
  var item = sel.Item(j);

I have no idea why you would want to do this, but there you have it. (If you want the focused item rather than the selection, then get the Focused­Item property.)

Okay, maybe you can use this as a quick-and-dirty way to get the parsing name for a shell item: Open an Explorer window, select the item you are interested in, then run the script to see what gets printed out as the Path.

Comments (8)
  1. dbacher says:

    I can think of one time I've seen this used effectively, but I can't remember what app it was.  It was some file management tool, maybe an archiver or a backup program I'm drawing a blank but it was some file management tool.  But it's assumption was that if you had Explorer open on a folder, that you might be wanting to act on that same folder.  So it showed the system libraries first, then pinned folders, then folders that were open in explorer, then finally the normal system tree.

    Again, I don't recall what app it was off the top of my head — just that I saw it and said "oh, that's a pretty good idea."

  2. Steve Clark says:

    I did something similar to insert hyperlinks to files in emails.  Basically you clicked a button in the Outlook email editor, a window popped up listing each file selected in all the open Explorer windows, you clicked the one you wanted and it inserted the hyperlink.  Very useful indeed.  And all done in VBA.

  3. says:

    How would it interact with "spawn folder windows in separate processes" checkbox ?

    [What is the "it" you're referring to? The script works just fine. -Raymond]
  4. Ganesh says:

    Is this JScript or WScript? If I run it with a .vbs extension, it does not work.

    [Presumably you know how to program in VB. Is this a valid VB program? -Raymond]
  5. Jeroen says:

    This script doesn't work very well. new ActiveXObject("Shell.Application").Windows() also returns embedded Internet Explorer windows and they don't have a SelectedItems() property on their Document. It also breaks if you have an Explorer "Computer" window open with a "Class doesn't support Automation" error.

    [Fixing the IE issue is left as an exercise. Remember, Little Programs come with little to no error checking. (And I cannot reproduce the problem with "Computer". I get the selected item just fine.) -Raymond]
  6. Ganesh says:

    Sorry Raymond, I am weak in scripting. I work with VC++ and C#. I assumed since the script uses WScript object, it needs to be run as a file with .vbs extension. And I don't think its a valid VB6 program.

    [WScript supports many scripting languages, not just VB. -Raymond]
  7. Ganesh says:

    Read the post but still could not figure out what extension to use :). Tried wsf but did not help. Seems that the first three lines are js and next few are vbs. Or may be not.

  8. Ben Voigt [Visual C++ MVP] says:

    @Ganesh: Given that every line contains either a semicolon or curly brace, you know that it's a C-syntax language.  The var keyword rules out C++.  Looks like it would be valid Javascript or C# (as a snippet of a large program — C# doesn't allow executable statements at file scope).

Comments are closed.