Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Simple Custom Context Menu Commands - How can a VB Script be made to run that uses the file path/name that was right clicked?

I downloaded a file and wanted to verify it's MD5 Checksum. 7Zip's file context menu output doesn't include an MD5 checksum, so I downloaded fciv.exe from the Windows site, and copied it into my System32 folder.

Then I went down the rabbit hole of trying to add a custom context menu item. I got as far as to see that I could modify the registry at Computer\HKEY_CLASSES_ROOT*\shell and add a MD5 key with a command key underneath it to execute cmd /k fciv.exe "%1" as a solution.

However, I wanted to go further and use a VB Script to send the output to a simple message box instead of having the console open up. I found code here as follows:

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

This was where I got stuck:

  1. I have no idea how to adapt the script to use the file path available from the right click menu and second when I even try to run the script.

  2. When I even try to run the script using the context menu, Windows blocks it with a "This app can't run on your PC" pop-up.

Any suggestions? Bonus points if it's to a dialog box where the text can be copied. Thanks in advance!

like image 934
Rdt Avatar asked Nov 06 '22 04:11

Rdt


1 Answers

Assuming, for example, that you have created c:\temp\md5.vbs (and that your script works!), why don't you set the command value to (by importing this registry):

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\""

and your script to:

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

This is untested, and you may want to 'variablise' (environment variable?) the location of c:\temp\ in the real world as opposed to a hard coded path...

like image 136
Captain_Planet Avatar answered Nov 15 '22 08:11

Captain_Planet