How to keep cmd window open?

When I run contig.exe (http://technet.microsoft.com/en-us/sysinternals/bb897428.aspx) from the Windows command-line then the cmd window stays open at the end of the operation, showing the result.

However, when I run contig.exe with the same arguments from a DOpus button then the cmd closes after the end of the operation (although @leavedoswindowopen instruction is provided):

<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
	<label>Contig</label>
	<tip>Show defragmentation of the selected file or directory</tip>
	<icon1>#newcommand</icon1>
	<function type="batch">
		<instruction>@leavedoswindowopen </instruction>
		<instruction>@admin</instruction>
		<instruction>Contig.exe -a &quot;%1&quot;</instruction>
	</function>
</button>

So how can I keep the cmd window open?

@leavedoswindowopen doesn't seem to work with @admin but you can use the Pause command instead:

@admin Contig.exe -a "%1" pause

Thank you Leo, this works.

However, there is an other problem:

When I ran contig.exe on a directory from the Windows command-line then exactly the files in this directory (and subdirectories) are processed, for example:

contig.exe -v -s -a "D:\DELPHI\PDF"

With the above command-line all the files in D:\DELPHI\PDF are processed

But when I run contig.exe with the same parameters from a DOpus button then all the files in the PARENT directory (!) are processed:

<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
	<label>Contig</label>
	<tip>Show defragmentation</tip>
	<icon1>#newcommand</icon1>
	<function type="batch">
		<instruction>@admin</instruction>
		<instruction>contig.exe -v -s -a &quot;%1&quot;</instruction>
		<instruction>pause</instruction>
	</function>
</button>

With this button above when the directory D:\DELPHI\PDF is selected all the files in the directory D:\DELPHI are processed!!!

Put "echo " before contig.exe and see what command you are actually generating.

Ah, it's because you are using %1 in an MS-DOS type button. MS-DOS is funny about % characters.

You should use {filepath} instead. %1 is only needed for compatibility with Explorer when defining filetypes and never needs to be used in buttons like this.

(In fact, it doesn't look like %1 works at all in MS-DOS type buttons which is probably just as well as it would conflict with other uses of % in such buttons.)

contig.exe -v -s -a {filepath$}

Thank you, Leo. This works, although quotes should be added to make it work with paths containing spaces:
"{filepath}"

However, there's an other question:

contig.exe seems to process files only when the -s parameter is not used and to process SUB-directories only when the -s parameter is used.
So to be able to process BOTH directories and files with one single DOpus button I would need something like a if-then-else statement inside the button, where the IF command is executed if the selected item is a file an the ELSE command is executed when the selected item is a directory:

IF SelectedItem.IsFile then
    contig.exe -v -a "{filepath}"
ELSE IF SelectedItem.IsDirectory then
    contig.exe -v -s -a "{filepath}"

Is this possible?

In general, there's no need to put quotes around {filepath} as Opus will do so automatically if they are needed.

To run different commands on files and folders you can use something like this:

[One Hotkey for 2 commands)

The only thing you should need to change is the command lines to these:

If fs.FileExists(filepath) Then
   command = """contig.exe"" -v -a """ & filepath & """"
ElseIf fs.FolderExists(filepath) Then
   command = """contig.exe"" -v -s -a """ & filepath & """"
Else 

Thank you, Leo. The vbs script works, however it does not wait at the end of the processing. Since I'm not fluent in VBS, which command do I need to add to the script to make the output wait for a key-press?

Put a pause command at the end of the Opus button that runs the VBScript, like before.

That's what I have done. But the DOpus button and the VBS scripting are two different processes, so when the pause is output (press any key) the scripting output from contig.exe has vanished. That#s why I have asked for a pause statement inside the vbs script command.

Hmm, try running the .VBS file using cscript, like:

cscript.exe blah.vbs {filepath}
pause

It's the same: The contig output vanishes and with cscript.exe the following is displayed (before "press any key"):

Microsoft (R) Windows Script Host, Version 5.7
Copyright (C) Microsoft Corporation 1996-2001.

Yeah. :frowning: I assumed cscript at least would make it works but on further investigation it doesn't seem to be easy to make VBScript run commands within the same DOS window as themselves. :frowning:

You can try just running the pause from within the vbscript AFTER the $command:

shell.Run("pause",True)

Or maybe wait for an key to be pressed with:

sccript.StdIn.ReadLine

The problem is that VBScript doesn't pipe the program's output to the same command window. You can make it do that, but then you get the output in two windows. Seems like a bit of a missing thing in VBScript (or Windows Scripting Host, rather).

BTW, the problem in the root thread turned out to be that @leavedoswindowopen doesn't have any effect if there's a space after it. Turns out the @admin wasn't the problem after all. Should be fixed in the next update.

Thank you, Leo and Steje. I've solved the problem by writing a small utility which does the job very well. For people who would like to use it I've attached it to this posting. For using it in DOpus one could create a button which when clicked with the left mouse button defragments the selected file or the files in the selected directory, and when clicked with the right mouse button only tests the defragmentation of the selected file or the files in the selected directory:

<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none" type="three_button">
	<label>Contig</label>
	<icon1>#newcommand</icon1>
	<button backcol="none" display="both" label_pos="right" textcol="none">
		<label>Contig</label>
		<tip>Defragment</tip>
		<icon1>#newcommand</icon1>
		<function type="batch">
			<instruction>@admin</instruction>
			<instruction>Launch_Contig.exe defrag {filepath}</instruction>
			<instruction />
			<instruction> </instruction>
		</function>
	</button>
	<button backcol="none" display="both" label_pos="right" textcol="none">
		<label>Contig</label>
		<tip>Show fragmentation</tip>
		<icon1>#newcommand</icon1>
		<function type="batch">
			<instruction>@admin</instruction>
			<instruction>Launch_Contig.exe test {filepath}</instruction>
		</function>
	</button>
</button>

Launch_Contig.zip (59.2 KB)

Ah... I gotcha, yeah I don't often have to mess around with output from an external executable needing to show up in my cscript/vb console windows, but I see what you mean.

So then aside from fixes in next releases etc, an "immediate" answer is to use a combo of batch and vbs (and maybe someone more fluent in batch language possibilities has a way to work out the part that we're using VB for in this example...) i.e.:

Opus Button:

<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
	<label>Contig</label>
	<tip>Show defragmentation</tip>
	<icon1>#newcommand</icon1>
	<function type="batch">
		<instruction>@runmode hide</instruction>
		<instruction>@admin</instruction>
		<instruction>contig.vbs {filepath}</instruction>
	</function>
</button>

Change the relevant section of VB of code to (i.e run a batch file instead of the executable):

If fs.FileExists(filepath) Then 
   command = """contig.cmd"" -v -a """ & filepath & """" 
ElseIf fs.FolderExists(filepath) Then 
   command = """contig.cmd"" -v -s -a """ & filepath & """" 
Else

And finally the batch file (contig.cmd):

contig.exe %1 %2 %3 %4
pause

And I suppose that does what the OP is looking for...? The 'folder' case in the CMD file could probably be "technically" cleaner with some SHIFT crap or whatever, since it wouldn't ACTUALLY have a %4 arg... but it "works" even dirty as it is...?

Woops, was writing my reply while you were posting yours... glad you also worked out your own way to make it work.

That'll work, but if you run it on multiple selected files or folders then I think you'll get a DOS window for each selected item, instead of one DOS window showing all the results.

Whether that matters depends on how you plan to use the button, of course.

Peter, glad you worked out a solution!

FWIW, its interesting that the effect the extra space has on that particular modifier is actually caused by Opus command editor control adding a 'trailing' space to the end of every argument :smiley:. Personally, I don't see the need since if there is NO space at the end of a previous argument, Opus also adds a 'leading' space when you add the next argument... Also weird that it always insists on 'visually' adding a newline to the end of every buttons set of instructions (though its not present in the xml for the button). I guess both are an attempt to give the user the next position to continue typing 'stuff' without having to manually hit the spacebar? WOW - Jon, can you turn my coffee pot on in the morning too yet - lol?