Jump to content

Changing Folder Path in Windows Explorer - ControlClick()


etidd
 Share

Go to solution Solved by TheXman,

Recommended Posts

Hi all, 👋

I want to navigate to another folder in Windows Explorer, but it seems there is no predefined function to do this.

Here is a link to another thread on this topic.

Unfortunately, the code snippet below does not achieve this:

If WinActive("User Data") Then
    ControlClick("User Data", "", 1001, "left", 1)
    Send("C:\Users\Tyler\AppData\Local\Google\Chrome\User Data\Default" & "{ENTER}")
EndIf

Of course, 1001 is not a valid Control ID.

I wish I could use the AutoIt Window Info Tool to check the different controls in the window, but it crashes every time I run it.

Is there another way to do this in the same explorer window, or am I going to have to resort to opening a new window?

 

Thanks in advance.

Edited by etidd
Link to comment
Share on other sites

You may be running into a timing issue. 

1001 is valid. 

image.png.7b0377274bd133b7950fb76ac31ac2aa.png

I would also suggest using ControlSend and looking into that instead. Try This:

$Path = 'C:\Program Files'
ShellExecute('explorer.exe', $Path)

If WinWaitActive($Path,"",10) Then

    ControlClick($Path, "", 1001, "left", 1)

    ControlSend($Path, "", 1001, 'C:\Windows' & "{ENTER}")

Else
    Msgbox(0,"Error",$Path & " Is Not Active")
EndIf
Edited by SkysLastChance

You miss 100% of the shots you don't take. -Wayne Gretzky -Michael Scott

Link to comment
Share on other sites

3 hours ago, etidd said:

Is there another way to do this in the same explorer window, or am I going to have to resort to opening a new window?

If you want to reuse the Explorer window, then I would suggest getting its handle.  With its handle, you can activate/reference it without having to keep up with whatever the current title may be.  I would also suggest sending alt+d to access the explorer's address bar instead of ControlClick().  From there, you can send or paste your path as you are doing now.

 

Example:

#AutoIt3Wrapper_AU3Check_Parameters=-w 3 -w 4 -w 5 -w 6 -d

#include <Constants.au3>

Opt('WinTitleMatchMode', -2)
Opt('SendKeyDelay', 0)


example()

Func example()
    Local $hWnd = -1

    ;Open an explorer window
    ShellExecute("explorer", @DesktopDir)
    If @error Then Return MsgBox($MB_ICONERROR + $MB_TOPMOST, "Error", "Unable to open explorer")

    ;Wait for the explorer window to become active and get its handle
    $hWnd = WinWaitActive("desktop", "", 3)
    If Not $hWnd Then Return MsgBox($MB_ICONERROR + $MB_TOPMOST, "Error", "Timeout occurred waiting for window to become active.")

    MsgBox($MB_ICONINFORMATION + $MB_TOPMOST, "Info", "Hit OK to go to the Program Files")

    ;Change explorer path to "C:\Program Files"
    $hWnd = WinActivate($hWnd)
    If Not $hWnd Then Return MsgBox($MB_ICONERROR + $MB_TOPMOST, "Error", "Unable to activate the explorer window.")
    Send("!d")
    Send("C:\Program Files{ENTER}")

    MsgBox($MB_ICONINFORMATION + $MB_TOPMOST, "Info", "Hit OK to go to the C:\Windows\System32")

    ;Change explorer path to "C:\Windows\System32"
    $hWnd = WinActivate($hWnd)
    If Not $hWnd Then Return MsgBox($MB_ICONERROR + $MB_TOPMOST, "Error", "Unable to activate the explorer window.")
    Send("!d")
    Send("C:\Windows\System32{ENTER}")

    MsgBox($MB_ICONINFORMATION + $MB_TOPMOST, "Info", "Script finished.")
EndFunc

 

Edited by TheXman
Link to comment
Share on other sites

 

7 hours ago, etidd said:

is there another way to do this

 

$hWnd = WinActive("User Data")
If  $hWnd Then
    ControlCommand($hWnd, "", "ToolbarWindow323", "SendCommandID", 1280)
    ControlSetText($hWnd, "", "Edit1", @LocalAppDataDir & "\Google\Chrome\User Data\Default")
    ControlSend($hWnd, "", "Edit1", "{ENTER}")
EndIf

 

7 hours ago, etidd said:

AutoIt Window Info Tool ....., but it crashes every time I run it.

take a look, maybe it will help  209083-au3info-not-working-anymore

you can alternatively use Control Viewer , as a reserve

 

I know that I know nothing

Link to comment
Share on other sites

  • Solution

I think @AllenAA's suggestion to use Shell COM objects is one of, if not the best, solution for changing the Explorer's path.  Of course if more than one Explorer window was open, the example that he provided would've changed the path of all of those Explorer windows.  The example below shows how to open a new Explorer window and only manipulate that specific window.  If using COM is the accepted solution for this topic, then credit should go to @AllenAA for suggesting it, not me.  :)  I'm simply providing a more detailed example to help the original poster understand how a single Explorer window can be manipulated - as was requested in his original post.

Example: (Click below to view)

Spoiler
#include <Constants.au3>

Global $goComErr = ObjEvent("AutoIt.Error", com_error_handler)

explorer_com_example()

Func explorer_com_example()
    Local $oShell, $oShellWindows, $oShellWindow

    ;Open new Explorer window and pause a bit for it to open
    $oShell = ObjCreate("shell.application")
    $oShell.Explore("")
    Sleep(500)

    ;Get collection of Explorer window objects
    $oShellWindows = $oShell.Windows
    If $oShellWindows.Count = 0 Then Return MsgBox($MB_ICONWARNING + $MB_TOPMOST, "Warning", "No Explorer windows found.")

    ;Get last explorer window object that was opened (in case there were explorer windows open already)
    $oShellWindow = $oShellWindows.Item($oShellWindows.Count - 1)

    ;Navigate to some folders
    MsgBox($MB_ICONINFORMATION + $MB_TOPMOST, "Info", "Click OK to go to Recycle Bin")
    $oShellWindow.Navigate("shell:::{645ff040-5081-101b-9f08-00aa002f954e}") ;Recycle Bin

    MsgBox($MB_ICONINFORMATION + $MB_TOPMOST, "Info", "Click OK to go to Desktop")
    $oShellWindow.Navigate(@DesktopDir)

    MsgBox($MB_ICONINFORMATION + $MB_TOPMOST, "Info", "Click OK to go to System32")
    $oShellWindow.Navigate("C:\Windows\System32")

    MsgBox($MB_ICONINFORMATION + $MB_TOPMOST, "Info", "Click OK to go to All Tasks (God Mode)")
    $oShellWindow.Navigate("shell:::{ED7BA470-8E54-465E-825C-99712043E01C}") ;All Tasks (God Mode)

    MsgBox($MB_ICONINFORMATION + $MB_TOPMOST, "Info", "Script finished.  Click OK to close Explorer window and exit.")

    ;Close the explorer window
    $oShellWindow.Quit
EndFunc

Func com_error_handler()
    With $goComErr
        ConsoleWrite(@CRLF)
        ConsoleWrite("COM ERROR:" & @CRLF)
        ConsoleWrite("- Error Number........... " & "0x" & Hex(.number) & @CRLF)
        ConsoleWrite("- Error WinDescription... " & StringStripWS(.windescription, $STR_STRIPTRAILING) & @CRLF)
        ConsoleWrite("- Error Description...... " & StringStripWS(.description   , $STR_STRIPTRAILING) & @CRLF)
        ConsoleWrite("- Error ScriptLine....... " & .scriptline & @CRLF)
        ConsoleWrite("- Error RetCode.......... " & "0x" & Hex(.retcode) & @CRLF)
    EndWith
    Exit
EndFunc

 

 

Edited by TheXman
Link to comment
Share on other sites

Thanks to all for the informative replies. :rambo:

I started by sending ALT+D to the explorer windows on XMan's suggestion, but I switched to the use of Shell COM objects per AllenAA's input.

This is what part of the script looks like:

; creation of shell COM object

Local $oShell, $oShellWindows, $oShellWindow
$oShell = ObjCreate("Shell.Application")
$oShellWindows = $oShell.Windows
If $oShellWindows.Count = 0 Then MsgBox($MB_ICONWARNING + $MB_TOPMOST, "Warning", "No Explorer windows found.")

; second round of backup

$oShellWindow = $oShellWindows.Item($oShellWindows.Count - 1) ; selects the window I want
$oShellWindow.Navigate("----folder location redacted----") ; goes to the new destination
Sleep(500)
WinActivate("Default")
Sleep(500)
Send("^a")
Send("^c")
$oShellWindow = $oShellWindows.Item($oShellWindows.Count - 3) ; selects the other window I want
$oShellWindow.Navigate("----other folder location redacted----") ; goes to the new destination
Sleep(500)
WinActivate("Chrome Data")
Sleep(500)
Send("^v")
Sleep(3000)

 

On 9/15/2023 at 5:31 PM, SkysLastChance said:

You may be running into a timing issue. 

I'm surprised by how important timing is in AutoIt. After employing the Shell COM objects and making sure the Sleep() commands were consistent, the timing issues were resolved, but timing is likely to be the topic of a future thread of mine or a topic I need to read further on as I become more proficient in this language.

Now, I have a working script! :drool:

(although, I want to add a thing or two more in there to make it even better and more reflexive if it is run under different conditions)

 

On 9/15/2023 at 10:58 PM, ioa747 said:

take a look, maybe it will help  209083-au3info-not-working-anymore

you can alternatively use Control Viewer , as a reserve

I downloaded UIASpy.au3 by tankew, but I have just downloaded Control Viewer on your recommendation so I have that as well. I have begun to use UIASpy to pinpoint control ID's and learn more about targets I can manipulate in a future script.

----

I'm really impressed by all that was put together by the AutoIt developers. This is a really cool language. :ILA2:

Edited by etidd
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...