Windows Explorer Context Menu – How to Add Entry for Large Files

context menuwindowswindows-explorer

How can I add an option in the context menu in Windows Explorer only for files larger than some threshold?

For example, when adding an entry in the context menu in Windows Explorer to split files by 15 GB chunks, I'd like the context menu entry to only appear if the selected file is over 15 GB.

Best Answer

(This is a copy of an answer I left on Quora in response to a copy of this question left there.)

Thanks for this question! To be honest, I would normally not put nearly as much effort into answering it. But it happens to be relevant to a project I’m working on, and I’m still in love with how simple it turns out to be, since I thought I’d have to write a Windows program with all sorts of fiddly bits to talk to the Windows shell and add and remove menu options on the fly, and it turns out that it only takes about six new entries in the registry. Which is so cool.

The documentation for this feature is at https://docs.microsoft.com/en-us/windows/win32/shell/context-menu-handlers. It’s a bit daunting, because it discusses all the options, including the complicated ones. But the one you want is “Getting Dynamic Behavior for Static Verbs by Using Advanced Query Syntax”.

Advanced Query Syntax is what you use, without realizing it, when you search for files in the File Explorer. For example, if I just type “test” in the search box, it looks for files with “test” in the name.

enter image description here

But if I use the menus in the Search tab to say “I just want files modified this week, that are between 16K and 1MB”, that will bring up a smaller set of results. And you can see that the menus are doing that by actually adding more terms like “datemodified:thisweek” and “size:small” to the search box — it’s actually building an Advanced Query Syntax query for you.

enter image description here

And if you’re a power user, once you realize that’s what’s going on, you can start to make queries that the menus don’t allow you do to — like “Show me all files with ‘test’ in the name, last modified before the start of 2020, and between 2K and 5K in size” — by using the Advanced Query Syntax keywords yourself (in this case, “test datemodified:<2020–01–01 size:>2000 size:<5000”).

enter image description here

And so this brings us to what you want to do, which is to make a context menu option that divides files. Let’s say that your program that does this is C:\Program Files\divider.exe. You would run it by giving it a filename and a size, like this:

C:\Program Files\divider.exe /size:15000000000 C:\MyData\MyHugeFile.txt

As the rest of the “Creating Shortcut Menu Handlers” documentation shows (insert majestic handwave here), if you just want to add this as a menu option for all text files, you can do it with this set of registry entries:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\txtfile\shell\divide]
@="Divide Large File"

[HKEY_CLASSES_ROOT\txtfile\shell\divide\command]
@="\"C:\\Program Files\\Divider.exe\" /size:15000000000 \"%1\""

This creates a new context menu command, “Divide Large File”, that shows up for text files (files with the extension ‘.txt’). When the user selects that command, the Divider program is launched to divide the file.

enter image description here

But you want something more specific — you want to only display this for really big files. So we add one more magic line to the registry. (Actually I’ll add two — let’s make this only appear when you select exactly one file, since this sounds like a pretty intensive operation.)

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\txtfile\shell\divide]
"AppliesTo"="System.Size:>15000000000"
@="Divide Large File"
"MultiSelectModel"="Single"

[HKEY_CLASSES_ROOT\txtfile\shell\divide\command]
@="\"C:\\Program Files\\Divider.exe\" /size:15000000000 \"%1\""

The “AppliesTo” value says “Only display this menu option if the selected file matches this Advanced Query Syntax query — in this case, if the size is greater than 15 gigabytes.” (And the “MultiSelectModel” line says “Only show this if there’s a single file selected.”)

And now I’ve got this really useful tool in my pocket, that can be used for so many other subtle context-sensitive things, with just a few lines of text added to the registry.

(I’m reminded of the story of the driver who brings their car to a repair shop because the engine is running rough. The mechanic says it’ll be $100 to fix it. The driver agrees. The mechanic peers under the hood for a minute, then grabs a hammer and gives one connector a tap. The engine immediately starts purring like a kitten. The driver is amazed but also cross. “How is that worth $100? All you did was hit it once with a hammer!” The mechanic smiles. “Oh, hitting it with the hammer was free. Knowing where to hit it? That’s what’s worth $100.”)

Related Question