Returning+errors+from+Windows+commands

//by Jonathan Harston, August 2010//

The OSCLI command can be used to execute a Windows command. However, if the command fails, no error is generated, the Windows command interpreter merely //displays// an error message. You may see this appearing in a DOS command box before it quickly disappears.

However, the Windows command interpreter and properly-written commands output error message to the //standard-error// output stream. This can be redirected to a file, and then any contents examined later. The following function does this.

code format="bb4w" DEF FNwin_cmd(cli$) LOCAL ch%,err$,tmp$:tmp$=@tmp$+STR$TIME OSCLI "cmd /c "+cli$+" 2>"+tmp$ ch%=OPENIN(tmp$):IF ch%=0:="" err$=GET$#ch%:CLOSE#ch%:OS."Delete "+tmp$ =err$ code

This redirects the error output to a file, then reads that file to see if anything has been output to it. Bear in mind that if the called command displays its error reports to //standard-out// instead of //standard-error//, then it will return an empty string.

Examples
code format="bb4w" >PRINT FNwin_cmd("junk") 'junk' is not recognised as an internal or external command. >PRINT FNwin_cmd("dir")

> code

Note that, as opposed to OSCLI itself, the command is //always// passed to Windows. In the second example above, the Windows **DIR** command is executed, which will result in a directory listing appearing in a DOS command box before quickly diappearing.