Help me write a script to save hash amounts to a file at the click of a button

Direct me where to dig!

The algorithm is as follows

  1. I select a file.

  2. In the menu, click the button on the command bar.

  3. Next to the file or folder, a text file with UTF-8-BOM encoding is created with the name
    Name with the extension + _hash +. txt
    For example, for the file
    "directory_opus.exe"
    a file will be created
    "directory_opus.exe_HASHs.txt"

  4. The file with hash sums must contain the text: File
    name with the extension
    md5: xxxxxxxxxxxxxx
    sha-1: xxxxxxxxxxxxxxx
    sha-256: xxxxxxxxxxxxxxxx

The introduction to scripting, and the page listing the scripting objects Opus provides, can both be found in the Opus Manual menu at the top of the forum.

If you need help working out how to use something, please ask and we should be able to assist. It's hard to know where to point you without knowing where you are stuck.

Grab the ingredients from these two threads, mix, shake, and strain them a bit, and you are almost done :slight_smile:

1 Like

Message:
Help me upgrade my Java script.

function OnClick(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 filenamenew = eSel.item().name + "_HASHs.txt";
				var filepathnew = filepath + filenamenew;
				
				//File Data Modified
				var dateNow = DOpus.Create.Date();
				
				//File HASH summ
			    var md5 = DOpus.FSUtil.Hash(eSel.item().RealPath,"md5");
				var sha1 = DOpus.FSUtil.Hash(eSel.item().RealPath,"sha1");
				var sha256 = DOpus.FSUtil.Hash(eSel.item().RealPath,"sha256");//sha256 incorrect for large files (> 1 GB :(( )!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

				var fso = new ActiveXObject("Scripting.FileSystemObject");
				var textFile = fso.CreateTextFile(filepathnew, true);
				textFile.WriteLine(
					"File Name: " + eSel.item().name
					 + "\n" + "File Size: " + eSel.item().size.fmt + " (" + eSel.item().size + " bytes)"
					 + "\n" + "File Date Modified: " + dateNow.Format("D#yyyy.MM.dd, T#HH:mm:ss")
					 + "\n" + "MD5: " + md5
					 + "\n" + "SHA-1: " + sha1
					 + "\n" + "SHA-256: " + sha256
				);
				textFile.Close();
				
				var dlg= DOpus.Dlg;
			    dlg.message="File is complete!";
				dlg.Show;
				
			}
		}
	}
}

for the file " experienceindexok_x64_p.exe "
creates a text file
with ANSI encoding
named "ExperienceIndexOK_x64_p.exe_HASHs.txt"
with the following content:

File Name: ExperienceIndexOK_x64_p.exe
File Size: 132 Kb (135560 bytes)
File Date Modified: 2021.01.04, 15:35:35
MD5: b19934746b960dfa7e4096ae25df8229
SHA-1: a1a8c4dc6156fd47babae83311aaef58b7ce3542
SHA-256: c6ae6d4f2725eb11a9fc929ff1c5cacf50e9040b6ee2dbb2384072a41f2db4e8

QUESTIONS!

  1. How to create a file with UTF-8 encoding without BOM?
  2. How to divide bytes by three digits (for example: 13556015318 -> 13 556 015 318)?
  3. How to read the "Creation Date" and "Modification Date" from the file?
  4. How to write hash sums in large letters (b19934746b960dfa7e4096ae25df8229 -> B19934746B960DFA7E4096AE25DF8229)?

Use Opus's StringTools to encode the text as UTF-8 with a BOM, which gives you a Blob which you can write to disk with File.

Opus's Item object can give you that. (Windows also provides its own scripting objects which can do that.)

These aren't really Opus questions and we aren't a forum for teaching JScript/Javascript. If you google for "javascript uppercase" you'll find lots of results pointing to the string.toUpperCase() method.

The current script looks like this:

function OnInit(initData)
{
	initData.name = "save_HASHs_to_file";
	initData.version = "1.0";
	initData.copyright = "(c) 2021 t23111";
	initData.url = "";
	initData.desc = "Save file HASH summ to text file.";
	initData.default_enable = true;
	initData.min_version = "";

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

function save_HASHs_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 filesize = eSel.item().size;
				var filenamenew = eSel.item().name + "_HASHs.txt";
				var filepathnew = filepath + filenamenew;
				
				//File Data Modify, Create and Access
				var date_create = DOpus.Create.Date(eSel.item().create);
				var date_create_utc = DOpus.Create.Date(eSel.item().create_utc);
				var date_modify = DOpus.Create.Date(eSel.item().modify);
				var date_modify_utc = DOpus.Create.Date(eSel.item().modify_utc);
				var date_access = DOpus.Create.Date(eSel.item().access);
				var date_access_utc = DOpus.Create.Date(eSel.item().access_utc);
				
				//File HASH summ
				var crc32 = DOpus.FSUtil.Hash(eSel.item().RealPath,"crc32").toUpperCase();
				var crc32_php = DOpus.FSUtil.Hash(eSel.item().RealPath,"crc32_php").toUpperCase();
				var crc32_php_rev = DOpus.FSUtil.Hash(eSel.item().RealPath,"crc32_php_rev").toUpperCase();
				var md5 = DOpus.FSUtil.Hash(eSel.item().RealPath,"md5").toUpperCase();
				var sha1 = DOpus.FSUtil.Hash(eSel.item().RealPath,"sha1").toUpperCase();
				var sha256 = DOpus.FSUtil.Hash(eSel.item().RealPath,"sha256").toUpperCase();//sha256 incorrect for large files >= 512 Mb :((
				var sha512 = DOpus.FSUtil.Hash(eSel.item().RealPath,"sha512").toUpperCase();//sha256 incorrect for large files >= 512 Mb :((
				
				//File Content
				var filecontent = 
					"File Name: " + eSel.item().name
					 + "\n" + "File Size: " + filesize.fmt + " (" + number_on_three(filesize) + " bytes)"
					 + "\n\n" + "File Date Create: \t\t" + date_create.Format("D#yyyy.MM.dd, T#HH:mm:ss")
					 + "\n" + "File Date Create UTC: \t" + date_create_utc.Format("D#yyyy.MM.dd, T#HH:mm:ss")
					 + "\n" + "File Date Modify: \t\t" + date_modify.Format("D#yyyy.MM.dd, T#HH:mm:ss")
					 + "\n" + "File Date Modify UTC: \t" + date_modify_utc.Format("D#yyyy.MM.dd, T#HH:mm:ss")
					 + "\n" + "File Date Access: \t\t" + date_access.Format("D#yyyy.MM.dd, T#HH:mm:ss")
					 + "\n" + "File Date Access UTC: \t" + date_access_utc.Format("D#yyyy.MM.dd, T#HH:mm:ss")
					 + "\n\n" + "CRC32 (7-Zip; PKZip; CRC32b in PHP): \n" + crc32
					 + "\n" + "CRC32_PHP (BZIP2; CRC32 in PHP): \n" + crc32_php
					 + "\n" + "CRC32_PHP_REV (CRC32 in PHP with byte-order reversed): \n" + crc32_php_rev
					 + "\n" + "SHA-1: \n" + sha1
					 + "\n" + "SHA-256: \n" + sha256
					 + "\n" + "SHA-512: \n" + sha512;
				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 number_on_three(x) {
	return String(x).replace(/\B(?=(\d{3})+(?!\d))/g, " ");
}


for the file "experienceindexok_x64_p.exe"
creates a text file
UTF-8-BOM encoded
with the name "ExperienceIndexOK_x64_p.exe_HASHs.txt"
with the following content:

File Name: ExperienceIndexOK_x64_p.exe
File Size: 132 Kb (135 560 bytes)

File Date Create: 2021.01.03, 16:09:23
File Date Create UTC: 2021.01.03, 13:09:23
File Date Modify: 2021.01.04, 20:22:59
File Date Modify UTC: 2021.01.04, 17:22:59
File Date Access: 2021.01.03, 16:09:23
File Date Access UTC: 2021.01.03, 13:09:23

CRC32 (7-Zip; PKZip; CRC32b in PHP):
85BCE596
CRC32_PHP (BZIP2; CRC32 in PHP):
5CD90305
CRC32_PHP_REV (CRC32 in PHP with byte-order reversed):
0503D95C
SHA-1:
A1A8C4DC6156FD47BABAE83311AAEF58B7CE3542
SHA-256:
C6AE6D4F2725EB11A9FC929FF1C5CACF50E9040B6EE2DBB2384072A41F2DB4E8
SHA-512:
8AD7D88B92612CD5EF1D09D751C941233EA55987227B92DDA795DDDF49596C7C6F619FA719D30148405103C76A6105531B8611706B5294353A344C5783184F87

QUESTION!!!
How do I make the calculation progress dialog graphical? (For example, in the form of a green bar.)

You won't be able to show a progress bar for each individual file (since your script will be blocking waiting for the Hash call to return a result). But you can show a progress dialog which reports the current file and shows a progress bar of how many files are done vs the total number of files.

https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Scripting/Progress.htm

These questions should really go into separate threads: