User Tools

Site Tools


returning_20errors_20from_20windows_20commands

This is an old revision of the document!




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. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
returning_20errors_20from_20windows_20commands.1523726530.txt.gz · Last modified: 2018/04/14 18:22 by richardrussell