创建简单的自定义上下文菜单命令-如何运行使用右键单击的文件路径/名称的VB脚本?

问题描述

我下载了一个文件,并想验证它的MD5校验和。 7Zip的文件上下文菜单输出不包含MD5校验和,因此我从Windows站点下载了fciv.exe,并将其复制到了System32文件夹中。

然后,我尝试添加一个自定义上下文菜单项。我发现我可以在Computer \ HKEY_CLASSES_ROOT * \ shell处修改注册表,并在其下面添加带有命令键的MD5密钥以执行cmd /k fciv.exe "%1"作为解决方案。

但是,我想走得更远,并使用VB脚本将输出发送到简单的消息框,而不是打开控制台。我发现代码here如下:

Option Explicit

Const WshRunning = 0
Const WshFinished = 1
Const WshFailed = 2

Dim shell : Set shell = CreateObject("WScript.Shell")
Dim exec : Set exec = shell.Exec("fciv.exe filename-from-right-click")

While exec.Status = WshRunning
    WScript.Sleep 50
Wend

Dim output

If exec.Status = WshFailed Then
    output = exec.StdErr.ReadAll
Else
    output = exec.StdOut.ReadAll
End If

WScript.Echo output

这就是我被困住的地方:

  1. 我不知道如何修改脚本以使用右键单击菜单中可用的文件路径 第二,甚至在我尝试运行脚本时。

  2. 当我什至尝试使用上下文菜单运行脚本时,Windows都会弹出一个“此应用程序无法在您的PC上运行”的弹出窗口。

有什么建议吗?如果它是可复制文本的对话框,则加分。预先感谢!

解决方法

例如,假设您创建了c:\ temp \ md5.vbs(并且脚本可以正常工作!),为什么不将命令值设置为(通过导入此注册表):

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\MD5]
@="Get MD5 Checksum"

[HKEY_CLASSES_ROOT\*\shell\MD5\command]
@="wscript.exe \"c:\\temp\\md5.vbs\" \"%1\""

和您的脚本可以:

Option Explicit

Const WshRunning = 0
Const WshFinished = 1
Const WshFailed = 2

Dim shell : Set shell = CreateObject("WScript.Shell")
Dim exec : Set exec = shell.Exec("fciv.exe " & chr(34) & Wscript.Arguments(0) & chr(34))

While exec.Status = WshRunning
    WScript.Sleep 50
Wend

Dim output

If exec.Status = WshFailed Then
    output = exec.StdErr.ReadAll
Else
    output = exec.StdOut.ReadAll
    InputBox "Copy and paste your MD5 checksum","MD5 Checksum",output 
End If

Set exec = Nothing
Set shell = Nothing

这是未经测试的,您可能想要“可变”(环境变量?)c:\ temp \在现实世界中的位置,而不是硬编码的路径...