Jump to content
Macro Express Forums
terrypin

Close then re-open all folders?

Recommended Posts

Closing all Win 10 File Explorer folders at once is straightforward with this macro which simply uses the fixed position of the FE icon in the taskbar:

 

Mouse Move: 164, 1182 Relative to Screen
Delay: 0.1 seconds
Mouse Right Click
Delay: 0.3 seconds
Mouse Move: 0, -38 Relative to Last Position
Delay: 0.1 seconds
Mouse Left Click

Code:

<MOUSE MOVE Option="\x01" X="164" Y="1182" _PROMPT="0x000A"/>
<DELAY Flags="\x01" Time="0.1"/>
<MOUSE RIGHT CLICK/>
<DELAY Flags="\x01" Time="0.3"/>
<MOUSE MOVE Option="\x03" X="0" Y="-38" _PROMPT="0x000A"/>
<DELAY Flags="\x01" Time="0.1"/>
<MOUSE LEFT CLICK/>

But does anyone have any ideas on how to re-open them please? Presumably a matter of recording their full paths before they are closed, which eludes me right now. Half an hour searching the registry with  targets like 'folders' and 'closed' found nothing relevant. Negative result from brief googling.

 

 

 

 

Share this post


Link to post
Share on other sites

I don't have Windows 10, so can't check this.

 

In earlier versions of Windows, it was possible to display the full path in the title bar in Windows Explorer under Tools > Folder Options > View

 

If you can do this in Windows 10, then maybe you could loop through the open Explorer windows and use this:

 

Variable Set String %Win[1]% to topmost window title

 

Alternatively, you might be able to extract the full path from the "Address" line of each instance of the File Explorer, although this would be a less reliable method than the aforementioned example.

 

Share this post


Link to post
Share on other sites

There is still an option to display the full path in the title bar. 

 

F4 will focus the address control and you could do Ctrl+A then clipboard copy. 

 

I'm guessing one could use a Macro Control commands also. But it might give the 'pretty' display text of the address control. 

 

You could also use the Copy Path command and drop the name of the file or folder selected.

 

You could also fire off the Properties dialog and use the "Location" control.

Share this post


Link to post
Share on other sites

Hi Terry,


Hopefully this will get you started. No guarantees, as I did this because I thought it might make for a fun project this evening. No doubt the code can be improved, and perhaps someone else will come up with a more reliable way to test if a window is actually an instance of the File Explorer. (My script checks for ":\" in the title bar, as in "c:\Users" or x:\tmp\hello"

 

 

// This macro checks each window. 
// If its title bar contains :\  we assume the window is an instance of the File Explorer
// We extract the path and store it in a file:
Variable Set String %ResultsFile% to "C:\Users\DELL\Documents\Alan\tmp\All Results.txt"
 
// FOR THIS MACRO TO WORK...
// 1. Set File Explorer options to display the full path in the title bar
 
// LIMITATIONS
// Testing for :\  may not be a foolproof way to determine if the window is an instance of File Explorer
 
Variable Set Integer %Count% to 0
Repeat with Windows: Visible Windows: Store in variable %Title%
  If Variable %Title% Contains ":\"
    Variable Modify Integer %Count%: Increment
    Variable Set String %Result[%Count%]% to "%Title%"
    Text Box Display: Result[%Count%] = %Title%
  End If
End Repeat
 
Switch( %Count% )
Case: 0 // There are zero instances open
  MessageBox: Sorry!
End Case
Default Case // There is at least one instance
  Variable Set Integer %x% to 1
  Repeat Start (Repeat %Count% times)
    Variable Modify String: Append %Result[%x%]% to text file, "%ResultsFile%"
    Variable Modify Integer %x%: Increment
  End Repeat
End Case
End Switch
<COMMENT Value="This macro checks each window. "/>
<COMMENT Value="If its title bar contains :\\  we assume the window is an instance of the File Explorer"/>
<COMMENT Value="We extract the path and store it in a file:"/>
<VARIABLE SET STRING Option="\x00" Destination="%ResultsFile%" Value="C:\\Users\\DELL\\Documents\\Alan\\tmp\\All Results.txt" NoEmbeddedVars="FALSE"/>
<COMMENT/>
<COMMENT Value="FOR THIS MACRO TO WORK..."/>
<COMMENT Value="1. Set File Explorer options to display the full path in the title bar"/>
<COMMENT/>
<COMMENT Value="LIMITATIONS"/>
<COMMENT Value="Testing for :\\  may not be a foolproof way to determine if the window is an instance of File Explorer"/>
<COMMENT/>
<VARIABLE SET INTEGER Option="\x00" Destination="%Count%" Value="0"/>
<REPEAT WITH WINDOWS ToRetrieve="\x01" SortOrder="\x00" Destination="%Title%"/>
<IF VARIABLE Variable="%Title%" Condition="\x06" Value=":\\" IgnoreCase="FALSE"/>
<VARIABLE MODIFY INTEGER Option="\x07" Destination="%Count%"/>
<VARIABLE SET STRING Option="\x00" Destination="%Result[%Count%]%" Value="%Title%" NoEmbeddedVars="FALSE"/>
<TEXT BOX DISPLAY Title="Result[%Count%] = %Title%" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs20 \r\n\\par }\r\n" Left="Center" Top="Center" Width="278" Height="200" Monitor="0" OnTop="TRUE" Keep_Focus="TRUE" Mode="\x00" Delay="0" _ENABLED="FALSE"/>
<END IF/>
<END REPEAT/>
<COMMENT/>
<SWITCH Variable="%Count%"/>
<CASE Value="0" _COMMENT="There are zero instances open"/>
<MESSAGEBOX Caption="Sorry!" Message="There are no instances of File Explorer" Icon="2"/>
<END CASE/>
<DEFAULT CASE _COMMENT="There is at least one instance"/>
<VARIABLE SET INTEGER Option="\x00" Destination="%x%" Value="1"/>
<REPEAT START Start="1" Step="1" Count="%Count%" Save="TRUE" Variable="%x%"/>
<VARIABLE MODIFY STRING Option="\x12" Destination="%Result[%x%]%" Filename="%ResultsFile%" Strip="TRUE" NoEmbeddedVars="FALSE"/>
<VARIABLE MODIFY INTEGER Option="\x07" Destination="%x%"/>
<END REPEAT/>
<END CASE/>
<END SWITCH/>

 

Share this post


Link to post
Share on other sites

Thanks both, much appreciated.

 

Will explore your helpful ideas later, Cory.

 

Meanwhile, Alan, I've just tested the macro you prepared while I slept! Looked solid to me. I duly set titles to show full path - that will need automating later if I go ahead with this little project - and created an empty file. My only edit was to line 5:

Variable Set String %ResultsFile% to "C:\Users\terry\Dropbox\Misc\All Results.txt"

But running it (from F9) gave a very interesting result here, which was consistent on repeating. It promptly closed ME Pro! Well, not quite; Script Editor and Explorer and the tray icon vanished but (on being unable to promptly restart) I found in Task Mgr that MacExp.exe was still loaded.

 

One other exception I would be able to filter out easily: my text editor, TextPad, displays tiltles containing its 'child' windows, e.g:
Latest - TextPad - [C:\Users\terry\Dropbox\Electronics\Arduino\WORKFLOW.txt]
As you warn, there may be others.

 

I'm going out soon but will pick it up again late today.

 

--------------------

EDIT: Tuesday 14 January 2020, 0936

Alan: I should have postponed my testing until after breakfast, or at least my first coffee; your macro now works a treat! 🙂

 

When defining each of your variables after pasting, I'd failed to set %Result% as an array.

 

I've added an IF to skip that TextPad folder and I'd also probably clear the text before each run. That would still leave steps to toggle the full folder title. But after sleeping on it I'm hesitating about proceeding. Background is that an Excel VBA macro I'm developing does not work properly under Win 10 due to some obscure bug in the latter's API (above my know how level), if there are any File Explorer folders open. Hence my thought of toggling them on and off. But I've since thought of a way of accomplishing that part of the VBA macro's objective by using an MEP macro instead.

 

I've been trying hard to rise above my 'copy-paste-edit' mode of working with VBA. But it's a struggle for someone who has never learnt an object-orientated programming language. MEP macro writing is so much more intuitive ... although I do love the speed of VBA.

 

Neat work Alan, thank you!

 

EDIT: 14:00

Back home earlier than expected and went ahead after all. Even if not used for the originally intended purpose this could be useful for other work occasionally. So I now have macros to toggle the folder full title on,  close all folders except TextPad, create All results.txt, restore folders, and toggle folder full title off.

 

Thanks again for giving me a kick start.

 

Share this post


Link to post
Share on other sites

Here is a way to simplify the script: only one repeat loop instead of two, and no arrays. This version ends by opening the file containing the list.

 

// This macro checks each window. 
// If its title bar contains :\  we assume the window is an instance of the File Explorer
// We extract the path and store it in a file:
Variable Set String %ResultsFile% to "C:\Users\DELL\Documents\Alan\tmp\All Results.txt"
 
// FOR THIS MACRO TO WORK...
// 1. Set File Explorer options to display the full path in the title bar
 
// LIMITATIONS
// Testing for :\  may not be a foolproof way to determine if the window is an instance of File Explorer
 
Variable Set Integer %Count% to 0 // Keep track of the number of matches
 
Repeat with Windows: Visible Windows: Store in variable %Title%
  If Variable %Title% Contains ":\"
    Variable Modify Integer %Count%: Increment
    Variable Modify String: Append %Title% to text file, "%ResultsFile%"
  End If
End Repeat
 
If Variable %Count% Equals "0"
  MessageBox: Sorry!
Else
  Program Launch: "notepad.exe" (Maximized)
Parameters: %ResultsFile%
End If
<COMMENT Value="This macro checks each window. "/>
<COMMENT Value="If its title bar contains :\\  we assume the window is an instance of the File Explorer"/>
<COMMENT Value="We extract the path and store it in a file:"/>
<VARIABLE SET STRING Option="\x00" Destination="%ResultsFile%" Value="C:\\Users\\DELL\\Documents\\Alan\\tmp\\All Results.txt" NoEmbeddedVars="FALSE"/>
<COMMENT/>
<COMMENT Value="FOR THIS MACRO TO WORK..."/>
<COMMENT Value="1. Set File Explorer options to display the full path in the title bar"/>
<COMMENT/>
<COMMENT Value="LIMITATIONS"/>
<COMMENT Value="Testing for :\\  may not be a foolproof way to determine if the window is an instance of File Explorer"/>
<COMMENT/>
<VARIABLE SET INTEGER Option="\x00" Destination="%Count%" Value="0" _COMMENT="Keep track of the number of matches"/>
<COMMENT/>
<REPEAT WITH WINDOWS ToRetrieve="\x01" SortOrder="\x00" Destination="%Title%"/>
<IF VARIABLE Variable="%Title%" Condition="\x06" Value=":\\" IgnoreCase="FALSE"/>
<VARIABLE MODIFY INTEGER Option="\x07" Destination="%Count%"/>
<VARIABLE MODIFY STRING Option="\x12" Destination="%Title%" Filename="%ResultsFile%" Strip="TRUE" NoEmbeddedVars="FALSE"/>
<END IF/>
<END REPEAT/>
<COMMENT/>
<IF VARIABLE Variable="%Count%" Condition="\x00" Value="0" IgnoreCase="FALSE"/>
<MESSAGEBOX Caption="Sorry!" Message="There are no instances of File Explorer" Icon="2"/>
<ELSE/>
<PROGRAM LAUNCH Path="C:\\Windows\\notepad.exe" Mode="\x02" Parameters="%ResultsFile%" Default_Path="TRUE" Wait="1" Get_Console="FALSE"/>
<END IF/>

 

Share this post


Link to post
Share on other sites

Thanks Alan, that is a tidier one, although I'm happy enough with  your original. After making the various mods I described I've used it several times since my last post, in conjunction with the other three macros I added.

 

// Alan's macro plus my edits
// This macro checks each window. 
// If its title bar contains :\  we assume the window is an instance of the File Explorer
// We extract the path and store it in a file:
Variable Set String %ResultsFile% to "C:\Users\terry\Dropbox\Misc\All Results.txt"
// Clear previous list of folders from text file
Program Launch: "All Results.txt" (Normal)
Parameters: 
Wait for Window Title: C:\Users\terry\Dropbox\Misc\All Results.txt]
Text Type (Simulate Keystrokes): <CONTROL><HOME> // Go to start
Delay: 0.1 seconds
Text Type (Simulate Keystrokes): <CONTROL><SHIFT><END> // Select all text
Delay: 0.1 seconds
Text Type (Simulate Keystrokes): <DELETE> // Delete all text
Delay: 0.1 seconds
Text Type (Simulate Keystrokes): <CONTROL>s // Save the file
 
// FOR THIS MACRO TO WORK...
// 1. Set File Explorer options to display the full path in the title bar
 
// LIMITATIONS
// Testing for :\  may not be a foolproof way to determine if the window is an instance of File Explorer
 
Variable Set Integer %Count% to 0
Repeat with Windows: Visible Windows: Store in variable %Title%
  If Variable %Title% Contains "Latest -"
    Continue
  End If
  If Variable %Title% Contains ":\"
    Variable Modify Integer %Count%: Increment
    Variable Set String %Result[%Count%]% to "%Title%"
  End If
End Repeat
 
Switch( %Count% )
Case: 0 // There are zero instances open
  MessageBox: Sorry!
End Case
Default Case // There is at least one instance
  Variable Set Integer %x% to 1
  Repeat Start (Repeat %Count% times)
    Variable Modify String: Append %Result[%x%]% to text file, "%ResultsFile%"
    Variable Modify Integer %x%: Increment
  End Repeat
End Case
// Save 'All Results.txt'
End Switch
Text Type (Simulate Keystrokes): <CONTROL>s // Save finished file

 

Share this post


Link to post
Share on other sites

Hi Terry, it's looking good!

 

Here is another thought: Instead of opening All Results.txt, selecting all, deleting, and resaving the file, maybe store an empty file in the same folder called, for example, "All Results Backup.txt".

 

Start by splitting up the path and file name so they are separate variables:

 

Variable Set String %ResultsPath% to "C:\Users\tmp\"

Variable Set String %ResultsFile% to "All Results.txt"

 

Then you could do this:

 

Delete File/Files: "%ResultsPath%%ResultsName%"
Rename File/Files: "%ResultsPath%All Results Backup.txt" to "%ResultsPath%%ResultsName%"


 

Share this post


Link to post
Share on other sites

Thanks Alan. Yes, that’s nice. And it might have reduced the problems I had with my text editor, TextPad, at one stage of development, warning me that “Another program has  modified your file; do want to reload?” I was admittedly in a hurry - this was a digression from the VBA macro that’s preoccupying me - so my edit was a bit rough. I’ve learnt something from your approach, thanks for investing the time.

Share this post


Link to post
Share on other sites

Interesting how progress on these projects is iterative. If I were doing it again, the code could be easier to read (and a bit less opaque) by doing this instead:

 

Variable Set String %ResultsFile% to "C:\Users\tmp\All Results.txt"

Variable Set String %ResultsBackup% to "C:\Users\tmp\All Results Backup.txt"

 

And then...

 

Delete File/Files: "%ResultsFile%"
Rename File/Files: "%ResultsBackup%" to "%ResultsFile%"

Share this post


Link to post
Share on other sites

Hi Terry,

 

Another thought. Since we've figured out a way to get Macro Express to keep track of windows that need to be re-opened, perhaps the same data could be used to close the windows before reopening them. By using an array to store the titles of File Explorer windows, it's not necessary to store them externally in a file.

 

// This macro closes every instance of File Explorer, and then re-opens them all
// If a window's title bar contains :\  we assume it is an instance of the File Explorer
 
// FOR THIS MACRO TO WORK...
// Set File Explorer options to display the full path in the title bar
 
// LIMITATIONS
// Testing for :\  may not be a foolproof way to determine if the window is an instance of File Explorer
 
// 1. Gather titles for every instance of File Explorer
Variable Set Integer %Count% to 0
Repeat with Windows: Visible Windows: Store in variable %TitleBar%
  If Variable %TitleBar% Contains ":\"
    Variable Modify Integer %Count%: Increment
    Variable Set String %Result[%Count%]% to "%TitleBar%"
  End If
End Repeat
 
// If there are no instances of File Explorer, stop
If Variable %Count% Equals "0"
  MessageBox: Sorry!
  Macro Stop
End If
 
// 2. Close every instance...
Variable Set Integer %x% to 1
Repeat Start (Repeat %Count% times)
  Window Close: %Result[%x%]%
  Variable Modify Integer %x%: Increment
End Repeat
 
// 3. Re-open every instance...
Variable Set Integer %x% to 1
Repeat Start (Repeat %Count% times)
  Open Folder to "%Result[%x%]%"
  Variable Modify Integer %x%: Increment
End Repeat
<COMMENT Value="This macro closes every instance of File Explorer, and then re-opens them all"/>
<COMMENT Value="If a window's title bar contains :\\  we assume it is an instance of the File Explorer"/>
<COMMENT/>
<COMMENT Value="FOR THIS MACRO TO WORK..."/>
<COMMENT Value="Set File Explorer options to display the full path in the title bar"/>
<COMMENT/>
<COMMENT Value="LIMITATIONS"/>
<COMMENT Value="Testing for :\\  may not be a foolproof way to determine if the window is an instance of File Explorer"/>
<COMMENT/>
<COMMENT Value="1. Gather titles for every instance of File Explorer"/>
<VARIABLE SET INTEGER Option="\x00" Destination="%Count%" Value="0"/>
<REPEAT WITH WINDOWS ToRetrieve="\x01" SortOrder="\x00" Destination="%TitleBar%"/>
<IF VARIABLE Variable="%TitleBar%" Condition="\x06" Value=":\\" IgnoreCase="FALSE"/>
<VARIABLE MODIFY INTEGER Option="\x07" Destination="%Count%"/>
<VARIABLE SET STRING Option="\x00" Destination="%Result[%Count%]%" Value="%TitleBar%" NoEmbeddedVars="FALSE"/>
<END IF/>
<END REPEAT/>
<COMMENT/>
<COMMENT Value="If there are no instances of File Explorer, stop"/>
<IF VARIABLE Variable="%Count%" Condition="\x00" Value="0" IgnoreCase="FALSE"/>
<MESSAGEBOX Caption="Sorry!" Message="There are no instances of File Explorer" Icon="2"/>
<MACRO STOP/>
<END IF/>
<COMMENT/>
<COMMENT Value="2. Close every instance..."/>
<VARIABLE SET INTEGER Option="\x00" Destination="%x%" Value="1"/>
<REPEAT START Start="1" Step="1" Count="%Count%" Save="TRUE" Variable="%x%"/>
<WINDOW CLOSE Option="\x01" Title="%Result[%x%]%" Partial="FALSE" Wildcards="FALSE" _IGNORE="0x0006"/>
<VARIABLE MODIFY INTEGER Option="\x07" Destination="%x%"/>
<END REPEAT/>
<COMMENT/>
<COMMENT Value="3. Re-open every instance..."/>
<VARIABLE SET INTEGER Option="\x00" Destination="%x%" Value="1"/>
<REPEAT START Start="1" Step="1" Count="%Count%" Save="TRUE" Variable="%x%"/>
<OPEN FOLDER Path="%Result[%x%]%"/>
<VARIABLE MODIFY INTEGER Option="\x07" Destination="%x%"/>
<END REPEAT/>

 

This version gathers the titles of File Explorer windows, one after another. Then the script closes each window in the order they were discovered. Then the script reopens them sequentially in the same order they were found.

 

It might be possible to simplify the process by closing each window, and immediately re-opening it. This "innovation" might introduce timing issues that don't seem to exist now, but it would eliminate the need for the last repeat loop. (I haven't tried it.)

 

// 2. Close AND reopen every instance...

Variable Set Integer %x% to 1

Repeat Start (Repeat %Count% times)

  Window Close: %Result[%x%]%

  Open Folder to "%Result[%x%]%"

  Variable Modify Integer %x%: Increment

End Repeat

// Done!

 

Share this post


Link to post
Share on other sites

Hi Alan,

 

Smart move! I’ll try that when (or possibly if) I get this Excel VBA macro finished.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...