Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

RunSub: Shell Context menu example

  • Please log in to reply
No replies to this topic
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
Here's a quick example I wrote that adds itself as a context menu item for folders, and then executes a task.

In this case, the task is to open the file contained in the folder that has the same name as the folder. (It was a request from a coworker)

I don't handle the case where there is more than one file that matches the criteria.

Warning: Adds registry keys.

;run subfolder item of same name

RegRead, KeyExists, HKLM, Software\Classes\Folder\Shell\RunSub

If (KeyExists = "" and ERRORLEVEL = 1)
;ask to install it
  if not A_IsAdmin
     DllCall("shell32\ShellExecuteA", uint, 0, str, "RunAs", str, A_AhkPath
        , str, """" . A_ScriptFullPath . """", str, A_WorkingDir, int, 1)

  RegWrite, REG_EXPAND_SZ, HKLM, Software\Classes\Folder\Shell\RunSub, , Open Subfile of same name
  RegWrite, REG_SZ, HKLM, Software\Classes\Folder\Shell\RunSub\command, , "%A_ScriptFullPath%" "`%L"
  MsgBox, RunSub is now installed.

args = %0%
if (args < 1)
  MsgBox, To use RunSub, Right click on a folder and select "Open Subfile of same name"

arg1 = %1%
SplitPath, arg1, lastdir, dir
ItemsFound := 0

Loop, %dir%\%lastdir%\%lastdir%.*
    itemlist .= A_LOOPFILENAME . "|"
    item%A_index% := A_LOOPFILENAME
    ItemsFound := A_Index

  If (ItemsFound = 0)
    MsgBox, No Items found in this folder with a matching name.
  If (ItemsFound = 1)
    Run, %dir%\%lastdir%\%item1%, %dir%\%lastdir%
  If (ItemsFound > 1)
      ;More than one item found that matches the criteria, must pick one.
       Msgbox, more than one item found


It can be expanded in multiple ways, but I just wanted to share the framework. (And my bad coding styles)