Help import the hash sum from RHash.exe to a text file!

There is a console utility RHash.exe.
The commands are described here.

In "cmd.exe" I enter commands for "RHash.exe":

rhash --md5 "D:\test\ExperienceIndexOK_x64_p.exe"

I get the line:

"b19934746b960dfa7e4096ae25df8229 D:\test\ExperienceIndexOK_x64_p.exe"

How do I do this in dopus in a JS script instead of "text_hash_md5"?

var hash_md5 = "text_rhash_md5".toUpperCase();

Is there a reason you want to use that tool instead of have Opus generate the hash?

Where does Opus come into the picture? This seems more a question about RHash.exe and JScript than it is about Opus.

I want to link DOpus and RHash to use other hash functions that DOpus does not support.

So you want to capture a text output of an external program.
In a nutshell, there are 2 ways to achieve this in jscript:

  • shell.Exec() method which is simple but will briefly show a console window (which may be annoying).
  • shell.Run() method which is more complex (it involves temp files behind the scenes).

Please take a look at this old thread, specifically a tbone's RunHiddenEx function (for which I am thankful and have used it successfully many times) -- it uses a Run() method and achieves exactly what you need.

Here's an example using a temp-file:

(The CaptureCommandOutput function, specifically. The rest can be ignored.)

The result is the following script:

function OnInit(initData) {
    initData.name = "Import_HASH_from_RHash_to_file";
    initData.version = "1.0";
    initData.copyright = "(c) 2021 t23111";
    initData.url = "";
    initData.desc = "Import HASH summ from RHash.exe to text file!";
    initData.default_enable = true;
    initData.min_version = "";

    var cmd = initData.AddCommand();
    cmd.name = "Import_HASH_from_RHash_to_file";
    cmd.method = "Import_HASH_from_RHash_to_file";
    //cmd.desc = "Import HASH summ from RHash.exe to text file!";
    //cmd.label = "Import_HASH_from_RHash_to_file";
    //cmd.template = "FILE,ADD/S,REMOVE/S,TOGGLE/S";
    //cmd.hide = false;
    //cmd.icon = "edit";
}

function Import_HASH_from_RHash_to_file(clickData) {
    if (clickData.func.sourcetab.selected.count == 0) {
        var dlg = DOpus.Dlg;
        dlg.message = "Please select a file!";
        dlg.Show;
        return;
    } else {
        for (var eSel = new Enumerator(clickData.func.sourcetab.selected); !eSel.atEnd(); eSel.moveNext()) {
            if (eSel.item().is_dir) {
                var dlg = DOpus.Dlg;
                dlg.message = eSel.item().RealPath + " is folder, not a file!";
                dlg.Show;
            } else {

                //New File Name
                var filepath = eSel.item().path + "\\";
                var filename = eSel.item().name;
                var filepathfull = filepath + filename;
                var filenamenew = eSel.item().name + "_RHash.txt";
                var filepathnew = filepath + filenamenew;
                var filesize = eSel.item().size;

                //Getting RHash program output
                var exe = "rhash.exe";
				var exe_path = "D:\\test\\RHash-1.4.1-win64\\";
				var exe_path_full = "\"" + exe_path + exe + "\"";
				var hash = "crc32";
					/*
					AICH - %{aich}
					BLAKE2B - %{blake2b}
					BLAKE2S - %{blake2s}
					BTIH - %{btih}
					CRC32 - %{crc32}
					CRC32C - %{crc32c}
					ED2K - %{ed2k}
					ED2K-LINK - %{ed2k-link}
					EDON-R256 - %{edon-r256}
					EDON-R512 - %{edon-r512}
					GOST12-256 - %{gost12-256}
					GOST12-512 - %{gost12-512}
					GOST94 - %{gost94}
					GOST94-CRYPTOPRO - %{gost94-cryptopro}
					HAS-160 - %{has160}
					MD4 - %{md4}
					MD5 - %{md5}
					RIPEMD-160 - %{ripemd160}
					SHA1 - %{sha1}
					SHA-224 - %{sha-224}
					SHA-256 - %{sha-256}
					SHA-384 - %{sha-384}
					SHA-512 - %{sha-512}
					SHA3-224 - %{sha3-224}
					SHA3-256 - %{sha3-256}
					SHA3-384 - %{sha3-384}
					SHA3-512 - %{sha3-512}
					SNEFRU-128 - %{snefru128}
					SNEFRU-256 - %{snefru256}
					TIGER - %{tiger}
					TTH - %{tth}
					WHIRLPOOL - %{whirlpool}
					*/

                var params = 
					" --printf=\"%p - %{"+hash+"}\" " 
					//" --list-hashes "//for get list all support hash
					//" --printf=\"%{"+hash+"}\" " 
					+ "\"" + filepathfull + "\"";
                var rhash_out = RunEx(exe_path_full, params).toUpperCase();
				var rhash_out_2 = RunHiddenEx(exe_path_full, params).toUpperCase();
                
                //File Content
                var filecontent =
                    "File Name: \n" + eSel.item().name
                     + "\n" + "File Path: \n" + filepath
                     + "\n" + "File Path Full: \n" + filepathfull
                     + "\n\n" + "exe: \n" + exe
                     + "\n" + "exe_path: \n" + exe_path
                     + "\n" + "exe_path_full: \n" + exe_path_full
                     + "\n" + "params: \n" + params
                     + "\n" + "RHash command: \n" + exe_path_full + params
                     + "\n\n" + "HASH from RHash, RunEx: \n" + rhash_out
					 + "\n\n" + "HASH from RHash, RunHiddenEx: \n" + rhash_out_2
                     + "\n";
                var filecontentencode = DOpus.Create.StringTools.Encode(filecontent, "utf-8 bom"); //Encode to UTF-8-BOM

                var file = DOpus.FSUtil.OpenFile(filepathnew, "wa"); //Create text File for write
                if (file.error == 0) {
                    file.Write(filecontentencode); //Write to file
                    file.Close();
                }

                var dlg = DOpus.Dlg;
                dlg.message = "File is complete!";
                dlg.Show;

            }
        }
    }
}

///////////////////////////////////////////////////////////////////////////////
function RunEx(exe, params){
	//params = (params?" "+params:"");
	var shell = new ActiveXObject("WScript.Shell");
	var exec = shell.Exec(exe + params);
	var stdOut = "", stdErr = "";

	while(exec.Status == 0){
		stdOut += exec.StdOut.ReadAll();
		stdErr += exec.StdErr.ReadAll();
	}

	var result = {
		returncode : exec.ExitCode,
		stdout : stdOut,
		stderr : stdErr
	}
	return result.stdout;
}

///////////////////////////////////////////////////////////////////////////////
function RunHiddenEx ( exe, params, tmpFileBase, tmpFileExt, shell, fso){ //v1.2
      if (!fso)   fso      = new ActiveXObject("Scripting.FilesystemObject");
      if (!shell) shell   = new ActiveXObject("WScript.Shell");
      if (!tmpFileBase)   tmpFileBase = "DO.Temp.";
      if (!tmpFileExt)   tmpFileExt = ".txt";
      var tmpFileStdout = GetTmpFileName(tmpFileBase, tmpFileExt, fso).fullname;
      var tmpFileStderr = tmpFileStdout+".err"+tmpFileExt;
      var cmd = '%comspec% /c ""'+exe+'" '+params+' >"'+tmpFileStdout+'" 2>"'+tmpFileStderr+'""';
      var result = {};
      result.cmd = cmd;
      if (exe.match(/^([A-z]:\\.+|\\\\(.*?)\\.+)$/)){ //test path to exe if given
         if (!fso.FileExists(exe)){
            var msg = "E   Executable not found ["+exe+"]";
            DOpus.Output(msg);
            throw msg;
         }
      }
      result.returncode = shell.Run( cmd, 0, true);
      result.stdout = ReadFile(tmpFileStdout, fso); fso.DeleteFile(tmpFileStdout);
      result.stderr = ReadFile(tmpFileStderr, fso); fso.DeleteFile(tmpFileStderr);
      return result.stdout;
   }
///////////////////////////////////////////////////////////////////////////////
function ReadFile ( path, fso ){
	fso = fso || new ActiveXObject("Scripting.FilesystemObject");
	var content = "";
	if (!fso.FileExists(path)){
	 return content;
	}
	var file = fso.OpenTextFile( path, 1, -2); // Read, UseDefaultEncoding
	if (!file.AtEndOfStream)
	 content = file.ReadAll();
	file.Close();
	return content;
}   
///////////////////////////////////////////////////////////////////////////
function GetTmpFileName(prefix, extension, fso) {
	fso = fso || new ActiveXObject("Scripting.FilesystemObject");
	var tFolder = fso.GetSpecialFolder(2); //2 = temp folder
	var tFile = fso.GetTempName();
	if (prefix!=undefined) tFile=prefix+tFile;
	if (extension!=undefined) tFile+=extension;
	return {
		path	: tFolder.Path,
		name	: tFile,
		fullname: tFolder.Path+'\\'+tFile
	};
}


The command for "RHash" is as follows:

"D:\test\RHash-1.4.1-win64\rhash.exe" --printf="%p - %{crc32}" "D:\test\ExperienceIndexOK_x64_p.exe"

The response should come:

D:\TEST\EXPERIENCEINDEXOK_X64_P.EXE - 85BCE596

I send the following parameters to the "RunEx()" and "RunHiddenEx()" functions:
exe_path_full:

"D:\test\RHash-1.4.1-win64\rhash.exe"

and
params:

--printf="%p - %{crc32}" "D:\test\ExperienceIndexOK_x64_p.exe"

In response, I get:

HASH from RHash, RunEx:
D:\TEST\EXPERIENCEINDEXOK_X64_P.EXE - 85BCE596

HASH from RHash, RunHiddenEx:
D:\TEST\RHASH-1.4.1-WIN64\RHASH.EXE - 3F8F5524D:\TEST\EXPERIENCEINDEXOK_X64_P.EXE - 85BCE596

That is, the problem is in the "RunHiddenEx()" function, which additionally counts the hash sum for itself =) )).
Help me fix this!!!

don't work

function OnInit(initData) {
    initData.name = "Import_HASH_from_RHash_to_file";
    initData.version = "1.0";
    initData.copyright = "(c) 2021 t23111";
    initData.url = "";
    initData.desc = "Import HASH summ from RHash.exe to text file!";
    initData.default_enable = true;
    initData.min_version = "";

    var cmd = initData.AddCommand();
    cmd.name = "Import_HASH_from_RHash_to_file";
    cmd.method = "Import_HASH_from_RHash_to_file";
    //cmd.desc = "Import HASH summ from RHash.exe to text file!";
    //cmd.label = "Import_HASH_from_RHash_to_file";
    //cmd.template = "FILE,ADD/S,REMOVE/S,TOGGLE/S";
    //cmd.hide = false;
    //cmd.icon = "edit";
}

function Import_HASH_from_RHash_to_file(clickData) {
    if (clickData.func.sourcetab.selected.count == 0) {
        var dlg = DOpus.Dlg;
        dlg.message = "Please select a file!";
        dlg.Show;
        return;
    } else {
        for (var eSel = new Enumerator(clickData.func.sourcetab.selected); !eSel.atEnd(); eSel.moveNext()) {
            if (eSel.item().is_dir) {
                var dlg = DOpus.Dlg;
                dlg.message = eSel.item().RealPath + " is folder, not a file!";
                dlg.Show;
            } else {

				//New File Name
				var filepath = eSel.item().path + "\\";
				var filename = eSel.item().name;
				var filepathfull = filepath + filename;
				var filenamenew = eSel.item().name + "_RHash.txt";
				var filepathnew = filepath + filenamenew;
				var filesize = eSel.item().size;

				//Getting RHash program output
				var exe = "rhash.exe";
				var exe_path = "D:\\test\\RHash-1.4.1-win64\\";
				var exe_path_full = "\"" + exe_path + exe + "\"";
				var hash = "crc32";
					/*
					AICH - %{aich}
					BLAKE2B - %{blake2b}
					BLAKE2S - %{blake2s}
					BTIH - %{btih}
					CRC32 - %{crc32}
					CRC32C - %{crc32c}
					ED2K - %{ed2k}
					ED2K-LINK - %{ed2k-link}
					EDON-R256 - %{edon-r256}
					EDON-R512 - %{edon-r512}
					GOST12-256 - %{gost12-256}
					GOST12-512 - %{gost12-512}
					GOST94 - %{gost94}
					GOST94-CRYPTOPRO - %{gost94-cryptopro}
					HAS-160 - %{has160}
					MD4 - %{md4}
					MD5 - %{md5}
					RIPEMD-160 - %{ripemd160}
					SHA1 - %{sha1}
					SHA-224 - %{sha-224}
					SHA-256 - %{sha-256}
					SHA-384 - %{sha-384}
					SHA-512 - %{sha-512}
					SHA3-224 - %{sha3-224}
					SHA3-256 - %{sha3-256}
					SHA3-384 - %{sha3-384}
					SHA3-512 - %{sha3-512}
					SNEFRU-128 - %{snefru128}
					SNEFRU-256 - %{snefru256}
					TIGER - %{tiger}
					TTH - %{tth}
					WHIRLPOOL - %{whirlpool}
					*/
				var params = 
					" --printf=\"%p - %{"+hash+"}\\n\" " 
					//" --list-hashes "//for get list all support hash
					//" --printf=\"%{"+hash+"}\" " 
					+ "\"" + filepathfull + "\"";
//				var rhash_out = RunEx(exe_path_full, params).toUpperCase();
//				var rhash_out_2 = RunHiddenEx(exe_path_full, params).toUpperCase();
				var rhash_out_3 = CaptureCommandOutput(exe_path_full, params).toUpperCase();
                
                //File Content
                var filecontent =
                    "File Name: \n" + eSel.item().name
                     + "\n" + "File Path: \n" + filepath
                     + "\n" + "File Path Full: \n" + filepathfull
                     + "\n\n" + "exe: \n" + exe
                     + "\n" + "exe_path: \n" + exe_path
                     + "\n\n" + "exe_path_full: \n" + exe_path_full
                     + "\n" + "params: \n" + params
                     + "\n" + "RHash command: \n" + exe_path_full + params
//                     + "\n\n" + "HASH from RHash, RunEx: \n" + rhash_out
//					 + "\n\n" + "HASH from RHash, RunHiddenEx: \n" + rhash_out_2
                     + "\n\n" + "HASH from RHash, CaptureCommandOutput: \n" + rhash_out_3
                     + "\n";
                var filecontentencode = DOpus.Create.StringTools.Encode(filecontent, "utf-8 bom"); //Encode to UTF-8-BOM

                var file = DOpus.FSUtil.OpenFile(filepathnew, "wa"); //Create text File for write
                if (file.error == 0) {
                    file.Write(filecontentencode); //Write to file
                    file.Close();
                }

                var dlg = DOpus.Dlg;
                dlg.message = "File is complete!";
                dlg.Show;

            }
        }
    }
}

///////////////////////////////////////////////////////////////////////////////
function CaptureCommandOutput(cmd, helpCmd)
{
	/*
	Alternative method is less code, but you can't
	stop the command prompt from flashing on the screen this way:
		var oShell = new ActiveXObject("WScript.Shell");
		var oExec = oShell.Exec(helpCmd);
		var strStdOut = oExec.StdOut.ReadAll();
		var strStdErr = oExec.StdErr.ReadAll();
		return strStdOut;
	*/

	var fs = new ActiveXObject('Scripting.FileSystemObject');
	var tempPath = '/temp/script_redir_' + fs.GetTempName();
	tempPath = DOpus.FSUtil.Resolve(tempPath);

	var cmdLine = '@sync:' + helpCmd  + '>"' + tempPath + '"';

	cmd.SetModifier("runmode","hide");
//	DOpus.Output(cmdLine);
	cmd.RunCommand(cmdLine);
	cmd.ClearModifier("runmode");

	var helpFile = fs.OpenTextFile(tempPath, 1);
	var helpText = helpFile.ReadAll();
	helpFile.Close();

	cmdLine = 'Delete QUIET NORECYCLE FILE="' + tempPath + '"';
	cmd.RunCommand(cmdLine);

	return helpText;
}

///////////////////////////////////////////////////////////////////////////////
function RunEx(exe, params){
	params = (params?" "+params:"");
	var shell = new ActiveXObject("WScript.Shell");
	var exec = shell.Exec(exe + params);
	var stdOut = "", stdErr = "";

	while(exec.Status == 0){
		stdOut += exec.StdOut.ReadAll();
		stdErr += exec.StdErr.ReadAll();
	}

	var result = {
		returncode : exec.ExitCode,
		stdout : stdOut,
		stderr : stdErr
	}
	return result.stdout;
}

///////////////////////////////////////////////////////////////////////////////
function RunHiddenEx ( exe, params, tmpFileBase, tmpFileExt, shell, fso){ //v1.2
      if (!fso)   fso      = new ActiveXObject("Scripting.FilesystemObject");
      if (!shell) shell   = new ActiveXObject("WScript.Shell");
      if (!tmpFileBase)   tmpFileBase = "DO.Temp.";
      if (!tmpFileExt)   tmpFileExt = ".txt";
      var tmpFileStdout = GetTmpFileName(tmpFileBase, tmpFileExt, fso).fullname;
      var tmpFileStderr = tmpFileStdout+".err"+tmpFileExt;
      var cmd = '%comspec% /c ""'+exe+'" '+params+' >"'+tmpFileStdout+'" 2>"'+tmpFileStderr+'""';
      var result = {};
      result.cmd = cmd;
      if (exe.match(/^([A-z]:\\.+|\\\\(.*?)\\.+)$/)){ //test path to exe if given
         if (!fso.FileExists(exe)){
            var msg = "E   Executable not found ["+exe+"]";
            DOpus.Output(msg);
            throw msg;
         }
      }
      result.returncode = shell.Run( cmd, 0, true);
      result.stdout = ReadFile(tmpFileStdout, fso); fso.DeleteFile(tmpFileStdout);
      result.stderr = ReadFile(tmpFileStderr, fso); fso.DeleteFile(tmpFileStderr);
      return result.stdout;
   }
///////////////////////////////////////////////////////////////////////////////
function ReadFile ( path, fso ){
	fso = fso || new ActiveXObject("Scripting.FilesystemObject");
	var content = "";
	if (!fso.FileExists(path)){
	 return content;
	}
	var file = fso.OpenTextFile( path, 1, -2); // Read, UseDefaultEncoding
	if (!file.AtEndOfStream)
	 content = file.ReadAll();
	file.Close();
	return content;
}   
///////////////////////////////////////////////////////////////////////////
function GetTmpFileName(prefix, extension, fso) {
	fso = fso || new ActiveXObject("Scripting.FilesystemObject");
	var tFolder = fso.GetSpecialFolder(2); //2 = temp folder
	var tFile = fso.GetTempName();
	if (prefix!=undefined) tFile=prefix+tFile;
	if (extension!=undefined) tFile+=extension;
	return {
		path	: tFolder.Path,
		name	: tFile,
		fullname: tFolder.Path+'\\'+tFile
	};
}


write:

Error in line 147, position 2
The object does not support this property or method (0x800a01b6)

Line 147 is the Close line here:

	var helpFile = fs.OpenTextFile(tempPath, 1);
	var helpText = helpFile.ReadAll();
	helpFile.Close();

That probably means the OpenTextFile line above it failed, presumably because tempPath doesn't exist.

Have you checked that the command you are running successfully writes its output to the temp file?

I think the problem may be that you're quoting the value in the exe_path_full variable, the RunHiddenEx already does its own quoting of the exe path, so it seems like it gets misquoted or something and then you get a weird command line.

So try something like var exe_path_full = exe_path + exe; without the extra quotes.

(in any case, inspect the resultant commandline that the RunHiddenEx generates before it executes it)

bytespiller :smiley:
Thanks!!! This is helped!

function OnInit(initData) {
    initData.name = "Import_HASH_from_RHash_to_file";
    initData.version = "1.0";
    initData.copyright = "(c) 2021 t23111";
    initData.url = "";
    initData.desc = "Import HASH summ from RHash.exe to text file!";
    initData.default_enable = true;
    initData.min_version = "";

    var cmd = initData.AddCommand();
    cmd.name = "Import_HASH_from_RHash_to_file";
    cmd.method = "Import_HASH_from_RHash_to_file";
    //cmd.desc = "Import HASH summ from RHash.exe to text file!";
    //cmd.label = "Import_HASH_from_RHash_to_file";
    //cmd.template = "FILE,ADD/S,REMOVE/S,TOGGLE/S";
    //cmd.hide = false;
    //cmd.icon = "edit";
}

function Import_HASH_from_RHash_to_file(clickData) {
    if (clickData.func.sourcetab.selected.count == 0) {
        var dlg = DOpus.Dlg;
        dlg.message = "Please select a file!";
        dlg.Show;
        return;
    } else {
        for (var eSel = new Enumerator(clickData.func.sourcetab.selected); !eSel.atEnd(); eSel.moveNext()) {
            if (eSel.item().is_dir) {
                var dlg = DOpus.Dlg;
                dlg.message = eSel.item().RealPath + " is folder, not a file!";
                dlg.Show;
            } else {

				//New File Name
				var filepath = eSel.item().path + "\\";
				var filename = eSel.item().name;
				var filepathfull = filepath + filename;
				var filenamenew = eSel.item().name + "_RHash.txt";
				var filepathnew = filepath + filenamenew;

				//Getting RHash program output
				var exe = "rhash.exe";
				var exe_path = "D:\\test\\RHash-1.4.0-win64\\";
				var exe_path_full = exe_path + exe;
				var hash = "crc32";

				var params = 
					" --printf=\"%{"+hash+"}\" " 
					//" --list-hashes "//for get list all support hash
					//" --printf=\"%{"+hash+"}\" " 
					+ "\"" + filepathfull + "\"";




				//var helpCmd = '"'+exe_path_full+'"'+params;
				//var helpCmd = exe_path_full;
				var helpCmd = 'cmd /c dir /?';
				var cmd = clickData.func.command
				cmd.ClearFiles();
				var rhash_out_3 = CaptureCommandOutput(cmd, helpCmd);
//				var rhash_out_3 = CaptureCommandOutput(exe_path_full, params)//.toUpperCase();


				
				//File Content
				var filecontent =
					 "HASH from RHash, helpCmd: \n" + helpCmd
					 + "\n" + "HASH from RHash, CaptureCommandOutput: \n" + rhash_out_3
					 + "\n";
                var filecontentencode = DOpus.Create.StringTools.Encode(filecontent, "utf-8 bom"); //Encode to UTF-8-BOM

                var file = DOpus.FSUtil.OpenFile(filepathnew, "wa"); //Create text File for write
                if (file.error == 0) {
                    file.Write(filecontentencode); //Write to file
                    file.Close();
                }

                var dlg = DOpus.Dlg;
                dlg.message = "File is complete!";
                dlg.Show;

            }
        }
    }
}

///////////////////////////////////////////////////////////////////////////////
function CaptureCommandOutput(cmd, helpCmd)
{
	/*
	Alternative method is less code, but you can't
	stop the command prompt from flashing on the screen this way:
		var oShell = new ActiveXObject("WScript.Shell");
		var oExec = oShell.Exec(helpCmd);
		var strStdOut = oExec.StdOut.ReadAll();
		var strStdErr = oExec.StdErr.ReadAll();
		return strStdOut;
	*/

	var fs = new ActiveXObject('Scripting.FileSystemObject');
	var tempPath = '/temp/script_redir_' + fs.GetTempName();
	tempPath = DOpus.FSUtil.Resolve(tempPath);

	var cmdLine = '@sync:' + helpCmd  + '>"' + tempPath + '"';

	cmd.SetModifier("runmode", "hide");
//	DOpus.Output(cmdLine);
	cmd.RunCommand(cmdLine);
	cmd.ClearModifier("runmode");

	var helpFile = fs.OpenTextFile(tempPath, 1);
	var helpText = helpFile.ReadAll();
	helpFile.Close();

	cmdLine = 'Delete QUIET NORECYCLE FILE="' + tempPath + '"';
	cmd.RunCommand(cmdLine);

	return helpText;
}

The "CaptureCommandOutput(cmd, helpCmd)" command only works with the line "'cmd /c dir /?'" :grin:
How do I make it eat my commands?
For example: exe_path_full