Create string from drive letter and label

In a script function, I would like to create a string of the name of the drive and/or its label the way that the GetFocusItem can do for a file or folder listed in a Tab. I am talking about when it is selected in the This PC Tab in a lister. Thanks

When/how is the script going to run? As a button or hotkey or via events?

What will the string be used for? Does it need to be in a certain format something else will understand?

I want it via an event. It will be used for passing the path or label or to a backup script.

You can get the label from the FileSystemObject:

var fso = new ActiveXObject('Scripting.FileSystemObject');
DOpus.Output(fso.GetDrive('C:').VolumeName);

AFAIK you can't get the selected item in This PC like in other file system paths. But you can create a new entry in the drive context menu, pointing to a user command to get the drive/label and do something with that info.
e.g. for the user command:

function OnClick(clickData) {
	if (!clickData.func.args.got_arg.disk) return;
	var disk = clickData.func.args.disk;
	var label = '';
	var drives = DOpus.FSUtil.Drives;
	for (var i = 0; i < drives.length; i++) {
		if (drives(i) == disk) {
			label = drives(i).label;
			break;
		}
	}
	if (!label) return;
	DOpus.Output('disk : ' + disk);
	DOpus.Output('label : ' + label);
	//do something with that info
	return;
}

cmddisk.ouc (1.2 KB)

Then you can create a new context menu entry in FileTypes > Drives, using:

"C:\Program Files\GPSoftware\Directory Opus\dopusrt.exe" /cmd:active cmddisk disk="%1"

OK, great for that part. I need to test what happens when a drive is connected with the same label as another. My thinking was that drive letter together with the label would make it unique. Thanks so much.

Hi, thanks for the reply. I am new to adding context commands. Although another useful thing to understand that is possible in Directory Opus. Do you mean that after all that is done it would be available to a script? Or is this the the way to right click and see a context menu that runs the script?

You could use SerialNumber instead of VolumeName. That's probably as unique as it gets.

1 Like

I just tried it and it works great. Am I correct in assuming that the serial number for a hard disk is always going to be the same on any system and cannot change? Thanks

That is a user command that you have to add as a new entry in the right click menu for drives. You can modify the command to your like, as it is now it only shows the root path of the disk and its label.
To summarize:

  1. Go to Settings / Customize / User Commands and click on Import, to import this command.

  2. Go to Settings / File Types and search for Drive and click on it.

  3. Go to Context Menu and click on New, then select Run Application and paste this in Application:
    "C:\Program Files\GPSoftware\Directory Opus\dopusrt.exe" /cmd:active cmddisk disk="%1"
    Change the dir to dopusrt.exe to yours. (seems that alias doesn't work in here)
    image

  4. Now a new entry in the right click menu for every drive should appear. Clicking on it will run the user command.

You can now edit the user command to your like, just don't modify the code before the //do something with that info line.

1 Like

I'd say yes.

Returns the decimal serial number used to uniquely identify a disk volume.

SerialNumber property | Microsoft Learn

1 Like

I see... This is fantastic to understand how all this works. Now you are to blame for my mad science. My question now is once this user command is inserted is Right Clicking to the context menu the only way the command

"C:\Program Files\GPSoftware\Directory Opus\dopusrt.exe" /cmd:active cmddisk disk="%1"

can be run? As apposed to a button or inside a script.
Thanks so much much!!!

You can use that command pretty much anywhere. The command's name is cmddisk and the only arguments it has is disk, which takes as its value a drive root (c:\, d:\, etc).