读取所选文件的文件名

时间:2014-07-18 21:00:05

标签: autoit

我试图找到一种方法来读取Windows资源管理器中所选文件的文件名。我尝试了这个,但它不起作用

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)

我知道我们可以使用Run("explorer.exe /e,/select," & filename)来完成它,但我想要另一种方法。

2 个答案:

答案 0 :(得分:2)

您的上述脚本访问DirectUIHWND。 ControlListView将命令发送到ListView32控件。 DirectUIHWND<> ListView32并不容易访问它。

这是一个"脏"访问它的方式:

;#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

如果您尝试访问左侧的树视图,请检查示例中的注释并替换controlid

你可以创建一个小gui,列出你想要的文件夹的所有文件,然后制作然后从gui中选择,甚至不必与explorer.exe交互 这是一个例子:

#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

答案 1 :(得分:1)

如所承诺的那样;)

这是一个更简单的例子,无需下载任何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

在第一个for循环中,您将拥有所有名称。在第二个中,我们只选择每个项目。 如果您需要更多帮助,请告诉我们