9

Every batch file I write opens a Cmd window and leaves it open until the program is completed. What is the command string to include in the .bat file to either not open the CMD window or open it and immediately hide it? I must stay within the confines of MSW7 Pro's built in programming. Right now, I'm just playing with the msg command to get this figured out. For success, only the message window itself should appear on screen.

Gray Death
  • 93
  • 1
  • 1
  • 4
  • 1
    You cannot hide the cmd window with any batch file command. You can launch the batch file from a vbscript and have it run as a background process which hides the cmd window. – Squashman Nov 13 '15 at 03:36
  • 2
    You could put `powershell -window hidden -command ""` in your script. Just be sure to `powershell -window normal -command ""` at the end if your script could conceivably be run from a cmd console (as opposed to being double-clicked). The window will still appear for a second or so while the PowerShell interpreter is invoked; possibly several seconds if this script's execution is the first invocation of PowerShell since the previous boot. The only other alternative of which I'm aware is as Squashman suggests, using VBScript. – rojo Nov 13 '15 at 04:46
  • @rojo I wish you had made that an answer. It's the only thing I've found so far that even comes close to working to hide a cmd window for my use case (passing an executable location and a list of arguments as two separate arguments to another program's internal implementation that calls "the dos run command" and adds its own arguments wherever it feels like in the middle of that). I'll probably end up compiling a custom run-silently exe eventually, but for the testing phase of the project... – coppereyecat Jun 17 '21 at 15:35

3 Answers3

8

You can do it by creating a vb script.

hideCMD.vbs

CreateObject("Wscript.Shell").Run "foo.bat", 0, True

This will run your batch file with command prompt window hidden.

secretgenes
  • 1,291
  • 1
  • 19
  • 39
  • Since it is hidden, how would you end this process after creation? @secretgenes – Trevor Tracy Jun 27 '18 at 17:55
  • If you have space in your bat file name or path, e.g. "foo with space.bat", then do this CreateObject("Wscript.Shell").Run """foo with space.bat""", 0, True – bobt Oct 13 '22 at 17:53
4

If you are starting a batch file then use:

cmd /c "Your Command and Parameters"

If you want the batch file to close the cmd window, then put exit at the end of the batch file

@echo My Batch File
exit

As a further though, the start command might help you. It can be set to minimize the window.

START /MIN MyBatch.Bat
miltonb
  • 6,905
  • 8
  • 45
  • 55
  • 1
    The idea is to make this script '@echo off msg * Hello? exit' pop up a message without showing a Cmd window at all when run directly by double click or as a scheduled task. When I tried it with the 'exit' command at the end, it still opened the cmd window and kept it open till I closed it. – Gray Death Nov 13 '15 at 03:26
  • 1
    Its recommended to include the code for the problem you want help with as its helps other help you. https://stackoverflow.com/help/mcve. You can just edit your question and add the above. – miltonb Nov 13 '15 at 03:28
1

What worked for me was to use start from within the batch file.

start does not wait for GUI apps to exit and so for batch files that are launching GUI applications, this has the effect of the batch file never being visible.

See the below from the docs:

If you run a 32-bit graphical user interface (GUI) application, cmd does not wait for the application to quit before returning to the command prompt. This behavior does not occur if you run the application from a command script.

For example to launch Task Manager from a batch file and not show the command window:

start taskmgr.exe

This immediately exits, Task Manager appears and no cmd window is left open.

For more information: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/start

mpj
  • 33
  • 6