Jump to content

Detect selected files in file explorer / desktop


Recommended Posts

I want to be able to detect the path to the selected files / folders in file explorer, or the Desktop if that is active instead. I have a script that only get the names of the Desktop files selected. This is a problem since that name could be pic, but maybe I have pic.jpg and pic.png, then which is it? And I don't know how to do it in file explorer at all. This is my script:

 

Sleep(3000)
If WinActive("[CLASS:Progman]") Then ; DESKTOP
    $ids = ControlListView("[CLASS:Progman]", "", 1, "GetSelected", 1)
    $id = StringSplit($ids, "|") ; start at 1
    local $name[Ubound($id)]
    For $i = 1 to Ubound($id) - 1
        $name[$i] = ControlListView("[CLASS:Progman]", "", 1, "GetText", $id[$i])
    Next
    $msgbox = ""
    For $i = 1 to Ubound($id) - 1
        $msgbox = $msgbox & $name[$i] & @CRLF
    Next
    MsgBox(0, "", $msgbox)
ElseIf WinActive("[CLASS:CabinetWClass]") Then ; EXPLORER
    $ids = ControlListView("[CLASS:CabinetWClass]", "", 1, "GetSelected", 1)
    MsgBox(0, "doesnt work", $ids)
EndIf

 

Link to post
Share on other sites

LarsJ has a UDF for this as well as an example at the top that looks like it does exactly what you're going for :)

See if this works for you!

 

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Link to post
Share on other sites

When you select files and copy, the full path is put in the clipboard, so maybe you could select, press a key and grab the paths in clipboard.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to post
Share on other sites
4 hours ago, careca said:

When you select files and copy, the full path is put in the clipboard, so maybe you could select, press a key and grab the paths in clipboard.

Thanks, a smart tip. But not in my situation unfortunately.

Link to post
Share on other sites
8 hours ago, seadoggie01 said:

LarsJ has a UDF for this as well as an example at the top that looks like it does exactly what you're going for :)

See if this works for you!

 

 

8 hours ago, KaFu said:

 

 

Great examples both of them! With both examples I could print out the selected files. Thanks!

But none of them work on the Desktop. The closest I get is with this code, but I only get the name without extension ( pic and not pic.jpg )

$ids = ControlListView("[CLASS:Progman]", "", 1, "GetSelected", 1)
    $id = StringSplit($ids, "|")
    For $i = 1 to Ubound($id) - 1
        MsgBox(0, "", ControlListView("[CLASS:Progman]", "", 1, "GetText", $id[$i]))
    Next

 

Edited by spuuunit
Link to post
Share on other sites

Are you talking about when you navigate to the desktop folder via File Explorer, or that you have icons selected on your desktop?

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Link to post
Share on other sites
$clip = ClipGet()
$split = StringSplit($clip, @LF, 1)
For $i = 1 to $split[0]
MsgBox(64 + 262144, $i, $split[$i])
Next

So you select and copy, then run this, or maybe, run in background and trigger with a key.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to post
Share on other sites
8 hours ago, careca said:
$clip = ClipGet()
$split = StringSplit($clip, @LF, 1)
For $i = 1 to $split[0]
MsgBox(64 + 262144, $i, $split[$i])
Next

So you select and copy, then run this, or maybe, run in background and trigger with a key.

Sorry but I really want to avoid copying the files. It's hard to explain, but it's not a good idea in my situation. Thanks anyway!

Is there any other way of doing this (get path of selected files on the desktop)?

Link to post
Share on other sites

Almost never should you ask "is there" in programming, but instead ask, "how?" ;)

Main()

Func Main()

    Local $hDesktop = WinWait("[TITLE:Program Manager;CLASS:Progman]")

    Local $ids = ControlListView($hDesktop, "", "[CLASS:SysListView32; INSTANCE:1]", "GetSelected", 1)
    Local $aItems = StringSplit($ids, "|", 3)
    Local $sFile, $sFullFile

    For $i = 0 to Ubound($aItems) - 1
        $sFile = ControlListView($hDesktop, "", "[CLASS:SysListView32; INSTANCE:1]", "GetText", $aItems[$i])
        ; Get the full name of the file
        $sFullFile = FindFile(@DesktopDir, $sFile)
        
        ; If there were multiple files found
        If @extended Then
            ConsoleWrite("Multiple files found: " & $sFullFile & @CRLF)
        Else
            ConsoleWrite("Selected file: " & $sFullFile & @CRLF)
        EndIf
    Next

EndFunc

Func FindFile($sFolder, $sFile)

    Local $hSearch = FileFindFirstFile($sFolder & "\" & $sFile & "*")
    If @error Then Return SetError(1, 0, False)
    ; Get the first file from the search
    Local $sRet = FileFindNextFile($hSearch)
    Local $sExtras = ""

    While True
        $sExtras &= FileFindNextFile($hSearch)
        If @error Then ExitLoop
    WEnd
    ; If there were extra files
    If $sExtras <> "" Then Return SetExtended(1, $sRet & $sExtras)
    Return $sRet

EndFunc

(I changed a bit of your code because it wasn't running on my computer, then i remembered that I run x64 by default, but AutoIt doesn't. I also used different variable names.)

The important bit is the FindFile function. It takes a folder and file and finds all files that match that name with any ending (.txt, .xlsx, etc).

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Link to post
Share on other sites
26 minutes ago, seadoggie01 said:

Almost never should you ask "is there" in programming, but instead ask, "how?" ;)

Main()

Func Main()

    Local $hDesktop = WinWait("[TITLE:Program Manager;CLASS:Progman]")

    Local $ids = ControlListView($hDesktop, "", "[CLASS:SysListView32; INSTANCE:1]", "GetSelected", 1)
    Local $aItems = StringSplit($ids, "|", 3)
    Local $sFile, $sFullFile

    For $i = 0 to Ubound($aItems) - 1
        $sFile = ControlListView($hDesktop, "", "[CLASS:SysListView32; INSTANCE:1]", "GetText", $aItems[$i])
        ; Get the full name of the file
        $sFullFile = FindFile(@DesktopDir, $sFile)
        
        ; If there were multiple files found
        If @extended Then
            ConsoleWrite("Multiple files found: " & $sFullFile & @CRLF)
        Else
            ConsoleWrite("Selected file: " & $sFullFile & @CRLF)
        EndIf
    Next

EndFunc

Func FindFile($sFolder, $sFile)

    Local $hSearch = FileFindFirstFile($sFolder & "\" & $sFile & "*")
    If @error Then Return SetError(1, 0, False)
    ; Get the first file from the search
    Local $sRet = FileFindNextFile($hSearch)
    Local $sExtras = ""

    While True
        $sExtras &= FileFindNextFile($hSearch)
        If @error Then ExitLoop
    WEnd
    ; If there were extra files
    If $sExtras <> "" Then Return SetExtended(1, $sRet & $sExtras)
    Return $sRet

EndFunc

(I changed a bit of your code because it wasn't running on my computer, then i remembered that I run x64 by default, but AutoIt doesn't. I also used different variable names.)

The important bit is the FindFile function. It takes a folder and file and finds all files that match that name with any ending (.txt, .xlsx, etc).

Thanks! But it would be great if it could know which one it is if there is multiples (for example I select pic.jpg, but there is also pic.gif). How do I do that? ;)

Link to post
Share on other sites

Lol :D I personally check the option in File Explorer for View -> Show/hide -> File name extensions. With that checked, your original selection will return pic.jpg or pic.gif. Other than that, I don't know. I'm sure there's a way to do this, and you might need to ping LarsJ to get him on this :)

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Link to post
Share on other sites

Interesting! But I don't like to have that on, so no good for me I'm afraid. Good idea, I'll try asking LarsJ. Thanks for all your help! If I manage to get it working I'll post the code here, for anyone else who's interested. :)

Link to post
Share on other sites
1 hour ago, LarsJ said:

You, are a legend my friend. Thank you so much for this! I'll post your example for my issue here for anyone to see ( of course you'll need LarsJ's included FileExplorer.au3 too ) :
 

#include "..\..\Includes\FileExplorer.au3"
#include <Array.au3>

Example()

Func Example()
    ; Desktop on Windows 7, 8, 10
    ;Local $hExplorer = WinGetHandle( "[CLASS:Progman]" )
    Local $hExplorer = _WinAPI_GetDesktopWindow()
    If Not $hExplorer Then
        MsgBox( 0, "Automating Windows Explorer", "Could not find Windows Explorer. Terminating." )
        Return
    EndIf

    ; Get an IShellBrowser interface
    GetIShellBrowser( $hExplorer )
    If Not IsObj( $oIShellBrowser ) Then
        MsgBox( 0, "Automating Windows Explorer", "Could not get an IShellBrowser interface. Terminating." )
        Return
    EndIf

    Local $objShell = ObjCreate( "shell.application" )
    $objShell.ToggleDesktop

    ; Get other interfaces
    GetShellInterfaces()

    ; Get all files with full path
    ;GetFiles( $fSelected = False, $fFullPath = False, $fPidl = False, $iMax = 0 )
    Local $aFiles = GetFiles( False, True )
    _ArrayDisplay( $aFiles, "All files" )

    ; Get selected files with full path
    ;GetFiles( $fSelected = False, $fFullPath = False, $fPidl = False, $iMax = 0 )
    $aFiles = GetFiles( True, True )
    _ArrayDisplay( $aFiles, "Selected files" )
EndFunc

Again, thanks a lot. Your Windows handling stuff is amazing!

Link to post
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...