User Tools

Site Tools


returning_20errors_20from_20windows_20commands

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.

        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$

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

      >PRINT FNwin_cmd("junk")
      'junk' is not recognised as an internal or external command.
      >PRINT FNwin_cmd("dir")

      >

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 disappearing.

References

This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information
returning_20errors_20from_20windows_20commands.txt · Last modified: 2018/04/14 17:22 by richardrussell