in reply to While doing force quiting the exe how to run a bat file
When a program exits, a return code is made available to the Windows shell. The normal code for "everything worked fine" is "0", zero. Non-zero codes are typically used for error conditions.
My thinking would be that since you want to run a Windows .bat file if this .exe is aborted by CTL-C, i.e. QUIT, then I would start this .exe from a Windows .bat file and check the return value to the shell and then run your "error .bat file" from this "starter" .bat file!
Below I wrote a very simple C program and show a simple .bat file to illustrate how to do this. The C program just echos input lines until you enter "quit". If you enter "quit" the program returns "0" to the shell. However, if you "abort", i.e. force the program to "quit" with CTL-C, it will return a code of "512" to the shell.
So basically, I can tell if the program stopped because of user entering "quit" versus program stopped because of CTL-C.
Type "help start" at your Windows command line for more options on the START command. The /WAIT option is important here! Also I just printed the ERRORLEVEL, I don't know exactly what you have in mind for your "fix-it" .bat file. You will need an IF statement on %ERRORLEVEL%.
Oh another point, ERRORLEVEL is NOT an environment variable! ERRORLEVEL is not %ERRORLEVEL%*** test.bat *** START /wait echoline ECHO %errorlevel% ****echoline.C **** #include <stdio.h> int main(void) /*simple test program: echoline.C */ { char line[256]; while (printf("%s","Enter Line:"),gets (line) && strcmp(line,"quit") != 0) { printf ("%s\n",line); } }
|
|---|