cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Recorder: Getting result from an executed batch cmd file

From a recorder script I want to run a batch (cmd) file. I am using Shell "<batchfile>".

I would like to record the result of the batch file, in particular the %errorlevel% that is returned, to determine the success and/or next step.

How can I accomplish this?

2 REPLIES 2

yuriy_look
Inactive

@Frans
S.

Hi Frans,

According to documentation, Shell function runs asynchronously,
and its return only reports on whether or not the new process has been spawned
successfully, but not on the results of the process execution. So, you will need to accomplish two things:

1.Waiting for the process to finish.

2.Checking the results once the process exits.

There could be different approaches to accomplishing this
task, some involve alternatives to Shell. The easiest, even though this is not an
elegant solution, you can just output errorlevel
in your cmd file, and then read the file once it exists, not locked, and not empty.

Note:There will be
very short periods when the file gets outputted when the former of these three
conditions is already true, but two others might be not yet true, so ideally you
should account for that. But,
considering that VBA is slow, by the time you come to the next line in your
code where you know that the file exists, output of the file will be finished,
even if you happen to check for existence when the file output was still in
progress.

Just in case, here are the links that you might take into
consideration:

https://www.experts-exchange.com/questions/10013781/How-do-I-get-the-errorlevel-from-a-dos-app.html

https://stackoverflow.com/questions/22498393/is-it-possible-to-return-error-code-to-vba-from-batch-f...

https://stackoverflow.com/questions/11948771/how-to-catch-windows-shell-script-errors-launched-from-...

https://stackoverflow.com/questions/334879/how-do-i-get-the-application-exit-code-from-a-windows-com...

Hope you will come up with a solution that fits your needs
the best.

Thank you,

Yuriy

Hi Yuriy, thank you very much for the explanation, and usefull pointers.
I will investigate the information, and see if I can get it to work.