talisker
8 years agoNew Contributor
Call and wait on a .bat file in python script
Hi, I am using python script to invoke a .bat file on Windows. This bat file contains a call to ssh.exe: ssh.exe user@targetserver 'some remote command' When running the batch file, I am ...
- 8 years ago
Hi,
I believe that you are looking for something like this (DelphiScript):
//----------------------------------------------------------------------------- //----------------------------------------------------------------------------- function ReadAllFromAny(oExec : OleVariant) : string; begin result := ''; if (not oExec.StdOut.AtEndOfStream) then begin result := oExec.StdOut.ReadAll; exit; end; if (not oExec.StdErr.AtEndOfStream) then begin result := 'STDERR: ' + oExec.StdErr.ReadAll; exit; end; end; //----------------------------------------------------------------------------- function ExecAndWaitForEnd(strProg, strArgs : string; var strOutput : string) : integer; const cEXEC_DELAY = 500; // time interval (in ms) when looking if spawned process have finished var WshShell : OleVariant; var oExec : OleVariant; var strCommand : string; var allInput : string; begin result := -1; strProg := aqString.Trim(strProg); If ('"' <> aqString.GetChar(strProg, 0)) then strProg := aqString.Quote(strProg); strCommand := strProg + ' ' + strArgs; Log.AppendFolder('Executing: ' + strCommand, strCommand); try WshShell := Sys.OleObject('WScript.Shell'); oExec := WshShell.Exec(strCommand); while (0 = oExec.Status) do BuiltIn.Delay(cEXEC_DELAY); allInput := ReadAllFromAny(oExec); result := oExec.ExitCode; strOutput := allInput; Log.Message(aqString.Format('Done. Exit code = %i (0x%x)', oExec.ExitCode, oExec.ExitCode), allInput); finally Log.PopLogFolder(); oExec := nil; WshShell := nil; end; end; //----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
To use Call ExecAndWaitForEnd() function. Exit code is returned as a result of function's call and the text from console output is returned in the strOutput variable passed by reference.