How do I make this run the command only once?

I have a JSCRIPT function I'm trying to write and I noticed that it's running the command several times (i.e. it's running 7z.exe 5 times if I have 5 folders selected when running the function).

I would prefer to run the 7z.exe once, but pass all of the files to it in a single run command.

Can somebody help me figure out what I'm doing wrong?

Thanks for any help! :slight_smile:

JSCRIPT

function OnClick(clickData)
{
    //no = No
    //ye = Echo Command (Do not Run) and leave window open
    //yr = Run command and leave window open
    useDebugMode = "ye"

	cmd = clickData.func.command;
	tab = clickData.func.sourcetab;
	selFiles = new Enumerator(tab.selected);
	firstSelFile = selFiles.item()

	if (firstSelFile != null) {

		cmd.setType("msdos")
		
        if (useDebugMode == 'no') {
            cmd.setModifier('runmode', 'min');
        }

		//cmd.setModifier('norunbatch');
		cmd.setModifier('noprogress');
		
        if ( useDebugMode.match(/^(ye|yr)$/) ) {
            cmd.setModifier('leavedoswindowopen');
        }
        

		//Create Dialog and setup default values
		Dlg = DOpus.Dlg;
	    Dlg.window = clickData.func.sourcetab;
	    Dlg.template = "Main"
	
		Dlg.Create(); // Dialog is not visible yet.
		Dlg.Control('compression-format-combobox').value = '0';
		Dlg.Control('compression-level-combobox').value = '5';
		Dlg.Control('compression-method-combobox').value = 'LZMA2';
		Dlg.RunDlg(); // Dialog becomes visible. Won't return until dialog is closed




		
		
		
		
		if (Dlg.result == 0) {
			//DOpus.Output('User Cancelled');
		}
		else {

			compressFormat = Dlg.Control('compression-format-combobox').value.name;
			
			
	
            zipExe = '"C:\\Program Files\\7-Zip\\7z.exe"';

			cmdToRun = '';

            archiveType = Dlg.Control('compression-format-combobox').value.name;
            delFiles = Dlg.Control('deleteinputfiles-checkbox').value;
            compressLevel = Dlg.Control('compression-level-combobox').value;

            cmdToRun = zipExe + ' a -t' + archiveType + (delFiles? " -sdel" : "") + ' -mx' + compressLevel + ' "{file$|noext}" "{filepath$|noterm}" ';
			
			if ( useDebugMode == "ye" ) {
                cmd.RunCommand('echo ' + cmdToRun);
            }
            else {
                cmd.RunCommand(cmdToRun);
            }
			
			if (Dlg.Control('playfinishsound-checkbox').value == 1) {
				sounder = '"X:\\Program Files\\Sounder\\sounder.exe"';
				wavToPlay = '"X:\\Program Files\\Sounder\\confirm_beepy_02.wav"';
				cmd.RunCommand(sounder + ' ' + wavToPlay);
			}
			
			//cmd.RunCommand('Go REFRESH');
		}
		
	}
	else {
		DOpus.Output('No files are selected');
	}
}

DIALOG

<resources>
	<resource name="Main" type="dialog">
		<dialog fontsize="8" height="198" lang="english" standard_buttons="ok,cancel" title="Add to Archive" width="282">
			<control halign="right" height="8" name="distance-statictext" title="Level" type="static" valign="top" width="62" x="12" y="43" />
			<control halign="right" height="8" name="speed-statictext" title="Method" type="static" valign="top" width="48" x="26" y="62" />
			<control height="60" name="compression-settings-group" title="Compresson Settings" type="group" width="270" x="6" y="30" />
			<control height="36" name="group2" title="Do Afterwards" type="group" width="114" x="162" y="138" />
			<control halign="left" height="8" name="compression-format-statictext" title="Compression Format" type="static" valign="top" width="78" x="12" y="12" />
			<control height="40" name="compression-format-combobox" type="combo" width="64" x="91" y="9">
				<contents>
					<item text="7z" />
					<item text="gzip" />
					<item text="zip" />
					<item text="bzip2" />
					<item text="tar" />
					<item text="iso" />
					<item text="udf" />
				</contents>
			</control>
			<control height="10" name="deleteinputfiles-checkbox" resize="y" title="Delete Input Files" type="check" width="78" x="168" y="150" />
			<control checked="yes" height="10" name="playfinishsound-checkbox" resize="xy" title="Play Finish Sound" type="check" width="78" x="168" y="162" />
			<control height="10" name="solid-mode-checkbox" resize="y" title="Solid Mode" type="check" width="78" x="12" y="78" />
			<control height="10" name="use-password-checkbox" resize="y" title="Use Password" type="check" width="78" x="12" y="96" />
			<control halign="left" height="12" name="password-editbox" password="yes" tip="Password" type="edit" width="120" x="12" y="114" />
			<control halign="left" height="12" name="confirm-password-editbox" password="yes" tip="Confirm Password" type="edit" width="120" x="150" y="114" />
			<control checked="yes" height="10" name="encrypt-filenames-checkbox" resize="y" title="Encrypt Filenames" type="check" width="78" x="168" y="42" />
			<control height="40" name="compression-method-combobox" type="combo" width="64" x="78" y="60">
				<contents>
					<item text="LZMA" />
					<item text="LZMA2" />
					<item text="PPMD" />
					<item text="BZip2" />
				</contents>
			</control>
			<control checked="yes" height="10" name="use-multiple-threads-checkbox" resize="y" title="Use Multiple Threads" type="check" width="90" x="168" y="54" />
			<control checked="yes" height="10" name="store-full-file-timestamps-checkbox" resize="y" title="Store full file timestamps" type="check" width="102" x="168" y="66" />
			<control edit="yes" height="40" name="compression-level-combobox" type="combo" width="64" x="78" y="42">
				<contents>
					<item text="0" />
					<item text="1" />
					<item text="2" />
					<item text="3" />
					<item text="4" />
					<item text="5" />
					<item text="6" />
					<item text="7" />
					<item text="8" />
					<item text="9" />
				</contents>
			</control>
			<control height="10" name="create-multiple-archives-checkbox" resize="y" title="Create Multiple Archives" type="check" width="102" x="168" y="78" />
			<control height="36" name="use-password-group" title="                              " type="group" width="270" x="6" y="96" />
		</dialog>
	</resource>
</resources>

Codes that insert a single filename/path like {file} will cause the command to be run once for each item.

There are other codes for inserting all names/paths at once, but since youโ€™re using scripting you can also loop through the files and insert exactly what you need into a single line, which may be easier if you (and the program being run) need something complex.

I think I figured out how to do it. :slight_smile:

If you checkmark "Create Multiple Archives", then the function will enumerate through the list of selected folders and run 7z.exe for each folder.

However, if you leave that unchecked, then the function will enumerate through the selected folders and create a single string containing all of the filenames.. and then it will run 7z.exe only once.

I believe this is working correctly now. :slight_smile:

EDIT: If I recall, I think 7zip has a built in command that will let you create multiple archives. So, I should rework this so that it still only runs 7z.exe once, but with the option to create multiple archives enabled.

EDIT2: ACTUALLY, I'm not even sure about that. I think I'm completely wrong about 7z supporting that feature.

JSCRIPT

function OnClick(clickData)
{
    //no = No
    //ye = Echo Command (Do not Run) and leave window open
    //yr = Run command and leave window open
    useDebugMode = "no"

	cmd = clickData.func.command;
	tab = clickData.func.sourcetab;
	selFiles = new Enumerator(tab.selected);
    selFilesStr = '';

    

	firstSelFile = selFiles.item()

	

	// clickData.func.command.ClearFiles();
    // cmd.clickData.func.command.AddFile(firstSelFile);
	
	if (firstSelFile != null) {
		
		cmd.setType("msdos")
		
        if (useDebugMode == 'no') {
            cmd.setModifier('runmode', 'min');
        }

		//cmd.setModifier('norunbatch');
		cmd.setModifier('noprogress');
		
        if ( useDebugMode.match(/^(ye|yr)$/) ) {
            cmd.setModifier('leavedoswindowopen');
        }
        

		//Create Dialog and setup default values
		Dlg = DOpus.Dlg;
	    Dlg.window = clickData.func.sourcetab;
	    Dlg.template = "Main"
	
		Dlg.Create(); // Dialog is not visible yet.
		Dlg.Control('compression-format-combobox').value = '0';
		Dlg.Control('compression-level-combobox').value = '5';
		Dlg.Control('compression-method-combobox').value = 'LZMA2';
		Dlg.RunDlg(); // Dialog becomes visible. Won't return until dialog is closed




		
		
		
		
		if (Dlg.result == 0) {
			//DOpus.Output('User Cancelled');
		}
		else {
			
			compressFormat = Dlg.Control('compression-format-combobox').value.name;
			
			
	
            zipExe = '"C:\\Program Files\\7-Zip\\7z.exe"';

			cmdToRun = '';

            archiveType = Dlg.Control('compression-format-combobox').value.name;
            delFiles = Dlg.Control('deleteinputfiles-checkbox').value;
            compressLevel = Dlg.Control('compression-level-combobox').value;

            if (Dlg.Control('create-multiple-archives-checkbox').value == 1) {
                for (selFiles;
                    !selFiles.atEnd();
                    selFiles.moveNext() )
                {
                    cmdToRun = zipExe + ' a -t' + archiveType + (delFiles? " -sdel" : "") + ' -mx' + compressLevel + ' "' + selFiles.item() + '" "' + selFiles.item() + '"';

                    if ( useDebugMode == "ye" ) {
                        cmd.RunCommand('echo ' + cmdToRun);
                    }
                    else {
                        cmd.RunCommand(cmdToRun);
                    }
                }
                
            }
            else {
                for (selFiles;
                    !selFiles.atEnd();
                    selFiles.moveNext() )
                {
                    selFilesStr += ' "';
                    selFilesStr += selFiles.item();
                    selFilesStr += '"';
                }

                cmdToRun = zipExe + ' a -t' + archiveType + (delFiles? " -sdel" : "") + ' -mx' + compressLevel + ' "' + firstSelFile + '"' + selFilesStr;

                if ( useDebugMode == "ye" ) {
                    cmd.RunCommand('echo ' + cmdToRun);
                }
                else {
                    cmd.RunCommand(cmdToRun);
                }
            }
            
            
			
			
			
			if (Dlg.Control('playfinishsound-checkbox').value == 1) {
				sounder = '"X:\\Program Files\\Sounder\\sounder.exe"';
				wavToPlay = '"X:\\Program Files\\Sounder\\confirm_beepy_02.wav"';
				cmd.RunCommand(sounder + ' ' + wavToPlay);
			}
			
			//cmd.RunCommand('Go REFRESH');
		}
		
	}
	else {
		DOpus.Output('No files are selected');
	}
}

DIALOG

<resources>
	<resource name="Main" type="dialog">
		<dialog fontsize="8" height="198" lang="english" standard_buttons="ok,cancel" title="Add to Archive" width="282">
			<control halign="right" height="8" name="distance-statictext" title="Level" type="static" valign="top" width="62" x="12" y="43" />
			<control halign="right" height="8" name="speed-statictext" title="Method" type="static" valign="top" width="48" x="26" y="62" />
			<control height="60" name="compression-settings-group" title="Compresson Settings" type="group" width="270" x="6" y="30" />
			<control height="36" name="group2" title="Do Afterwards" type="group" width="114" x="162" y="138" />
			<control halign="left" height="8" name="compression-format-statictext" title="Compression Format" type="static" valign="top" width="78" x="12" y="12" />
			<control height="40" name="compression-format-combobox" type="combo" width="64" x="91" y="9">
				<contents>
					<item text="7z" />
					<item text="gzip" />
					<item text="zip" />
					<item text="bzip2" />
					<item text="tar" />
					<item text="iso" />
					<item text="udf" />
				</contents>
			</control>
			<control height="10" name="deleteinputfiles-checkbox" resize="y" title="Delete Input Files" type="check" width="78" x="168" y="150" />
			<control checked="yes" height="10" name="playfinishsound-checkbox" resize="xy" title="Play Finish Sound" type="check" width="78" x="168" y="162" />
			<control height="10" name="solid-mode-checkbox" resize="y" title="Solid Mode" type="check" width="78" x="12" y="78" />
			<control height="10" name="use-password-checkbox" resize="y" title="Use Password" type="check" width="78" x="12" y="96" />
			<control halign="left" height="12" name="password-editbox" password="yes" tip="Password" type="edit" width="120" x="12" y="114" />
			<control halign="left" height="12" name="confirm-password-editbox" password="yes" tip="Confirm Password" type="edit" width="120" x="150" y="114" />
			<control checked="yes" height="10" name="encrypt-filenames-checkbox" resize="y" title="Encrypt Filenames" type="check" width="78" x="168" y="42" />
			<control height="40" name="compression-method-combobox" type="combo" width="64" x="78" y="60">
				<contents>
					<item text="LZMA" />
					<item text="LZMA2" />
					<item text="PPMD" />
					<item text="BZip2" />
				</contents>
			</control>
			<control checked="yes" height="10" name="use-multiple-threads-checkbox" resize="y" title="Use Multiple Threads" type="check" width="90" x="168" y="54" />
			<control checked="yes" height="10" name="store-full-file-timestamps-checkbox" resize="y" title="Store full file timestamps" type="check" width="102" x="168" y="66" />
			<control edit="yes" height="40" name="compression-level-combobox" type="combo" width="64" x="78" y="42">
				<contents>
					<item text="0" />
					<item text="1" />
					<item text="2" />
					<item text="3" />
					<item text="4" />
					<item text="5" />
					<item text="6" />
					<item text="7" />
					<item text="8" />
					<item text="9" />
				</contents>
			</control>
			<control height="10" name="create-multiple-archives-checkbox" resize="y" title="Create Multiple Archives" type="check" width="102" x="168" y="78" />
			<control height="36" name="use-password-group" title="                              " type="group" width="270" x="6" y="96" />
		</dialog>
	</resource>
</resources>