How to prevent a CMD Window from being closed

November 9, 2009 at 12:23:10
Specs: Windows XP
I have a batch file that first scrubs some SQL tables which sometimes takes a few minutes before opening the desired Report in Excel. The problem is, Users will sometimes Close the CMD window while the Tables are being scrubbed (and before the Excel Report is opened), causing DB issues. Is there a way to prevent a CMD window from being exited (closed or ctrl-c'd) while it is still running? Thank You!

See More: How to prevent a CMD Window from being closed

Report •

November 9, 2009 at 15:39:31
May be an answer here

A possible (though not foolproof) way is to create a shortcut to the batch file, and right-click prperties. Set the run box to "minimised" the window size to 1 x 1 and reame it "Do not close" or similar.

Report •

November 10, 2009 at 04:42:49
Thanks! We already have a message in the CMD window that states something like "Do NOT Close this Window, Serious DB loss will occur!" but when you get so many users, and those who don't read what is on the screen, we are still seeing DB issues due to the DB scrubbing being cancelled in mid-process. It seems if you give them a chance to close the window, they will, especially since the DB scrubbing takes 3-4 minutes sometimes. Sigh. I may try this but still looking for a way to "lock down" that window. Thanks Again!

Report •

November 10, 2009 at 05:07:34
The only way I know to do this is through a little C/C++.
There is a problem though, there seems to be a bug in the API,
you can disable the menu item but <alt>+<space> or clicking on
the cmd icon re-enables it, so the only sure way is to delete
the menu items(which unfortunately stays deleted on that console
window until it is closed via "exit", killing with the task
manager or the batch finishing).

This will also only work for win2000 and above. It also
does nothing about ^c.

#define _WIN32_WINNT 0x0500
#include <windows.h>

int main()
    DeleteMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE , MF_GRAYED);
    return 0;

Here is a complied executable, just add "noclose"
to the top of the script(with the executable either in the
same dir or a path dir).

It's up to you to decide if it will solve your problem or not.

Another option may be to use a utility like nircmd to make the script
"hide" itself until it's finished processing.

There is really no way to do what you want without some
sort of utility(or using vbs as mentioned above)....

Batch Variable how to

Report •

Related Solutions

Ask Question