Run Here tool

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
siberia-man
Posts: 174
Joined: 26 Dec 2013 09:28
Contact:

Run Here tool

#1 Post by siberia-man » 04 Aug 2020 21:03

I am happy to announce my tiny tool that allows to manage the so called PowerToy add-ons powered to run some commands over folders and drives.

There is short description explaining the usage of the tool when it is ran with no arguments:

Code: Select all

Open the command identified by the menu over the folder. 

run-here [/A] /I "menu" [/K iconfile] [/NO-CHECK] command [arguments]
run-here [/A] /U "menu"
run-here [/A] /S "menu"

menu       The menu item of the Windows Explorer context menu.
command    The command to be executed on selecting the menu.
arguments  Arguments to be passed to the command (otherwise %V).
/A         Apply for all users. By default, for the current user only.
/I         Install the menu item.
/U         Uninstall the existing item. 
/S         Show the Registry entries if they exist.
/K icon    Set the menu icon.
/NO-CHECK  Do not check the path to the command when installing.
This script manages easily and very fast the commands necessary for running on the right click on folders -- for exampe "Run Cygwin Here" or "Run Cmd Here" Today I decided extend the script to enable icons on the command menu and install BusyBox as a new command in the same way:

Code: Select all

run-here /i "Run BusyBox Here" /k c:\PROGS\opt\busybox\busybox64.exe c:\PROGS\opt\busybox\busybox64.exe sh -c "cd '%V' ; sh"
There is latest version of the script to the moment of posting. Also it can be found by this link -- https://github.com/ildar-shaimordanov/c ... n-here.bat.

Code: Select all

@echo off

setlocal

if /i "%~1" == "" goto :run_help

:: See also
:: https://www.howtogeek.com/165268/how-to-add-open-powershell-here-to-the-context-menu-in-windows/
:: https://www.tenforums.com/tutorials/60175-open-powershell-window-here-context-menu-add-windows-10-a.html
:: https://gist.github.com/davecan/f3045266aa9e3441211ab55f9db70c2b

:: Current user
:: HKEY_CURRENT_USER\Software\Classes\Drive\shell\<menu>\command
:: HKEY_CURRENT_USER\Software\Classes\Directory\shell\<menu>\command
:: HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\<menu>\command
set "run_rootkey=HKEY_CURRENT_USER"

:: All users
:: HKEY_LOCAL_MACHINE\Software\Classes\Drive\shell\<menu>\command
:: HKEY_LOCAL_MACHINE\Software\Classes\Directory\shell\<menu>\command
:: HKEY_LOCAL_MACHINE\Software\Classes\Directory\Background\shell\<menu>\command
if /i "%~1" == "/A" (
	set "run_rootkey=HKEY_LOCAL_MACHINE"
	shift
)

set "run_classkey=%run_rootkey%\Software\Classes"

:: /I, /U or /S
set "run_action=%~1"
shift

:: "menu"
set "run_menu=%~1"
shift

if not defined run_menu (
	>&2 echo:Menu not declared
	goto :run_help
)

if not "%run_menu%" == "%run_menu:\=%" (
	>&2 echo:Incorrect menu: %run_menu%
	goto :EOF
)

set "run_subkey_list=Drive Directory Directory\Background"

if /i "%run_action%" == "/I" goto :run_install
if /i "%run_action%" == "/U" goto :run_uninstall
if /i "%run_action%" == "/S" goto :run_show

>&2 echo:Unknown action: %run_action%
goto :run_help


:run_install
if /i "%run_menu%" == "cmd" goto :run_forbidden

:: [/K iconfile]
set "run_iconfile="
if /i "%~1" == "/K" (
	set "run_iconfile=%~2"
	shift
	shift
)

:: [/NO-CHECK]
set "run_nocheck="
if /i "%~1" == "/NO-CHECK" (
	set "run_nocheck=1"
	shift
)

:: command
set "run_command=%~1"
shift

if not defined run_command (
	>&2 echo:Command not specified
	goto :run_help
)

if defined run_nocheck (
	set "run_progpath=%run_command%"
	goto :run_install_args
)

set "run_progpath="
for /f "tokens=*" %%c in ( "%run_command%" ) do (
	if not "%%~$PATH:c" == "" set "run_progpath=%%~$PATH:c"
	if exist "%%~fc" set "run_progpath=%%~fc"
)

if not defined run_progpath (
	>&2 echo:Command not found: %run_command%
	goto :run_help
)

:run_install_args

:: [arguments] or "%V"
set "run_arguments="

:run_install_args_begin
if "%~1" == "" goto :run_install_args_end
	set "run_arguments=%run_arguments% %1"
	shift
goto :run_install_args_begin
:run_install_args_end

:: http://superuser.com/a/473602
:: http://www.robvanderwoude.com/ntstart.php
:: http://msdn.microsoft.com/en-us/library/windows/desktop/cc144101%28v=vs.85%29.aspx
if not defined run_arguments set "run_arguments= "%%V""

:: Escape quotes before enclosing within quotes
set "run_arguments=%run_arguments:"=\"%"

for %%s in ( %run_subkey_list% ) do (
	reg add "%run_classkey%\%%s\shell\%run_menu%\command" /ve /d "\"%run_progpath%\"%run_arguments%" /f
)

if defined run_iconfile for %%s in ( %run_subkey_list% ) do (
	reg add "%run_classkey%\%%s\shell\%run_menu%" /v Icon /t REG_SZ /d "%run_iconfile%" /f
)
goto :EOF


:run_uninstall
if /i "%run_menu%" == "cmd" goto :run_forbidden

for %%s in ( %run_subkey_list% ) do (
	reg delete "%run_classkey%\%%s\shell\%run_menu%" /f
)
goto :EOF


:run_show
for %%s in ( %run_subkey_list% ) do (
	reg query "%run_classkey%\%%s\shell\%run_menu%" /s
)
goto :EOF


:run_forbidden
>&2 echo:the operation over this element is forbidden.
goto :EOF


:run_help
echo:Open the command identified by the menu over the folder. 
echo:
echo:%~n0 [/A] /I "menu" [/K iconfile] [/NO-CHECK] command [arguments]
echo:%~n0 [/A] /U "menu"
echo:%~n0 [/A] /S "menu"
echo:
echo:menu       The menu item of the Windows Explorer context menu.
echo:command    The command to be executed on selecting the menu.
echo:arguments  Arguments to be passed to the command (otherwise %%V).
echo:/A         Apply for all users. By default, for the current user only.
echo:/I         Install the menu item.
echo:/U         Uninstall the existing item. 
echo:/S         Show the Registry entries if they exist.
echo:/K icon    Set the menu icon.
echo:/NO-CHECK  Do not check the path to the command when installing.
goto :EOF


Last edited by siberia-man on 16 Dec 2020 03:44, edited 2 times in total.

jfl
Posts: 176
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

Re: Run Here tool

#2 Post by jfl » 07 Aug 2020 03:54

Thanks Ildar, this is a useful tool.

I immediately used it to restore a context menu entry I missed from old versions of Windows:

Code: Select all

run-here /I "Command Here" /K C:\Windows\System32\cmd.exe C:\Windows\System32\cmd.exe
It works, except for a small annoyance: It starts cmd.exe in the parent directory of the directory I right-clicked.

siberia-man
Posts: 174
Joined: 26 Dec 2013 09:28
Contact:

Re: Run Here tool

#3 Post by siberia-man » 07 Aug 2020 04:05

except for a small annoyance: It starts cmd.exe in the parent directory
Some additional arguments are required

Code: Select all

run-here /I "Command Here" /K C:\Windows\System32\cmd.exe C:\Windows\System32\cmd.exe /k cd /d "%V"
In addition...

If someone would like to set up PowerShell in the same way:

Code: Select all

run-here /i "Run PowerShell Here" /k powershell.exe powershell.exe -NoExit -Command Set-Location -LiteralPath '%V'

jfl
Posts: 176
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

Re: Run Here tool

#4 Post by jfl » 07 Aug 2020 06:49

Thanks, works great now :D

siberia-man
Posts: 174
Joined: 26 Dec 2013 09:28
Contact:

Re: Run Here tool

#5 Post by siberia-man » 08 Aug 2020 01:14

Couple of changes:
-- taboo for "\" in the menu text
-- some cosmetic changes

Post Reply