Jump to content
Macro Express Forums

Challenge: a macro to report the size of a folder


Recommended Posts

12 hours ago, rberq said:

Yes, always fun watching a macro follow your instructions in real time.

 

My 3D Printer (recent generous and unexpected birthday present) gives me the same kick. But durations are measured in hours not seconds 🙂

 

Quote

pass it to text-recognition software, then parse and read out the results. 

 

I think that would be relatively easy, as I use OCR in several macros for hard-to-get text. But, apart from the occasional inaccuracy, it seems pointless as it comes after the only long step of getting the size!

 

I also tried several command prompt methods but all took at least as long as the straightforward macros by Alan and me. I tested one promising tool from SysInternals called DU

https://docs.microsoft.com/en-us/sysinternals/downloads/du

on several large folders. It was always no faster.

 

BTW, it was accurate to the byte on all but one of my tests, C:\Users\Terry\Dropbox\.

DU reported 27,971,874,555 bytes from 14995 'directories'. The direct method gave 27,965,987,107 from 14994 'folders'.

 

Link to comment
Share on other sites

Quote

DU reported 27,971,874,555 bytes from 14995 'directories'. The direct method gave 27,965,987,107 from 14994 'folders'.

 

I'm not sure what the difference is between "Size" and "Size on Disk," but the two values do not appear to be exactly the same. Could that account for the discrepancy? 

 

Now that I'm actually using this macro myself, I'm realizing it might be handy if it also reported the number of folders and files inside the folder, as well as the size of all those files and sub-folders

Link to comment
Share on other sites

Quote

I'm not sure what the difference is between "Size" and "Size on Disk," but the two values do not appear to be exactly the same. Could that account for the discrepancy? 

 

No, it's the folder/directory number. (And I compared the 'Size' result in all tests.)

Link to comment
Share on other sites

Here is a refined version: The macro now reports the number of contained files and subfolders as well as the total size of the parent folder. It reformats the text to improve legibility in the message box. For example, the macro displays size in GB, not in bytes. I tweaked the logic, renamed variables to make them (hopefully) easier to understand, etc.

 

This macro was supposed to be for a client, but I think I'll make more use of it than he will!

 

// In File Explorer, for the selected folder, display in a message box: 
 
// 1. The number of files and folders it contains
// 2. The total size of all subfolders and files contained in the folder
 
// Start with a *folder* selected in File Explorer -- selecting a file may or may not work
Text Type (Simulate Keystrokes): <ALT><ENTER> // Open the "Properties" screen for the selected item
Delay: 500 milliseconds // Wait for the "Properties" screen to open
 
Variable Set String %WinTitle% to topmost window title // Get the title bar of the "Properties" screen
 
// The script cannot handle this situation!
If Variable %WinTitle% Contains "OS ("
  MessageBox: Cannot get the size!
  Macro Stop
End If
 
// The macro clicks in the centre of the "Size" field. Later, it clicks in the centre of the "Contains" field.
 
// The horizontal centre of the "Size" field is 45% of the screen width from the left edge.
// The vertical centre varies. Depending on the height of the "Properties" window...
// ...the "Size" field is in one of two locations: 37% or 40% of the distance from the top.
 
// The horizontal centre of the "Contains" field is 30% of the screen width from the left edge.
// The vertical centre varies. Depending on the height of the "Properties" window...
// ...the "Contains" field is in one of two locations: 48% or 50% of the distance from the top.
 
// So set two "fudge factors" to use to calculate y-coordinates of the two fields
 
// The most common fudge factors:
Variable Set Decimal %SizeFudgeFactor% to 0.37
Variable Set Decimal %ContainsFudgeFactor% to 0.48
 
// Less common fudge factors:
If Variable %WinTitle% Equals "Documents Properties"
  OR
If Variable %WinTitle% Equals "Music Properties"
  OR
If Variable %WinTitle% Equals "Videos Properties"
  OR
If Variable %WinTitle% Equals "Pictures Properties"
  Variable Set Decimal %SizeFudgeFactor% to 0.4
  Variable Set Decimal %ContainsFudgeFactor% to 0.5
End If
 
// Get the width and height of the "Properties" screen
Variable Set Integer %Width%: Set to the Current Window's Width
Variable Set Integer %Height%: Set to the Current Window's Height
 
// Convert to decimal so values can be muliplied by fudge factors (which are fractions)
Variable Modify Integer %Width%: Convert to Decimal (%WidthDecimal%)
Variable Modify Integer %Height%: Convert to Decimal (%HeightDecimal%)
 
// Estimate coordinates for the centre of the "Size" field
Variable Modify Decimal: %SizeXDecimal% = %WidthDecimal% * 0.45
Variable Modify Decimal: %SizeYDecimal% = %HeightDecimal% * %SizeFudgeFactor%
 
Variable Modify Decimal %SizeXDecimal%: Truncate to Integer (%SizeXInteger%)
Variable Modify Decimal %SizeYDecimal%: Truncate to Integer (%SizeYInteger%)
 
// Estimate coordinates for the centre of the "Contains" field
Variable Modify Decimal: %ContainsXDecimal% = %HeightDecimal% * 0.3
Variable Modify Decimal: %ContainsYDecimal% = %HeightDecimal% * %ContainsFudgeFactor%
 
Variable Modify Decimal %ContainsXDecimal%: Truncate to Integer (%ContainsXInteger%)
Variable Modify Decimal %ContainsYDecimal%: Truncate to Integer (%ContainsYInteger%)
 
// Check every 0.5 second for 20 seconds to see if the "Size" field has changed
Variable Set String %FieldCapture2% to ""
 
Repeat Start (Repeat 40 times)
  Mouse Move: %SizeXInteger%, %SizeYInteger% Relative to Current Window // Centre of the "Size" field
  Mouse Left Click
  Text Type (Simulate Keystrokes): <HOME><SHIFT><END> // Select the "Size" field
  Text Type (Simulate Keystrokes): <CONTROL>c
  Delay: 50 milliseconds
  Variable Set String %FieldCapture1% from the clipboard contents
  Delay: 50 milliseconds
   
  If Variable %FieldCapture1% Equals "%FieldCapture2%" // Is "Size" field unchanged after two consecutive loops?
    Variable Set Bool %IsSizeFound% to "True" // Then we've captured the size!
    Repeat Exit
  Else
    Variable Set String %FieldCapture2% to "%FieldCapture1%"
    Delay: 500 milliseconds
  End If
   
End Repeat
 
If Variable %IsSizeFound% Equals "True"
// Capture content of "Contains" field
  Mouse Move: %ContainsXInteger%, %ContainsYInteger% Relative to Current Window // Centre of the "Contains" field
  Mouse Left Click
  Text Type (Simulate Keystrokes): <HOME><SHIFT><END> // Select the "Contains" field
  Text Type (Simulate Keystrokes): <CONTROL>c
  Delay: 50 milliseconds
  Variable Set String %Contains% from the clipboard contents
   
  Text Type (Simulate Keystrokes): <ESC> // Close the "Properties" window
   
  // Reformat "Size" to delete everything inside parentheses
  Variable Set String %Size% to "%FieldCapture1%"
  Variable Set Integer %LengthSize% to the length of variable %Size%
  Variable Set Integer %OpenParenPos% to the position of "(" in %Size%
  Variable Modify String: Delete part of text from %Size% starting at %OpenParenPos% and %LengthSize% characters long
   
  // Reformat "Contains" so Files and Folders are on separate lines
  Variable Modify String: Replace "Files, " in %Contains% with "Files
"
   
  // Output result
  MessageBox: Info from "%WinTitle%"
Else
// Report that we couldn't get solutions
  MessageBox: The macro failed!
End If

<COMMENT Value="In File Explorer, for the selected folder, display in a message box: "/>
<COMMENT/>
<COMMENT Value="1. The number of files and folders it contains"/>
<COMMENT Value="2. The total size of all subfolders and files contained in the folder"/>
<COMMENT/>
<COMMENT Value="Start with a *folder* selected in File Explorer -- selecting a file may or may not work"/>
<TEXT TYPE Action="0" Text="<ALT><ENTER>" _COMMENT="Open the \"Properties\" screen for the selected item"/>
<DELAY Flags="\x02" Time="500" _COMMENT="Wait for the \"Properties\" screen to open"/>
<COMMENT/>
<VARIABLE SET STRING Option="\x05" Destination="%WinTitle%" _COMMENT="Get the title bar of the \"Properties\" screen"/>
<COMMENT/>
<COMMENT Value="The script cannot handle this situation!"/>
<IF VARIABLE Variable="%WinTitle%" Condition="\x06" Value="OS (" IgnoreCase="FALSE"/>
<MESSAGEBOX Caption="Cannot get the size!" Message="This script doesn't work in \"%WinTitle%\"!" Icon="4"/>
<MACRO STOP/>
<END IF/>
<COMMENT/>
<COMMENT Value="The macro clicks in the centre of the \"Size\" field. Later, it clicks in the centre of the \"Contains\" field."/>
<COMMENT/>
<COMMENT Value="The horizontal centre of the \"Size\" field is 45% of the screen width from the left edge."/>
<COMMENT Value="The vertical centre varies. Depending on the height of the \"Properties\" window..."/>
<COMMENT Value="...the \"Size\" field is in one of two locations: 37% or 40% of the distance from the top."/>
<COMMENT/>
<COMMENT Value="The horizontal centre of the \"Contains\" field is 30% of the screen width from the left edge."/>
<COMMENT Value="The vertical centre varies. Depending on the height of the \"Properties\" window..."/>
<COMMENT Value="...the \"Contains\" field is in one of two locations: 48% or 50% of the distance from the top."/>
<COMMENT/>
<COMMENT Value="So set two \"fudge factors\" to use to calculate y-coordinates of the two fields"/>
<COMMENT/>
<COMMENT Value="The most common fudge factors:"/>
<VARIABLE SET DECIMAL Option="\x00" Destination="%SizeFudgeFactor%" Value="0.37"/>
<VARIABLE SET DECIMAL Option="\x00" Destination="%ContainsFudgeFactor%" Value="0.48"/>
<COMMENT/>
<COMMENT Value="Less common fudge factors:"/>
<IF VARIABLE Variable="%WinTitle%" Condition="\x00" Value="Documents Properties" IgnoreCase="FALSE"/>
<OR/>
<IF VARIABLE Variable="%WinTitle%" Condition="\x00" Value="Music Properties" IgnoreCase="FALSE"/>
<OR/>
<IF VARIABLE Variable="%WinTitle%" Condition="\x00" Value="Videos Properties" IgnoreCase="FALSE"/>
<OR/>
<IF VARIABLE Variable="%WinTitle%" Condition="\x00" Value="Pictures Properties" IgnoreCase="FALSE"/>
<VARIABLE SET DECIMAL Option="\x00" Destination="%SizeFudgeFactor%" Value="0.4"/>
<VARIABLE SET DECIMAL Option="\x00" Destination="%ContainsFudgeFactor%" Value="0.5"/>
<END IF/>
<COMMENT/>
<COMMENT Value="Get the width and height of the \"Properties\" screen"/>
<VARIABLE SET INTEGER Option="\x0A" Destination="%Width%"/>
<VARIABLE SET INTEGER Option="\x0B" Destination="%Height%"/>
<COMMENT/>
<COMMENT Value="Convert to decimal so values can be muliplied by fudge factors (which are fractions)"/>
<VARIABLE MODIFY INTEGER Option="\x05" Destination="%Width%" Variable="%WidthDecimal%"/>
<VARIABLE MODIFY INTEGER Option="\x05" Destination="%Height%" Variable="%HeightDecimal%"/>
<COMMENT/>
<COMMENT Value="Estimate coordinates for the centre of the \"Size\" field"/>
<VARIABLE MODIFY DECIMAL Option="\x02" Destination="%SizeXDecimal%" Value1="%WidthDecimal%" Value2="0.45"/>
<VARIABLE MODIFY DECIMAL Option="\x02" Destination="%SizeYDecimal%" Value1="%HeightDecimal%" Value2="%SizeFudgeFactor%"/>
<COMMENT/>
<VARIABLE MODIFY DECIMAL Option="\x06" Destination="%SizeXDecimal%" Variable="%SizeXInteger%"/>
<VARIABLE MODIFY DECIMAL Option="\x06" Destination="%SizeYDecimal%" Variable="%SizeYInteger%"/>
<COMMENT/>
<COMMENT Value="Estimate coordinates for the centre of the \"Contains\" field"/>
<VARIABLE MODIFY DECIMAL Option="\x02" Destination="%ContainsXDecimal%" Value1="%HeightDecimal%" Value2="0.3"/>
<VARIABLE MODIFY DECIMAL Option="\x02" Destination="%ContainsYDecimal%" Value1="%HeightDecimal%" Value2="%ContainsFudgeFactor%"/>
<COMMENT/>
<VARIABLE MODIFY DECIMAL Option="\x06" Destination="%ContainsXDecimal%" Variable="%ContainsXInteger%"/>
<VARIABLE MODIFY DECIMAL Option="\x06" Destination="%ContainsYDecimal%" Variable="%ContainsYInteger%"/>
<COMMENT/>
<COMMENT Value="Check every 0.5 second for 20 seconds to see if the \"Size\" field has changed"/>
<VARIABLE SET STRING Option="\x00" Destination="%FieldCapture2%" NoEmbeddedVars="FALSE"/>
<COMMENT/>
<REPEAT START Start="1" Step="1" Count="40" Save="TRUE" Variable="%Count%"/>
<MOUSE MOVE Option="\x02" X="%SizeXInteger%" Y="%SizeYInteger%" Control="%PropertyControl%" _PROMPT="0x000A" _COMMENT="Centre of the \"Size\" field"/>
<MOUSE LEFT CLICK/>
<TEXT TYPE Action="0" Text="<HOME><SHIFT><END>" _COMMENT="Select the \"Size\" field"/>
<TEXT TYPE Action="0" Text="<CONTROL>c"/>
<DELAY Flags="\x02" Time="50"/>
<VARIABLE SET STRING Option="\x02" Destination="%FieldCapture1%" NoEmbeddedVars="FALSE"/>
<DELAY Flags="\x02" Time="50"/>
<COMMENT/>
<IF VARIABLE Variable="%FieldCapture1%" Condition="\x00" Value="%FieldCapture2%" IgnoreCase="FALSE" _COMMENT="Is \"Size\" field unchanged after two consecutive loops?"/>
<VARIABLE SET BOOL Destination="%IsSizeFound%" Command="263" Value="TRUE" _COMMENT="Then we've captured the size!"/>
<REPEAT EXIT/>
<ELSE/>
<VARIABLE SET STRING Option="\x00" Destination="%FieldCapture2%" Value="%FieldCapture1%" NoEmbeddedVars="FALSE"/>
<DELAY Flags="\x02" Time="500"/>
<END IF/>
<COMMENT/>
<END REPEAT/>
<COMMENT/>
<IF VARIABLE Variable="%IsSizeFound%" Condition="\x00" Value="True" IgnoreCase="FALSE"/>
<COMMENT Value="Capture content of \"Contains\" field"/>
<MOUSE MOVE Option="\x02" X="%ContainsXInteger%" Y="%ContainsYInteger%" Control="%PropertyControl%" _PROMPT="0x000A" _COMMENT="Centre of the \"Contains\" field"/>
<MOUSE LEFT CLICK/>
<TEXT TYPE Action="0" Text="<HOME><SHIFT><END>" _COMMENT="Select the \"Contains\" field"/>
<TEXT TYPE Action="0" Text="<CONTROL>c"/>
<DELAY Flags="\x02" Time="50"/>
<VARIABLE SET STRING Option="\x02" Destination="%Contains%" NoEmbeddedVars="FALSE"/>
<COMMENT/>
<TEXT TYPE Action="0" Text="<ESC>" _COMMENT="Close the \"Properties\" window"/>
<COMMENT/>
<COMMENT Value="Reformat \"Size\" to delete everything inside parentheses"/>
<VARIABLE SET STRING Option="\x00" Destination="%Size%" Value="%FieldCapture1%" NoEmbeddedVars="FALSE"/>
<VARIABLE SET INTEGER Option="\x0D" Destination="%LengthSize%" Text_Variable="%Size%"/>
<VARIABLE SET INTEGER Option="\x0E" Destination="%OpenParenPos%" Text_Variable="%Size%" Text="(" Ignore_Case="FALSE"/>
<VARIABLE MODIFY STRING Option="\x0A" Destination="%Size%" Start="%OpenParenPos%" Count="%LengthSize%"/>
<COMMENT/>
<COMMENT Value="Reformat \"Contains\" so Files and Folders are on separate lines"/>
<VARIABLE MODIFY STRING Option="\x0F" Destination="%Contains%" ToReplace="Files, " ReplaceWith="Files\r\n" All="FALSE" IgnoreCase="FALSE" NoEmbeddedVars="FALSE"/>
<COMMENT/>
<COMMENT Value="Output result"/>
<MESSAGEBOX Caption="Info from \"%WinTitle%\"" Message="%Contains%\r\n\r\n%Size%\r\n\r\n(Iterations: %Count%)" Icon="2"/>
<ELSE/>
<COMMENT Value="Report that we couldn't get solutions"/>
<MESSAGEBOX Caption="The macro failed!" Message="Best guess for size: %FieldCapture1%\r\n\r\n(Iterations: %Count%)" Icon="4"/>
<END IF/>

 

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