windows 右键添加cmd

背景:用bat编写了一个小工具(会频繁用到),每次使用时都要到指定位置去查找,非常的不方便。有没有一种办法直接把这个bat脚本添加到鼠标右键呢?答案当然是肯定的!实现过程记录如下:

1. 当鼠标选中文件时,右击弹出菜单选项中添加的自定义的功能选项

下文中出现DoSomething的地方都可以替换成任意的名字(但不建议使用中文

1.1 效果展示:

1.2 实现过程:

1.2.1 install.bat文件(右击-->以管理员身份运行install.bat,即可完成自动加载

该脚本负责将DoSomething.bat这个脚本注册到右键

@echo off

set currentPath=%~dp0
set regPath=%currentPath:\=\\%

echo Windows Registry Editor Version 5.00 > install.reg
echo. >> install.reg

echo [HKEY_CLASSES_ROOT\*\shell\DoSomething] >> install.reg
echo @="DoSomething" >> install.reg
echo "Icon"="\"%regPath%favicon.ico\"" >> install.reg
echo. >> install.reg

echo [HKEY_CLASSES_ROOT\*\shell\DoSomething\command] >> install.reg
echo @="\"%regPath%DoSomething.bat\" \"%%1\"" >> install.reg

echo Installing...

regedit /S install.reg
del /f /q install.reg

echo install successfully.

pause

1.2.1.1 install.bat生成的中间文件install.reg内容如下:(该文本可以直接双击添加下面内容到windows注册表中)

Windows Registry Editor Version 5.00 
 
[HKEY_CLASSES_ROOT\*\shell\DoSomething] 
@="DoSomething" 
"Icon"="\"C:\\Users\\eternal_heart\\Desktop\\CMDTest\\favicon.ico\"" 
 
[HKEY_CLASSES_ROOT\*\shell\DoSomething\command] 
@="\"C:\\Users\\eternal_heart\\Desktop\\CMDTest\\DoSomething.bat\" \"%1\"" 

1.2.2 favicon.ico文件,功能:右键菜单前面的小图标

icon文件制作:ico在线制作工具

1.2.3 Uninstall.bat文件,功能:将DoSomething从右键菜单中移除

@echo off

echo Windows Registry Editor Version 5.00 > uninstall.reg
echo. >> uninstall.reg
echo [-HKEY_CLASSES_ROOT\*\shell\DoSomething] >> uninstall.reg


regedit /S uninstall.reg
del /f /q uninstall.reg

echo Uninstall successfully.

pause

1.2.3.1 双击uninstall.bat从右键菜单中移除DoSomething,生成的中间文件uninstall.reg内容如下: (该文本可以直接双击删除windows注册表中HKEY_CLASSES_ROOT\*\shell\DoSomething文件夹)

Windows Registry Editor Version 5.00 
 
[-HKEY_CLASSES_ROOT\*\shell\DoSomething] 

1.2.4 DoSomething.bat核心内容自己实现(即点击右键菜单后具体要干什么由这个脚本控制)

::点击《DoSomething》后执行的脚本
::核心内容由自己实现,可以调用其他exe/bat/python等window上的可执行文件

@echo off
::鼠标光标所在文件的绝对路径,右击菜单上的DoSomething传递进来的第二个参数
::(%0第一个参数:右击菜单上的DoSomething时所执行的文件的绝对路径)
echo %1
echo "Waitting to impl."
pause

1.3 添加完成后,查看window注册表

2. 在文件夹空白内处右击时,弹出菜单选项中添加的自定义的功能选项

2.1 文件结构及效果展示

2.2 实现过程

2.2.1 Install.bat的实现如下:(可直接复制到本地,把 @="MiniTool" 中" MiniTool"修改成你想要在右键菜单中显示的名字即可)

安装时,以管理员身份运行

@echo off

set currentPath=%~dp0
set regPath=%currentPath:\=\\%

echo Windows Registry Editor Version 5.00 > install.reg
echo. >> install.reg

::HKEY_CLASSES_ROOT\Directory\Background\shell\路径下创建一个MiniToolFolder文件夹即 新建--->项(k)
echo [HKEY_CLASSES_ROOT\Directory\Background\shell\MiniToolFolder] >> install.reg

::MiniToolFolder路径下创建默认值为"MiniTool" 该名字会出现在右击菜单中
echo @="MiniTool" >> install.reg

::MiniToolFolder路径下创建字符串值名称为:Icon,Icon的值为MiniToolIcon.ico所在绝对路径,该Icon会出现在右击菜单中
echo "Icon"="\"%regPath%MiniToolIcon.ico\"" >> install.reg
echo. >> install.reg

::MiniToolFolder路径下创建command新建-->项(k)
echo [HKEY_CLASSES_ROOT\Directory\Background\shell\MiniToolFolder\command] >> install.reg

::添加点击时所执行的bat脚本,并传递参数(%V)
echo @="\"%regPath%MiniTool.bat\" \"%%V\"" >> install.reg

echo Installing...

regedit /S install.reg
del /f /q install.reg

echo install successfully.

pause

2.2.1.1 install.reg中间脚本内容如下(注:在不同的位置执行Install.bat,则生成的中间文件内容会有差异)

Windows Registry Editor Version 5.00 
 
[HKEY_CLASSES_ROOT\Directory\Background\shell\MiniToolFolder] 
@="MiniTool" 
"Icon"="\"C:\\Users\\eternal_heart\\Desktop\\CMD_background\\MiniToolIcon.ico\"" 
 
[HKEY_CLASSES_ROOT\Directory\Background\shell\MiniTool\command] 
@="\"C:\\Users\\eternal_heart\\Desktop\\CMD_background\\MiniTool.bat\" \"%V\"" 

2.2.2  MiniToolIcon.ico文件,功能:右键菜单前面的小图标

icon文件制作:ico在线制作工具

2.2.3 Uninstall.bat内容(卸载时同样以管理员身份运行

@echo off

echo Windows Registry Editor Version 5.00 > uninstall.reg
echo. >> uninstall.reg
::MiniToolFolder修改成在Install中创建的文件夹的名字
echo [-HKEY_CLASSES_ROOT\Directory\Background\shell\MiniToolFolder] >> uninstall.reg


regedit /S uninstall.reg
del /f /q uninstall.reg

echo Uninstall successfully.

pause

2.2.4  MiniTool.bat心内容自己实现(即点击右键菜单后具体要干什么由这个脚本控制)

::点击《MiniToll》后执行的脚本
::核心内容由自己实现,可以调用其他exe/bat/python等window上的可执行文件

@echo off
::%0 即可执行脚本的绝对路径+名字
echo arg0: %0

::%1 即通过%V传递进来的参数
echo arg1: %1


echo Right-click on a blank space in the folder

echo Waitting you to impl...
pause

2.3 安装成功后,进入注册表查看(添加成功可忽略查看注册表步骤)

 

3. 在选中文件夹后右击时,弹出菜单选项中添加的自定义的功能(MiniTool)选项

3.1 效果展示:

执行结果:

3.2 实现过程:

3.2.1 Install.bat的实现 (以管理员身份运行即可安装MiniTool)

方法一:添加到HKEY_CLASSES_ROOT\Folder\shell\下

@echo off

set currentPath=%~dp0
set regPath=%currentPath:\=\\%

echo Windows Registry Editor Version 5.00 > install.reg
echo. >> install.reg

::HKEY_CLASSES_ROOT\Directory\Background\shell\路径下创建一个MiniToolFolder文件夹即 新建--->项(k)
echo [HKEY_CLASSES_ROOT\Folder\shell\MiniToolFolder] >> install.reg

::MiniToolFolder路径下创建默认值为"MiniTool" 该名字会出现在右击菜单中
echo @="MiniTool" >> install.reg

::MiniToolFolder路径下创建字符串值名称为:Icon,Icon的值为MiniToolIcon.ico所在绝对路径,该Icon会出现在右击菜单中
echo "Icon"="\"%regPath%MiniToolIcon.ico\"" >> install.reg
echo. >> install.reg

::MiniToolFolder路径下创建command新建-->项(k)
echo [HKEY_CLASSES_ROOT\Folder\shell\MiniToolFolder\command] >> install.reg

::添加点击时所执行的bat脚本,并传递参数(%V)
echo @="\"%regPath%MiniTool.bat\" \"%%V\"" >> install.reg

echo Installing...

regedit /S install.reg
::del /f /q install.reg

echo install successfully.

pause

方法二:添加到HKEY_CLASSES_ROOT\Directory\shell\下,实现方法与前面的基本一致,代码不再赘述,只展示添加成功后的注册表

3.2.2  MiniToolIcon.ico文件,功能:右键菜单前面的小图标

icon文件制作:ico在线制作工具

3.2.3  MiniTool.bat心内容自己实现(即点击右键菜单MiniTool后具体要干什么由这个脚本控制)

::点击《MiniToll》后执行的脚本
::核心内容由自己实现,可以调用其他exe/bat/python等window上的可执行文件

@echo off
::%0 即可执行脚本的绝对路径+名字
echo arg0: %0

::%1 即通过%V传递进来的参数
echo arg1: %1


echo Right-click on the folder

echo Waitting you to impl...
pause

3.2.4 Uninstall.bat脚本内容如下:(以管理员身份运行即可卸载MiniTool)

@echo off

echo Windows Registry Editor Version 5.00 > uninstall.reg
echo. >> uninstall.reg
echo [-HKEY_CLASSES_ROOT\Directory\Directory\D\shell\MiniToolFolder] >> uninstall.reg


regedit /S uninstall.reg
del /f /q uninstall.reg

echo Uninstall successfully.

pause

4. 添加多级联动(后面再完善)

http://blog.sina.com.cn/s/blog_a7c071b30102wm4w.html

http://blog.sina.com.cn/s/blog_50feb9170102yn1z.html

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值