fbpx

[AHK] [WSL] MD5 hash of a selected file with a key press

By January 18, 2018Tech

INFO: Script Requires Bash on Windows 10, also known as windows subsystem on linux (WSL).

If you use Autohotkey to make your own macros and do super awesome stuff with the push of a key, like I do, you may find this useful, specially if you download from the internet a lot, or transfer files over wireless network. You may need to compare the MD5 hash of files to check the integrity of transfer. Here is a quick code I wrote in Autohotkey script to get the MD5 hash of a file that you have selected in windows explorer.

Without further adieu, let me show the script to you.

Backspace::

;Get MD5SUM using WSL of selected file

var1 := % Explorer_GetSelection() ;stores the file path

Explorer_GetSelection(hwnd=””) { ;Script to get the path of the selected file >>>> START <<<<

    hwnd := hwnd ? hwnd : WinExist(“A”)

    WinGetClass classahk_id %hwnd%

    if (class=“CabinetWClass” or class=“ExploreWClass” or class=“Progman”)

        for window in ComObjCreate(“Shell.Application”).Windows

            if (window.hwnd==hwnd)

    sel := window.Document.SelectedItems

    for item in sel

    ToReturn .= item.path “`n”

    return Trim(ToReturn,“`n”)

;Script to get the path of the selected file >>>> END <<<<

StringReplace, var2, var1, \, /, all ;Replace \ with /

StringReplace, var3, var2, :, , all  ;Remove the colon from the address

RegExMatch(var3, “^[A-Z]”, Var4) ;Storing Drive letter to another variable

StringLower, Var4, Var4 ;changing drive letter to lowercase

StringTrimLeft, VarString1, Var3, 1 ;Removing the capital drive letter from the path

VarStringFinal = /mnt/%Var4%%VarString1% ;Joining together all the part of the path to be converted to a linux path

code =

(@echo off

bash -c “md5sum ‘%VarStringFinal%'”

)

run %comspec% /k %code%

return

The script is pretty self explanatory with the comments making it easier to understand. I have the Macro assigned to backspace key on a separate keyboard but you can assign it to any key you want. How I assigned it to a separate keyboard is a post for another day.

Why so many variables you may ask, because you need to change the file path to linux’s file path in WSL for the md5sum to work. Let’s say you have a file called file.txt in C drive, you will need it to be /mnt/c/file.txt in WSL. I used a lot of variables and I am sure a professional coder can make it much shorter.

This is how the end result looks like, see the selected file in the file explorer in the background.

In the end what you have is basically a bash script running inside a batch script running inside an autohotkey script. Scriptception!!