Jump to content

Get selected filename?


Recommended Posts

I'm quite new to Autoit. What I try to do is to open an explorer window and select the first file. That works good so far, using :


I wanted to add some error handling to my code (see below) by checking/getting the selected file name. However, that seems to be quite challenging.

#include <File.au3>
#include <WinAPIShellEx.au3>

; Variables
$username = EnvGet("username")
$mdir = 'C:\Users\' & $username & '\Desktop\Downloads'

; Open Explorer
Local $aList = _FileListToArray($mdir, '*', 1)
If IsArray($aList) Then
    ; Select first file
    _WinAPI_ShellOpenFolderAndSelectItems($mdir, $aList, 1, 1)
    ; Open file by pressing <ENTER>
    $explorer = WinGetHandle("[TITLE:Downloads]")

Can someone give me a hint how I can verify that the first file is selected in the specific folder before I press the ENTER key?

Link to comment
Share on other sites

10 minutes ago, HansSolo said:

This function returns 0 in case of failure, and 1 in case of success.
You could do something like:

If _WinAPI_ShellOpenFolderAndSelectItems($mdir, $aList, 1, 1) Then ; Success
    ; ...
Else ; Failure
    ; Error Handling


Edited by FrancescoDiMuro

Click here to see my signature:




Link to comment
Share on other sites

You can do something like the following rather than using send keys:

#include <Array.au3>
#include <File.au3>

Local $sDownloads = @DesktopDir & "\Downloads"
    If FileExists($sDownloads) = 0 Then Exit MsgBox(4096, "Folder Error", '"' & $sDownloads & '" could not be found.' & @CRLF & "Exiting Script")

Local $aFileList = _FileListToArray($sDownloads, "*", 1)
If @error Then Exit MsgBox(4096, "File List", "No Files found.")

For $i = 1 To $aFileList[0]
    ShellExecute($sDownloads & "\" & $aFileList[$i])


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

  • Recently Browsing   0 members

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