Jump to content

How to Get a List of Folders and Files Selected in Windows Explorer


Recommended Posts

Hi,

I'm wondering how to do the following using AutoIt.

Let's say I have an Explorer window open and I select multiple files and folders (so that they are highlighted in the Explorer window). Is there any way to get AutoIt to 'grab' all the full paths of all those highlighted folders and files (and only the highlighted ones because some the folders and files might not be 'selected' at all) in one go? Really so that I end up with a 'list' of the full paths of all the selected items. (I'm assuming that somewhere in Windows there is something that is actually storing the full paths to those folders and files as you select them.)

Edited by Radish

Operating System: Windows 7 Pro. x64 SP1

Link to comment
Share on other sites

  • Moderators

Radish,

This works for me on Win7:

; Read title of Explorer window
$sTitle = ControlGetText("[CLASS:CabinetWClass]", "", "[CLASS:ToolbarWindow32;INSTANCE:2]")
; Extract path from title
$sPath = StringRegExpReplace($sTitle, ".*(\w:\\.*)", "$1")
; Get selected items
$sSelected = ControlListView("[CLASS:CabinetWClass]", "", "[CLASS:SysListView32]", "GetSelected", 1)
; Convert to array
$aSelected = StringSplit($sSelected, "|")
For $i = 1 To $aSelected[0]
    ; Get text of items and prepend path
    $sItem = ControlListView("[CLASS:CabinetWClass]", "", "[CLASS:SysListView32]", "GetText", $aSelected[$i], 0)
    ; Display results
    ConsoleWrite($sPath & "\" & $sItem & @CRLF)
Next

Any use?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks Melba. I've just tried this, compiled it and run the 'exe' but I can't tell if it's working as I can't see any output. Where does the output go?

I had a quick look at ConsoleWrite in the AutoIt help file and it says something about "This does not write to a DOS console unless the script is compiled as a console application." I don't know how to complile in that way - is that something that I need to do?

P.S. I too am running Win7.
 

 

 

Operating System: Windows 7 Pro. x64 SP1

Link to comment
Share on other sites

  • Moderators

ConsoleWrite is only for uncompiled scripts.If you compile to executable, change the ConsoleWrite to a MsgBox for testing, or output to a file with FileWriteLine.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Moderators

Radish,

As JLogan3o13 has explained, ConsoleWrite is used mainly in uncompiled scripts run in SciTE where it displays the output in the lower pane of the editor window. As previously suggested, use a MsgBox or write to a file to see the output if you run the script compiled.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Melba23, Your code does definitely not work on Win 7 SP1 64 bit. On that OS Windows Explorer does not contain a SysListView32 control. Instead of it contains a DUIListView control which is a virtual listview. This can be verified with the UI Automation framework or Inspect.exe from Windows SDK. The control is not recognized by the "AutoIt Window Info" tool.

To automate this DUIListView you can use Shell objects. One of the problems with Shell objects is, that only the objects which  MicroSoft have implemented are available. But you can easily generate a list of selected items.

Fortunately the Shell objects can also be created with ObjCreateInterface (native AutoIt function). When we ourselves create the objects, we can create all the objects we need.

You can find an implementation of Shell objects created with ObjCreateInterface in Automating Windows Explorer. And you can find a collection of small examples here. This code works on all Windows versions.

 

Edited by LarsJ
Link to comment
Share on other sites

@Melba23

i tried your script it works fine

windows 7 professional sp1.

one question,

[CLASS:SysListView32]  ;where did you get this?

[CLASS:SysTreeView32; INSTANCE:1]  ;this is what ive got from autoit window info

 

Edited by 232showtime

ill get to that... i still need to learn and understand a lot of codes graduated.gif

Correct answer, learn to walk before you take on that marathon.

Link to comment
Share on other sites

  • Moderators

232showtime,

You answered your own question: "got from autoit window info"

LarsJ,

Thanks for the additional information.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • 2 weeks later...

Hi,

I tried Melba23's script, though I added a bit to it so that I could see output when the script is compiled. Basically the script doesn't work on my system, Windows 7 x64. It does correctly get the path to the location of the files and folders I've selected, but it doesn't get a list of the selected folders and files.

For example if I select two folders and two files like this in the root directory of D:

- Test Folder 1

- Test Folder 2

- Test File 1

- Test File 2

The only output Melba23's script produces is "D:\0" (The path D:\ is correct but the zero that follows is nothing like what I selected so, I assume, it's just an indication that the script detected that zero folders/files were selected and that it does that because that part of the script isn't working.)

I've read the comments above and see that LarsJ says that the script won't t work on a Win7 x64 system. I looked at LarsJ comments and followed the links he provided and quickly tried out the scripts he provided - but they don't actually deal with my intial query, and to be honest I'm only a newbie and his examples are so complex I don't understand them.

So could I repeat my original query and ask if someone knows of a way to get a list of selected folders and files in Explorer on a Win7 x64 system? (In language that a newbie can understand.)

Operating System: Windows 7 Pro. x64 SP1

Link to comment
Share on other sites

After several hours of tinkering with some parts of LarsJ's files contained in the zip file here I managed to get the output I needed for the script I'm working on. YES!

Definitely don't understand most of what is in LarsJ's scripts though - very complicated.

Thanks to all who responded and big thanks to LarsJ for providing the solution. Happy now! :)

Operating System: Windows 7 Pro. x64 SP1

Link to comment
Share on other sites

I say! Thanks for your solution, Mikell! After a quick test it seems to work fine, though I'd have to sort through the output once it goes to a text file. Or sort it properly before it's written to a text file. But I have a rough idea how to do that so that is within my capabilities.

Many thanks.

(That said I think I'll go with LarsJ's thing given that I worked for hours trying to puzzle out the bits I needed to puzzle out. Still stored your solution though, maybe it will be useful to me in the future. Thanks again.)

Edited by Radish

Operating System: Windows 7 Pro. x64 SP1

Link to comment
Share on other sites

I just tested LarsJ's script by trying to highlight items on the Desktop. It doesn't work in that scenario. Ah! Of course not it's looking for content in an Explorer window.

Then tested Mikell's solution on the Desktop. That does work for there. So I'll go with Mikell's solution for now as I need what I'm doing to also be able to work for items on the Desktop.

Is it possible to get LarsJ's solution to work on the Desktop without it having to be opened in an Explorer window?

Operating System: Windows 7 Pro. x64 SP1

Link to comment
Share on other sites

Radish, Good to see that you got it to work. And good observation about the desktop. You are also right in the cause of the problem. I will certainly try if I can do something about it.

I think the code by Melba23 in the post above will work for the desktop. The desktop seems to be a special implementation of Windows Explorer right pane. As far as I know the desktop is using the SysListView32 control on all Windows versions. Just replace CabinetWClass with Progman and the code by Melba23 should work. Of course there is no toolbar.

It's not surprising that the solution by mikell is working. This code will probably work more or less on everything that can be treated as a selection. And it's probably also the fastest solution. Radish, if this solution works for you, you should stay with this.

KaFu and Melba23, that's exactly the solution with Shell objects I was talking about. I thing this will work on all Windows versions. And also the desktop. Just use Progman for the class name of the desktop.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...