rdp
rdp

Reputation: 2088

Read the filename of the selected file

I am trying to figure out a way to read the file name of a selected file in windows explorer. I tried this but it did not work

run ("C:\WINDOWS\explorer.exe C:\ProgramData\")
WinWaitActive("ProgramData")
Sleep(2000)

;select the 4th file
Send("{down}{down}{down}")

$index=ControlListView ( "ProgramData", "", "SysListView321", "GetSelected")
ConsoleWrite("$index: " & $index & @CRLF)
$text = ControlListView ( "ProgramData", "", "SysListView321", "GetText",$index)
ConsoleWrite("$text: " & $text & @CRLF)

I know that we can do it with Run("explorer.exe /e,/select," & filename) but I want an alternate way to this.

Upvotes: 0

Views: 2122

Answers (2)

user2530266
user2530266

Reputation: 287

As promised ;)

Here is an simpler example without having to download any udf:

HotKeySet("{ESC}", "_Exit")

Do
    Sleep(10)
Until WinExists("[REGEXPCLASS:^(Cabinet|Explore)WClass$]")

Local $hWin = WinGetHandle( "[REGEXPCLASS:^(Cabinet|Explore)WClass$]" )
If Not $hWin Then Exit

Local $hItem = "", $hSelection, $sText
WinActivate($hWin)

; Shell object
$oShell = ObjCreate("Shell.Application")

; Find window
For $oWindow In $oShell.Windows()
    If $oWindow.HWND() = $hWin Then ExitLoop
Next

; Selected items
For $oItem In $oWindow.Document.SelectedItems()
    $hItem = $oItem.Name()
Next

While WinExists("[REGEXPCLASS:^(Cabinet|Explore)WClass$]")
    For $oItem In $oWindow.Document.SelectedItems()
        If $oItem.Name() <> $hItem Then
            ConsoleWrite("Name: " & $oItem.Name & @CRLF)    ;Sets or gets the item's name.
            ConsoleWrite("Type: " & $oItem.Type & @CRLF)    ;Contains a string representation of the item's type.
            ConsoleWrite("Path: " & $oItem.Path  & @CRLF)   ;Contains the item's full path and name.
            ConsoleWrite("IsLink: " & $oItem.IsLink  & @CRLF)   ;Indicates whether the item is a shortcut.
            ConsoleWrite("IsFolder: " & $oItem.IsFolder & @CRLF)    ;Indicates whether the item is a folder.
            ConsoleWrite("IsFileSystem: " & $oItem.IsFileSystem & @CRLF)    ;Indicates if the item is part of the file system.
            ConsoleWrite("IsBrowsable : " & $oItem.IsBrowsable & @CRLF) ;Indicates if the item can be hosted inside a browser or Windows Explorer frame.
            ConsoleWrite("GetLink : " & $oItem.GetLink  & @CRLF)    ;Contains the item's ShellLinkObject object, if the item is a shortcut.
            ConsoleWrite("GetFolder : " & $oItem.GetFolder & @CRLF) ;Contains the item's Folder object, if the item is a folder.
            ConsoleWrite("Application : " & $oItem.Application  & @CRLF)    ;Contains the Application object of the folder item.
            ConsoleWrite("Date: " & $oItem.ModifyDate  & @CRLF)   ;yy/mm/dd hh/nn/ss
            ConsoleWrite("Size: " & $oItem.Size & " bytes"& @CRLF)  ;Contains the item's size.
            ;ConsoleWrite("Type: " & Round($oItem.Size / 1000) & " KB"& @CRLF)
            $hItem = $oItem.Name()
        EndIf
    Next
    Sleep(100)
WEnd
ConsoleWrite("Exit" & @LF)

Func _Exit()
    Exit
EndFunc   ;==>_Exit

In the first for loop you will have all the names. In the second we simply select every item. If you need more help let me know

Upvotes: 1

user2530266
user2530266

Reputation: 287

Your above script accesses DirectUIHWND. ControlListView sends a command to a ListView32 control. DirectUIHWND <> ListView32 and it can not be accesses that easily.

This is a "dirty" way to access it:

;#include <MsgBoxConstants.au3>

;Run("explorer.exe")
;Sleep(2000)
Local $hWin = WinGetHandle("Libraries")    ;Change to ProgramData for you
Local $hwnd = ControlGetHandle($hWin, "", "DirectUIHWND3")    ;the name of the list on the left is : SysTreeView321. Hence it is a listTREEview
ConsoleWrite("Window handle: " & $hWin & @LF)
ConsoleWrite("Control handle: " & $hwnd & @LF)
WinActivate($hWin)
ControlFocus($hwnd, "", $hwnd)
Send("{down}{down}{down}")    ;;;select a random item
ConsoleWrite(SysTreeViewGetText() & @LF)




Func SysTreeViewGetText()
    ClipPut("")
    ControlSend($hWnd, "", "", "{F2}")
    ControlSend($hWnd, "", "", "^c")
    ControlSend($hWnd, "", "", "{ESC}")
    Return ClipGet()
EndFunc

If you are trying to acces the treeview on the left then check the comments in the example and replace the controlid

You can create a small gui that will list all the files of the folder you want and then make then select from the gui without even having to interact the explorer.exe Here is an example:

#include <File.au3>


Local $aFileList
Local $hGui = GUICreate("ProgramData", 450, 300)
Local $hButton = GUICtrlCreateButton("File list", 180, 50, 100)
Local $hList = GUICtrlCreateList("", 10, 100, 430, 200)
GUISetState(@SW_SHOW)


While True
    $sMsg = GUIGetMsg()
    Switch $sMsg
        Case -3
            Exit
        Case $hButton
            $aFileList = _FileListToArray(@DesktopDir, "*")  ; add your path here
            For $i = 0 To UBound($aFileList) -1
                GUICtrlSetData($hList, $aFileList[$i])
            Next
    EndSwitch
    Sleep(100)
WEnd

Upvotes: 2

Related Questions