Jump to content

Windows Explorer current folder


WideBoyDixon
 Share

Recommended Posts

No, if at all (meaning the other file manager providing an object interface) this had to be build from the scratch. Better take a look at the ControlListView(), _GUICtrlListView_* ,ControlTreeView() and _GUICtrlTreeView_* functions in the help-files. Use the AU3Info to determine the control classes and go on from there.

Link to comment
Share on other sites

  • 3 months later...

Hello, I am new to this forum and am only here because of this threads relativity to a question I have been trying to get answered. How do I return the local address or addresses of the object or objects currently highlighted in Windows Explorer as a string or array? I am trying to get a program that I am writing to be able to get the locations of what is highlighted upon request. I have the MSDN at my disposal if it is needed. Thanks in advance :oops:

Link to comment
Share on other sites

  • Moderators

ZGorlock,

Welcome to the AutoIt forum. :oops:

Using Ascend4nt's code from post #35 you can get the selected items in string or array form like this:

#include <Array.au3>

Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ComErrFunc")

$aWinList=WinList("[REGEXPCLASS:^(Explore|Cabinet)WClass$]")

For $i = 1 To $aWinList[0][0]
    ; Get base folder and selections
    $aSelection = _ExplorerWinGetSelectedItems($aWinList[$i][1])
    ; Display as strings
    ConsoleWrite("Explorer Window: " & $aSelection[1] & " Selection(s)" & @CRLF)
    For $j = 2 To $aSelection[0] + 1
        ConsoleWrite(@TAB & $aSelection[$j] & @CRLF)
    Next
    ; Display as array
    _ArrayDisplay($aSelection, "Explorer Instance #" & $i & " / " & $aWinList[$i][0])
Next

; ==========================================================================================================================

; Func _ObjectSHFolderViewFromWin($hWnd)
;
; Returns an 'ShellFolderView' Object for the given Window handle
;
; Author: Ascend4nt, based on code by KaFu, klaus.s
; ==========================================================================================================================

Func _ObjectSHFolderViewFromWin($hWnd)
    If Not IsHWnd($hWnd) Then Return SetError(1,0,0)
    Local $oShell,$oShellWindows,$oIEObject,$oSHFolderView

    ; Shell Object
    $oShell=ObjCreate("Shell.Application")
    If Not IsObj($oShell) Then Return SetError(2,0,0)

;   Get a 'ShellWindows Collection' object
    $oShellWindows = $oShell.Windows()
    If Not IsObj($oShellWindows) Then Return SetError(3,0,0)

;   Iterate through the collection - each of type 'InternetExplorer' Object

    For $oIEObject In $oShellWindows
        If $oIEObject.HWND = $hWnd Then
            ; InternetExplorer->Document = ShellFolderView object
            $oSHFolderView=$oIEObject.Document
            If IsObj($oSHFolderView) Then Return $oSHFolderView
            Return SetError(4,0,0)
        EndIf
    Next

    Return SetError(-1,0,0)
EndFunc

; ==========================================================================================================================
; Func _ExplorerWinGetSelectedItems($hWnd)
;
;
; Author: klaus.s, KaFu, Ascend4nt (consolidation & cleanup, Path name simplification)
; ==========================================================================================================================

Func _ExplorerWinGetSelectedItems($hWnd)
    If Not IsHWnd($hWnd) Then Return SetError(1,0,'')
    Local $oSHFolderView
    Local $iSelectedItems,$iCounter=2,$aSelectedItems[2] = [0, ""]

    $oSHFolderView=_ObjectSHFolderViewFromWin($hWnd)
    If @error Then Return SetError(@error,0,'')

;   SelectedItems = FolderItems Collection object->Count
    $iSelectedItems = $oSHFolderView.SelectedItems.Count

    Dim $aSelectedItems[$iSelectedItems+2]  ; 2 extra -> 1 for count [0], 1 for Folder Path [1]

    $aSelectedItems[0]=$iSelectedItems
;   ShellFolderView->Folder->Self as 'FolderItem'->Path
    $aSelectedItems[1]=$oSHFolderView.Folder.Self.Path

;   ShellFolderView->SelectedItems = FolderItems Collection object
    $oSelectedFolderItems = $oSHFolderView.SelectedItems

#cs
    ; For ALL items in an Explorer window (not just the selected ones):
    $oSelectedFolderItems = $oSHFolderView.Folder.Items
    ReDim $aSelectedItems[$oSelectedFolderItems.Count+2]
#ce

    For $oFolderItem In $oSelectedFolderItems
        $aSelectedItems[$iCounter] = $oFolderItem.Path
        $iCounter += 1
    Next

    Return SetExtended($iCounter-2,$aSelectedItems)
EndFunc   ;==>_ExplorerWinGetSelectedItems

Func _ComErrFunc($oError)
    ConsoleWrite("COM Error occurred:"  & @CRLF & _
        "Number: " & @TAB & $oError.number & @CRLF & _
        "Windescription:" & @TAB & $oError.windescription & @CRLF & _
        "Description is: " & @TAB & $oError.description & @CRLF & _
        "Source is: " & @TAB & $oError.source & @CRLF & _
        "Helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
        "Helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
        "Lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
        "Scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
        "Retcode is: " & @TAB & $oError.retcode & @CRLF & @CRLF)
EndFunc   ;==>_ComErrFunc

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

ZGorlock,

Welcome to the AutoIt forum. :oops:

Using Ascend4nt's code from post #35 you can get the selected items in string or array form like this:

#include <Array.au3>

Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ComErrFunc")

$aWinList=WinList("[REGEXPCLASS:^(Explore|Cabinet)WClass$]")

For $i = 1 To $aWinList[0][0]
    ; Get base folder and selections
    $aSelection = _ExplorerWinGetSelectedItems($aWinList[$i][1])
    ; Display as strings
    ConsoleWrite("Explorer Window: " & $aSelection[1] & " Selection(s)" & @CRLF)
    For $j = 2 To $aSelection[0] + 1
        ConsoleWrite(@TAB & $aSelection[$j] & @CRLF)
    Next
    ; Display as array
    _ArrayDisplay($aSelection, "Explorer Instance #" & $i & " / " & $aWinList[$i][0])
Next

; ==========================================================================================================================

; Func _ObjectSHFolderViewFromWin($hWnd)
;
; Returns an 'ShellFolderView' Object for the given Window handle
;
; Author: Ascend4nt, based on code by KaFu, klaus.s
; ==========================================================================================================================

Func _ObjectSHFolderViewFromWin($hWnd)
    If Not IsHWnd($hWnd) Then Return SetError(1,0,0)
    Local $oShell,$oShellWindows,$oIEObject,$oSHFolderView

    ; Shell Object
    $oShell=ObjCreate("Shell.Application")
    If Not IsObj($oShell) Then Return SetError(2,0,0)

;   Get a 'ShellWindows Collection' object
    $oShellWindows = $oShell.Windows()
    If Not IsObj($oShellWindows) Then Return SetError(3,0,0)

;   Iterate through the collection - each of type 'InternetExplorer' Object

    For $oIEObject In $oShellWindows
        If $oIEObject.HWND = $hWnd Then
            ; InternetExplorer->Document = ShellFolderView object
            $oSHFolderView=$oIEObject.Document
            If IsObj($oSHFolderView) Then Return $oSHFolderView
            Return SetError(4,0,0)
        EndIf
    Next

    Return SetError(-1,0,0)
EndFunc

; ==========================================================================================================================
; Func _ExplorerWinGetSelectedItems($hWnd)
;
;
; Author: klaus.s, KaFu, Ascend4nt (consolidation & cleanup, Path name simplification)
; ==========================================================================================================================

Func _ExplorerWinGetSelectedItems($hWnd)
    If Not IsHWnd($hWnd) Then Return SetError(1,0,'')
    Local $oSHFolderView
    Local $iSelectedItems,$iCounter=2,$aSelectedItems[2] = [0, ""]

    $oSHFolderView=_ObjectSHFolderViewFromWin($hWnd)
    If @error Then Return SetError(@error,0,'')

;   SelectedItems = FolderItems Collection object->Count
    $iSelectedItems = $oSHFolderView.SelectedItems.Count

    Dim $aSelectedItems[$iSelectedItems+2]  ; 2 extra -> 1 for count [0], 1 for Folder Path [1]

    $aSelectedItems[0]=$iSelectedItems
;   ShellFolderView->Folder->Self as 'FolderItem'->Path
    $aSelectedItems[1]=$oSHFolderView.Folder.Self.Path

;   ShellFolderView->SelectedItems = FolderItems Collection object
    $oSelectedFolderItems = $oSHFolderView.SelectedItems

#cs
    ; For ALL items in an Explorer window (not just the selected ones):
    $oSelectedFolderItems = $oSHFolderView.Folder.Items
    ReDim $aSelectedItems[$oSelectedFolderItems.Count+2]
#ce

    For $oFolderItem In $oSelectedFolderItems
        $aSelectedItems[$iCounter] = $oFolderItem.Path
        $iCounter += 1
    Next

    Return SetExtended($iCounter-2,$aSelectedItems)
EndFunc   ;==>_ExplorerWinGetSelectedItems

Func _ComErrFunc($oError)
    ConsoleWrite("COM Error occurred:"  & @CRLF & _
        "Number: " & @TAB & $oError.number & @CRLF & _
        "Windescription:" & @TAB & $oError.windescription & @CRLF & _
        "Description is: " & @TAB & $oError.description & @CRLF & _
        "Source is: " & @TAB & $oError.source & @CRLF & _
        "Helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
        "Helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
        "Lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
        "Scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
        "Retcode is: " & @TAB & $oError.retcode & @CRLF & @CRLF)
EndFunc   ;==>_ComErrFunc

M23

Could someone please convert this to C code?
Link to comment
Share on other sites

  • Moderators

ZGorlock,

Could someone please convert this to C code?

- 1. This is the AutoIt forum - if you want C code, I suggest you go to a C forum.

- 2. This now has nothing to do with the original thread - please do not go off-topic.

M23

Edited by Melba23
Typo

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

:oops: Thanx a lot. This was surely a handy function, you saved me 20 lines of code. Edited by MKISH

----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1

Link to comment
Share on other sites

If you are in a position to stipulate the user enable "display full path in title bar" and/or "display full path in addressbar" for XP and later systems then I find this function very fast for polling:

Func Poll4Folder()
    Do
        While 1
            $handle = _WinAPI_GetForegroundWindow()
            $className = _WinAPI_GetClassName($handle)
            If $className = "ExploreWClass" Or $className = "CabinetWClass" Then
                ExitLoop
            Else
                Return
            EndIf
        WEnd
        $title = _WinAPI_GetWindowText($handle)
        $text = WinGetText($title)
        $folders = _StringBetween($text, "Address: ", @LF)
        If Not @error Then
            $folder = $folders[0]
        Else
            $textLines = StringSplit($text, @LF)
            If $textLines[0] > 1 Then
                $folder = $textLines[2]
            Else
                $folder = $textLines[1]
            EndIf
        EndIf
        If $folder = $tmp Then
            $weight += 1
            If $weight = $weightTrigger Then
                _Insert_Folder($folder)
                $weight = 0
                $tmp = ""
                Return
            EndIf
        ElseIf _ValidPath($folder) Then
            $tmp = $folder
            $weight = 1
            Return
        EndIf
        ExitLoop
    Until 0
EndFunc   ;==>Poll4Folder

Note that it uses some global variables and a function to insert the Folder into an array for saving to disk and displaying in a Gui ListBox. It works on XP, Vista and W7 both 32 and 64 bit.

The business with the Weight variable is because I tried to tune it to avoid saving every single folder as the user drills down by double clicking. Whenever the user pauses for a split second the folder should be saved. It's not perfect but I've been using it on some MRU folder caching programs for a few years and I haven't had complaints.

edit: I know there are apparent redundancies like getting the title of the window using the tile of the window. But I determined via trial and error that it works better that way. Could be due to not setting text matching to slow mode. But this code has worked for a long time so it ain't broke and I don't fix it. :oops:

edit2: You may substitute FileExists() for _ValidPath() function. _ValidPath() is just an attempt to avoid calling FileExists() on network drives that aren't mapped any longer etc.. Doing that tends to induce a pause as the FileExist() call times out.

Edited by MilesAhead
Link to comment
Share on other sites

  • 2 years later...

If you have a file "F' selected in folder "folderX",you can retrieve its long name by simulating Windows' RightClick+CopyFileName"

WinActivate("C:...FolderX","Address: C:...FolderX")

Send("{SHIFTDOWN}{F10}{SHIFTUP}{UP}{UP}{UP}{UP}{UP}{UP}{UP}{UP}{UP}{UP}{ENTER}");The number of "{UP}" depends on the position of "Copy file name" in your right click's button

Sleep(110)

$path=ClipGet()

MsgBox(0,"$path",$path)

Link to comment
Share on other sites

  • 8 years later...

Melba23's code is remarkable to someone as dumb as me. But it works like a champ. I looked at a number of Send Key techniques but I really don't like doing things that way when there is another way. Thank you Melba23. Nice piece of code!

Here's what you get when you pass Melba23's _ExplorerWinGetSelectedItems function the handle to a Windows Explorer window. The directory is the first element of the array and any selected items follow.

_ExplorerWinGetSelectedItems.png.723a495644cb1a5831f4fb09ea76b4c1.png

Edited by AGlassman
Add image
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...