Wait till RunCommand finishes before moving to next line

I want to run a dos command against set of files in a folder.
The command is working, however I cant get the script to wait till the dos command is finished and closed before continuing.

I tried a few different ways. This is the simplest.

 Dim cmdCompress
 Set cmdCompress = DOpus.Create.Command()
 cmdCompress.SetSource(strFolder)
 cmdCompress.AddFilesFromFolder(strFolder)
 cmdCompress.SetType("msdos")
 cmdCompress.SetModifier("filesonly")
 cmdCompress.SetModifier("sync")
 cmdCompress.RunCommand("sync:""" + sJpegRecompressPath + """  --method smallfry ""{file}"" ""{file}""")

'Do next thing after command is finished.

How do force the script to wait till RunCommand finishes and the windows closes?

Try @sync: on the last line instead of sync:

If that doesn't work, what happens if you run the command from a Command Prompt? Does it return immediately there as well, or only once it has finished?

Hi leo thanks for the reply.
Changing to @sync: didn't help.

If I run from command prompt, it outputs while running, then return control when done.

Also I can see in the output when the above script is run, each file is processed one at a time. So the second file is processed after the first file is completed.

The exe is public if you would like try locally.

A simpler more complete example. Opening a DOS cmd, running a sleep, then showing a dopus dialog. The dialog appears before the CMD closes.
I didn't include the @sync: (though i did try it locally) before the command as I have multiple commands, I do have the sync modifier set.

<?xml version="1.0"?>
<button backcol="none" display="label" label_pos="right" textcol="none">
	<label>test dos</label>
	<icon1>#newcommand</icon1>
	<function type="script">
		<instruction>@script VBScript</instruction>
		<instruction>Option Explicit</instruction>
		<instruction>Function OnClick(ByRef clickData)</instruction>
		<instruction>	DOpus.ClearOutput</instruction>
		<instruction>	Dim cmd</instruction>
		<instruction>	Set cmd = DOpus.Create.Command()</instruction>
		<instruction>	cmd.SetType(&quot;msdos&quot;)</instruction>
		<instruction>	cmd.SetModifier(&quot;sync&quot;)</instruction>
		<instruction>	cmd.AddLine(&quot;@echo off&quot;)</instruction>
		<instruction>	cmd.AddLine(&quot;echo %time%&quot;)</instruction>
		<instruction>	cmd.AddLine(&quot;timeout 10 &gt; NUL&quot;)</instruction>
		<instruction>	cmd.AddLine(&quot;echo %time%&quot;)</instruction>
		<instruction>	cmd.Run()</instruction>
		<instruction>	</instruction>
		<instruction>	dim dlg</instruction>
		<instruction>	Set dlg = DOpus.Dlg </instruction>
		<instruction>	dlg.message = &quot;hi, done&quot;</instruction>
		<instruction>	dlg.Show</instruction>
		<instruction>End Function</instruction>
	</function>
</button>

You could use the windows shell like this

Option Explicit
Function OnClick(ByRef clickData)
	Dim wsh
	Set wsh = CreateObject("WScript.Shell")
	wsh.run "cmd.exe /c echo %time% && timeout 4 && echo %time%",1,true

	dim dlg
	Set dlg = DOpus.Dlg 
	dlg.message = "hi, done"
	dlg.Show
End Function

good idea, I will give that a go.

I was keen to use the dopus command, as it reduces the code as you can tell it to act on all files in a folder then have a generic command.
like this.

 cmdCompress.AddFilesFromFolder(strFolder)
 cmdCompress.SetType("msdos")
 cmdCompress.SetModifier("filesonly")
 cmdCompress.SetModifier("sync")
 cmdCompress.RunCommand("sync:""" + sJpegRecompressPath + """  --method smallfry ""{file}"" ""{file}""")

Saves on looping all the files, but not hard to change.
Thanks for the suggestion.

Aside, Should the previous code have worked?

I'd be surprised if an external exe could act upon the files in the Opus command object. Haven't tried it though. Is it so difficult to hand over the files to sJpegRecompressPath via command line parameter?

When you use DOpus commands like sync:"c:\myexe.exe" --method smallfry "{file}" "{file}". You are asking DOpus to generate the command for you. If you passed three files in to command object, then you see three lines executed. The external exe does not need to know about the {file} param it will never see it.

No, It's not difficult to do myself using WScript (good tip). However the DOpus command is cleaner, no need to looping the files.

My reading for the doco indicated this should work with the sync modifier. I'm keen to know if the code above should have worked? As that seems the point of the sync modifier.

At the moment DOS mode functions are always run asynchronously and never wait for completion. We'll change this in the next update; in the mean time, you can force it to wait by adding the @runbatch modifier to the end of the command, like this:

 Dim cmdCompress
 Set cmdCompress = DOpus.Create.Command()
 cmdCompress.SetSource(strFolder)
 cmdCompress.AddFilesFromFolder(strFolder)
 cmdCompress.SetType("msdos")
 cmdCompress.SetModifier("filesonly")
 cmdCompress.AddLine("""" + sJpegRecompressPath + """  --method smallfry ""{file}"" ""{file}""")
 cmdCompress.AddLine("@runbatch")
 cmdCompress.Run

'Do next thing after command is finished.
1 Like

Thanks @Jon.
Will give it a try.

I updated to12.12.4, worked well. thanks