Explorer: Detecting failure of Navigate(), Explore() and Open()

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
carrot
Posts: 1
Joined: 17 May 2022, 04:19

Explorer: Detecting failure of Navigate(), Explore() and Open()

Post by carrot » 17 May 2022, 07:11

I'm working on a script to save all open Explorer windows (ShellWindows) and restore them later. There are plenty of examples on the forum of how to do this for the currently open path in each Explorer window. However, I also want to save and restore the navigation history for each window, and this creates problems.

1) Saving the navigation history: I don't see any functions in the MS documentation for directly accessing the back/forward history. In theory, GoBack()/GoForward() could be combined with .Document.Folder.Self.Path to save each path. Unfortunately, those functions don't always indicate when an error occurs, and when an error occurs the path can't be saved. There is error 0x80004005 when trying to go back/forward beyond the start/end of the history, so bounds checking is at least possible. However, when a path that previously existed is encountered (such as for a disconnected USB drive) there is instead a dialog which GoBack()/GoForward() don't wait for and there is no error to catch. After such a dialog appears, It is not possible with these functions to skip over the inaccessible paths to the rest of the history. Using the Back/Forward drop-down button menus may make skipping inaccessible paths possible, but is that really the only option?

2) Restoring the navigation history: If the history is saved either manually or automatically, restoring it may not work if creating each Explorer window and/or subsequent calls to Navigate() result in an error. Again, the problem is the inability to detect when failures occur. Details are below.

2a) Restoring the first history path: A new Explorer window must be opened to restore the first path. Unfortunately, running explorer.exe with the path always returns 0 immediately. If an error occurs, a dialog appears and could be detected, but I'm hoping there's a cleaner solution than this. This i because I would still like to insert the currently inaccessible path into the history for later use. If I call Shell.Explore() or Shell.Open() instead of running explorer.exe with the path, ErrorLevel is always 0 and no dialog appears for both success and failure, so those both fail silently which is terrible. Basically, it looks like I would have to check that each first path still exists before creating a new Explorer window, but that would still mean being forced to skip over temporarily inaccessible paths from the saved history, so that's not really ideal.

2b) Restoring the remaining history paths: Navigate() seems like the only option for adding more paths to the history. This helpfully gives error 0x800704C7 and also shows a dialog when a path is requested that isn't accessible. This would make it quick and reliable to skip over restoration of temporarily inaccessible paths, but again skipping paths is not ideal, so I'm still looking for an alternative. Of course, if all remaining paths are accessible this would work well.

Has anybody else tried to save and restore Explorer's navigation history, or does anybody have thoughts on the problems described?

Any help would be really great. TIA!

Return to “Ask for Help (v1)”