windows - CMD/Batch/Registry - 字符串替换没有按预期工作?

标签 windows shell batch-file cmd registry

我在尝试替换 Windows 注册表项的一行代码中的子字符串时遇到了最有趣的事情

[HKEY_CLASSES_ROOT\PING\shell\open\command]
@="C:\\Windows\\System32\\ping.exe %1 -t"

场景和上下文:

上下文是一个自定义 url 协议(protocol),它将打开一个 shell 并连续 ping 到特定的 ip,我想从作为 传递的 uri 中删除协议(protocol) ping:// >%1。 当然我可以使用批处理文件,但我更愿意将其全部放在注册表中的一行中

我尝试过的:

到目前为止,我已经尝试使用 & 设置一个 var 然后回显它来附加一个命令列表。

尝试了 call set remove=ping:// & call set mynewvar=%1:%remove=% & ping.exe %mynewvar% -t

我尝试使用 %%

多次扩展变量

基本上我得到了奇怪的结果或者替换根本不起作用

不确定我做错了什么?输入这个,我开始认为我忽略了它在字符串 var @="..."

中的事实

谢谢

注册表项

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"

浏览器输入 url ping://8.8.8.8

cmd 提示输出 Ping 请求找不到主机 ping://8.8.8.8/ar。请检查名称并重试。

我注意到 ip 来自 %var% 之后的 ar 它似乎使用 %v 而不是 %var

最佳答案

明白了.. %v 实际上是

%v – 对于动词 none 表示 all。如果没有传递参数,这是工作目录。

this answer得到的

工作代码

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"

关于windows - CMD/Batch/Registry - 字符串替换没有按预期工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23334946/

相关文章:

java - 多个摄像头同时openimaj

使用 pysftp 通过 HTTP 代理进行 Python 连接

bash - 下一个命令完成后,如何停止从 bash 启动的后台进程?

windows - 通过 IOCP 进行串行通信

windows - MidiInProc 回调在什么上下文中执行?

json - Linux grep命令从json中查找key的值

unix - 如何更改 shell 脚本的字符编码?

windows - for/f "the system cannot find the file"在 windows 命令脚本中

linux - 反向差异功能

php - 如何使用cmd批量多次运行.php脚本?