April 22nd, 2013

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);
 WScript.StdOut.WriteLine(w.LocationName);
 var sel = w.Document.SelectedItems();
 for (var j = 0; j < sel.Count; j++) {
  var item = sel.Item(j);
  WScript.StdOut.WriteLine(item.Name);
  WScript.StdOut.WriteLine(item.Path);
 }
}

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.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.