I'm trying to automate a program I made with a test suite via a .cmd file.
I can get the program that I ran's return code via %errorlevel%.
My program has certain return codes for each type of error.
For example:
1 - means failed for such and such a reason
2 - means failed for some other reason
...
echo FAILED: Test case failed, error level: %errorlevel% >> TestSuite1Log.txt
Instead I'd like to somehow say:
echo FAILED: Test case failed, error reason: lookupError(%errorlevel%) >> TestSuite1Log.txt
Is this possible with a .bat file? Or do I have to move to a scripting language like python/perl?
You can use the 'IF ERRORLEVEL' statement to do different things based on the return code.
See:
http://www.robvanderwoude.com/errorlevel.html
In response to your second question, I would move to using a scripting language anyway, since Windows batch files are inherently so limited. There are great Windows distributions for Perl, Python, Ruby, etc., so no reason not to use them, really. I personally love doing Perl scripting on Windows.
You can do this quite neatly with the
ENABLEDELAYEDEXPANSION
option. This allows you to use!
as variable marker that is evaluated after%
.Type
HELP SETLOCAL
andHELP SET
at a command prompt for more information on delayed expansion.Yes you can use call. Just on a new line have call, and pas the errorcode. This should work, but i have not tested.
SEDIT: orry i may have misunderstood a bit, but you can use IF also
You can do something like the following code. Note that the error level comparisons should be in decreasing order due to a cmd quirk.
Not exactly like that, with a subroutine, but you can either populate the a variable with the text using a goto workaround.
It may be easier if this test suite of yours grows quite a bit to use a more powerful language. Perl or even Windows Scripting Host can help you there.
Test your values in reverse order and use the overloaded behaviour of IF:
If you want to be more powerful, you could try something like this (you'd need to replace the %1 with %errorlevel but it's harder to test for me). You would need to put a label for each error level you deal with:
Here is a test: