批处理笔记(15) - 右键菜单执行批处理文件

本篇无知识点,只是将前面写的,弄到右键菜单,方便随时调用。

对以前批处理代码进行调整,主要是对进入目录参数进行调整。

这里把一个批处理文件做两份处理:

  • 在文件上鼠标右键的:set "HandleDir=%~1" 对应到 HKEY_CLASSES_ROOT\Directory\shell\
  • 在目录空白区域右键的:set "HandleDir=%cd%" 对应到 HKEY_CLASSES_ROOT\Directory\Background\

执行时分别为:

  • @="cmd.exe /c C:\\tool\\bat\\dir.bat %1 \"%V\""
  • @="cmd.exe /c C:\\tool\\bat\\dir-bg.bat %cd% \"%V\""

不贴源码,在网盘下载批处理文件和写入注册表文件(提取码1024),注册表文件中引用批处理文件地址是C:\tool\bat

  • 文件命名操作 - file.bat, file-bg.bat
  • 目录命名操作 - dir.bat, dir-bg.bat
  • 获取文件和目录名 - name.bat, name-bg.bat

建立对应路径关系后,执行文件和目录命名操作.reg,然后在目标目录鼠标右键就可以选择相关操作项了。


右键菜单添加命令行入口.reg保存并执行,在任何目录即可调用cmd。此功能跟批处理没关系,可忽略。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
Windows Registry Editor Version 5.00

; Remove Settings
; [-HKEY_CLASSES_ROOT\Directory\shell\CommandPrompt]
; [-HKEY_CLASSES_ROOT\Directory\Background\shell\CommandPrompt]
; [-HKEY_CLASSES_ROOT\Drive\shell\CommandPrompt]
; [-HKEY_CLASSES_ROOT\LibraryFolder\background\shell\CommandPrompt]

; Command Prompt
; ========================================================

[HKEY_CLASSES_ROOT\Directory\shell\CommandPrompt]
"MUIVerb"="Command Prompt"
"Icon"="cmd.exe"
[HKEY_CLASSES_ROOT\Directory\shell\CommandPrompt\command]
@="cmd.exe /s /k pushd \"%V\""


[HKEY_CLASSES_ROOT\Directory\Background\shell\CommandPrompt]
"MUIVerb"="Command Prompt"
"Icon"="cmd.exe"
[HKEY_CLASSES_ROOT\Directory\Background\shell\CommandPrompt\command]
@="cmd.exe /s /k pushd \"%V\""


[HKEY_CLASSES_ROOT\Drive\shell\CommandPrompt]
"MUIVerb"="Command Prompt"
"Icon"="cmd.exe"
[HKEY_CLASSES_ROOT\Drive\shell\CommandPrompt\command]
@="cmd.exe /s /k pushd \"%V\""


[HKEY_CLASSES_ROOT\LibraryFolder\background\shell\CommandPrompt]
"MUIVerb"="Command Prompt"
"Icon"="cmd.exe"
[HKEY_CLASSES_ROOT\LibraryFolder\background\shell\CommandPrompt\command]
@="cmd.exe /s /k pushd \"%V\""

; Remove Settings
;[-HKEY_CLASSES_ROOT\Directory\shell\runas]
;[-HKEY_CLASSES_ROOT\Directory\Background\shell\runas]
;[-HKEY_CLASSES_ROOT\Drive\shell\runas]
;[-HKEY_CLASSES_ROOT\LibraryFolder\background\shell\runas]

; Command Prompt Administrator
; ========================================================

[HKEY_CLASSES_ROOT\Directory\shell\runas]
"MUIVerb"="Command Prompt Administrator(&X)"
"Icon"="cmd.exe"
"HasLUAShield"=""
[HKEY_CLASSES_ROOT\Directory\shell\runas\command]
@="cmd.exe /s /k pushd \"%V\""


[HKEY_CLASSES_ROOT\Directory\Background\shell\runas]
"MUIVerb"="Command Prompt Administrator(&X)"
"Icon"="cmd.exe"
"HasLUAShield"=""
[HKEY_CLASSES_ROOT\Directory\Background\shell\runas\command]
@="cmd.exe /s /k pushd \"%V\""


[HKEY_CLASSES_ROOT\Drive\shell\runas]
"MUIVerb"="Command Prompt Administrator(&X)"
"Icon"="cmd.exe"
"HasLUAShield"=""
[HKEY_CLASSES_ROOT\Drive\shell\runas\command]
@="cmd.exe /s /k pushd \"%V\""


[HKEY_CLASSES_ROOT\LibraryFolder\background\shell\runas]
"MUIVerb"="Command Prompt Administrator(&X)"
"Icon"="cmd.exe"
"HasLUAShield"=""
[HKEY_CLASSES_ROOT\LibraryFolder\background\shell\runas\command]
@="cmd.exe /s /k pushd \"%V\""

(本篇结束)

许可协议:自由转载-保持署名-非商业性使用-禁止演绎 (CC BY-NC-ND 4.0)

By 小可 from https://xinlu.ink/tech/batch-in-action-15-run-batch-at-folder-context-menu.html