Executing button code only when certain conditions are met

Is it possible to make a button, that only works, if some specified folder has items in it, and otherwise would do nothing? Something like an @ modifier. The goal is, to prevent starting an application, if there are any items in a certain folder.

The evaluator can check the current file/folder/lister, but if you want to act upon any item on your system, you will probably need to use a script that performs the checks.

Try

// https://resource.dopus.com/t/executing-button-code-only-when-certain-conditions-are-met/46959

function OnClick(clickData) {
    var cmd = clickData.func.command;
    var fsu = DOpus.FSUtil();
    var fso = new ActiveXObject('Scripting.FileSystemObject');
    cmd.deselect = false;
    
    var pathToCheck = fsu.Resolve('D:\\test');

    var folderItem = fso.GetFolder(pathToCheck);
    
    if (folderItem.Files.count == 0) {
        DOpus.Output('Sorry, nothing to do.');
    } else {
        cmd.RunCommand('vlc.exe');
    }
}
Button as XML
<?xml version="1.0"?>
<button backcol="none" display="label" hotkey_label="yes" label_pos="right" textcol="none">
	<label>46959</label>
	<tip>executing-button-code-only-when-certain-conditions-are-met</tip>
	<icon1>#script</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>// https://resource.dopus.com/t/executing-button-code-only-when-certain-conditions-are-met/46959</instruction>
		<instruction />
		<instruction>function OnClick(clickData) {</instruction>
		<instruction>    var cmd = clickData.func.command;</instruction>
		<instruction>    var fsu = DOpus.FSUtil();</instruction>
		<instruction>    var fso = new ActiveXObject(&apos;Scripting.FileSystemObject&apos;);</instruction>
		<instruction>    cmd.deselect = false;</instruction>
		<instruction>    </instruction>
		<instruction>    var pathToCheck = fsu.Resolve(&apos;D:\\test&apos;);</instruction>
		<instruction />
		<instruction>    var folderItem = fso.GetFolder(pathToCheck);</instruction>
		<instruction>    </instruction>
		<instruction>    if (folderItem.Files.count == 0) {</instruction>
		<instruction>        DOpus.Output(&apos;Sorry, nothing to do.&apos;);</instruction>
		<instruction>    } else {</instruction>
		<instruction>        cmd.RunCommand(&apos;vlc.exe&apos;);</instruction>
		<instruction>    }</instruction>
		<instruction>}</instruction>
	</function>
</button>

Evaluator Exists() function can use a wildcard at the end of the path, which should let you tell if a folder is empty or not.

The @ifexists: modifier can as well, for that matter.

1 Like

Oops. I tried this first but somehow didn't get the syntax right. Don't tell anybody :wink:

1 Like

Thanks. Looks like it wouldn't work out. I didn't try, because i would have to combine your script with other code ("Go N:\path1 openinleft, Go N:\path2 openinright, then your code), which is not possible.

Background is, that there are bad programmers out there, who don't understand the concept of 'portable installation', even though they claim to provide it. I'm talking about a browser, that leaves tons of sensitive information on my unencrypted C:\ drive, even though they claim it to be 'portable'.

Therefore i am moving that folder to a Veracrypt container, every time i finished the browsing session. But in case i fire up that browser without having moved it back, i get unwanted results. So, if that moved folder is present on my container drive, the button should prevent the browser from launching, and otherwise start the app.