I'm having the most fun trying to replace a substring in one line of code for a windows registry key
[HKEY_CLASSES_ROOT\PING\shell\open\command]
@="C:\\Windows\\System32\\ping.exe %1 -t"
Scenario and Context:
The context is a custom url protocol that will open up a shell with continuous ping to a specific ip and i want to remove the protocol ping://
from the uri that gets passed as %1
.
sure i could use a batch file, but I'd prefer to have it all in one line in the registry
What I've tried:
so far I've tried appending a list of commands with &
setting a var and then echoing it..
tried call set remove=ping://
&
call set mynewvar=%1:%remove=%
&
ping.exe %mynewvar% -t
I've tried expanding the variables a few times using %%
Basically I've been getting weird results or the replace not working at all
Not sure what I'm doing wrong? Typing this, I've started to think I'm overlooking the fact its in string var @="..."
thanks
Registry Key
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\PING]
@="url:ping protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\PING\shell]
[HKEY_CLASSES_ROOT\PING\shell\open]
[HKEY_CLASSES_ROOT\PING\shell\open\command]
@="cmd /k set var=%1 & call set var=%%var:ping://=%% & ping.exe %var% -t"
browser typed in url
ping://8.8.8.8
cmd prompt output
Ping request could not find host ping://8.8.8.8/ar. Please check the name and try again.
I notice the ar
after the ip is from %var%
it appears its using the %v
not %var
Got it.. %v
is actually
%v – For verbs that are none implies all. If there is no parameter passed this is the working directory.
got that from this answer
Working code
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\PING]
@="url:ping protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\PING\shell]
[HKEY_CLASSES_ROOT\PING\shell\open]
[HKEY_CLASSES_ROOT\PING\shell\open\command]
@="cmd /k set myvar=%1 & call set myvar=%%myvar:ping:=%% & call set myvar=%%myvar:/=%% & call ping.exe %%myvar%% -t"
String replace is only possible with variables not with parameters like %1
.
Something like this could be a starting point
set "var=%1" & call var=%%var:ping://=%%
In the registry it could look like
...
[HKEY_CLASSES_ROOT\PING\shell\open\command]
@="cmd /k ( set \"var=%1\" & call set var=%%var:ping://=%% & call set var=%%var:/=%% & call ping.exe %%var%% -t)"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With