Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / tools

Program Error Reports with Visuals from .bat Files (Win7)

5.00/5 (2 votes)
12 Jun 2014CPOL1 min read 7.8K   4  
Program return values inspected by .bat files with visual feedback

image from wikipedia : Ascii Art

<-- The Command Line Is Awesome!

This tip/trick is basically a short batch script example of a friend for the programmer who has to use other peoples tools, batches, programs in order to confirm whether everything went well with the external tools. :)

This revolves basically around inspecting what a .bat file (or possibly .exe files too) returns as the value after it has been run, (i.e., whether it succeeds or not) then reporting about any possible errors or successes in a visual way, so that any errors that the user of a batch script file could normally miss, won't be missed. :)

 

Why Use This?

Because in everyday work around computers, there are multiple EXEfiles, batch files, etc. that might come in play, some regularly, some maybe not so regularly, and this is a fun trick how to inspect whether those batch files did their job properly or not. :) Mostly, this is useful for people that use some set of external batch files to do some operations, but in case you are not one of those, it can still be a fun experience to download the articles sample and try it out on your Windows machine. :)

Using the Code

C++
usage: download the sample (coolbatchaction.zip)or follow the next instructions:
1. create a do_something_cool.bat that's empty
2. create another .bat file with the below script copy pasted in, save it as whatever.bat
3. then run whatever.bat and see what happens! ...or you can just download the sample ;)

Here's the body of the whole script:

@echo off
echo ---------------------------------------------------------------
echo .
echo .
echo . DOING SOMETHING IMPORTANT
echo .
echo .
echo ---------------------------------------------------------------

CMD /c do_something_cool.bat
IF %ERRORLEVEL% == 0 (GOTO ITSALLGOOD) ELSE (GOTO GURUMEDITATION)
 
:ITSALLGOOD 
echo ---------------------------------------------------------------
echo .
echo .
echo . That important thing went great! Happy time! :)
echo .
echo .
echo ---------------------------------------------------------------
mshta "javascript:var sh=new ActiveXObject( 'WScript.Shell' ); sh.Popup( 'THE THING SUCCEEDED!', 0, 'do_something_cool.bat', 64 );close()"
exit
 
:GURUMEDITATION
echo ---------------------------------------------------------------
echo .
echo .
echo . Something went wrong bro! You gotta meditate on this!
echo .
echo . Write exit to confirm and close after you've fixed it ;)
echo .
echo .
echo ---------------------------------------------------------------
mshta "javascript:var sh=new ActiveXObject( 'WScript.Shell' ); sh.Popup( 'THE THING FAILED, PLEASE FIX!', 0, 'do_something_cool.bat', 64 );close()"
cmd /K ping 127.0.0.1 -n 1 -w 1000 > nul

Points of Interest

The mshta JavaScript command line feed opens up some interesting opportunities for various smart snippets on Windows, that could be looked into more. :) Also, the %ERRORLEVEL% is an interesting builtin.

History

  • Version 0.01 uploaded, 12th of June 2014

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)