无忧启动论坛

标题: 通过注册表执行多行批处理脚本 [打印本页]

作者: hlowld    时间: 2025-2-13 12:57
标题: 通过注册表执行多行批处理脚本
众所周知,修改注册表,可实现右键菜单或者双击文件执行一个命令
例如
[HKEY_CLASSES_ROOT\inifile\shell\open\command]
@="\"Notepad.exe\" \"%1\""
可实现双击inifile(*.ini)时执行 "Notepad.exe 双击的文件的完整路径",即:将 双击的文件的完整路径 作为参数运行 Notepad.exe,效果就是Notepad.exe打开了 双击的文件

如果我希望 右键菜单或者双击文件执行一段批处理脚本,该如何实现呢
简单:
[HKCR\DesktopBackground\shell\重启 Explorer\command]
@="\"C:\\somewhere\\resetexplorer.cmd\""

如果我不希望有命令行黑框闪现呢
换成mshta.exe vbscript:createobject("wscript.shell").run("some.cmd",0)(window.close)调用即可
[HKCR\DesktopBackground\shell\重启 Explorer\command]
@="mshta.exe vbscript:createobject(\"wscript.shell\").run(\"C:\\somewhere\\resetexplorer.cmd\",0)(window.close)"

如果我不希望批处理文件resetexplorer.cmd存在呢,这才是这篇帖子真正要说的,以下是示例

@echo off & setlocal ENABLEDELAYEDEXPANSION

::改造批处理脚本,双引号替换为两个双引号,改为1行,用 & 连接语句,该加括号加,该转义括号转义 ^(
set "batCmd=taskkill /f /im explorer.exe & start """" ""explorer.exe"""

::自动执行注册表所需转义
set "batCmd1=!batCmd:%%=%%%%!"
set "batCmd2=!batCmd1:""=\"\"!"
::写入注册表
Reg.exe add "HKCR\DesktopBackground\Shell\重启 Explorer\command" /ve /t REG_SZ /d "mshta.exe vbscript:createobject(\"wscript.shell\").run(\"cmd /c !batCmd2!\",0)(window.close)" /f

由于需要将需执行的批处理脚本改为1行,所以对于太过于复杂的批处理是不适合的,这种情况就老实让批处理文件存在吧



作者: 小灰兔    时间: 2025-2-13 13:31
感谢分享
作者: zyy    时间: 2025-2-13 13:52
谢谢分享,学习一下
作者: 进士小站    时间: 2025-2-13 14:30
这个还真没有玩过呢
作者: yc2428    时间: 2025-2-13 15:55
谢谢分享
作者: ynb168c    时间: 2025-2-13 17:05

谢谢分享
作者: chairmansu    时间: 2025-2-13 18:27
感谢分享
作者: nongren    时间: 2025-2-13 19:51
感谢分享。
作者: HuoDD    时间: 7 天前
沙发
作者: wang1126    时间: 7 天前
谢谢楼主分享
作者: 忧心的启    时间: 7 天前
学习学习,感谢分享。
作者: guong    时间: 7 天前
感谢分享
作者: whsvchy    时间: 7 天前
谢谢分享
作者: Interesting    时间: 6 天前
感谢分享




欢迎光临 无忧启动论坛 (http://wuyou.net./) Powered by Discuz! X3.3