68

This is a batch file in Windows.

Here is my .bat file

@echo off
copy "C:\Remoting.config-Training" "C:\Remoting.config"

"C:\ThirdParty.exe"

This works fine except the .bat file leaves the command window open the whole time the "ThirdParty" application is running.
I need the command window to close.

I would use the short-cut for the application but I must be able to run this copy command first (it actually changes which data base and server to use for the application).

The ThirdParty application does not allow the user to change the source of the db or the application server.

We're doing this to allow users to change from a test environment to the production environment.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
JeffO
  • 7,957
  • 3
  • 44
  • 53
  • 1
    Just found this and I think, it's really handy to hide a console window on call: https://www.raymond.cc/blog/hidden-start-runs-batch-files-silently-without-flickering-console/ – Cadoiz Mar 20 '20 at 09:15

18 Answers18

52

Using start works for me:

@echo off
copy "C:\Remoting.config-Training" "C:\Remoting.config"
start C:\ThirdParty.exe

EDIT: Ok, looking more closely, start seems to interpret the first parameter as the new window title if quoted. So, if you need to quote the path to your ThirdParty.exe you must supply a title string as well.

Examples:

:: Title not needed:
start C:\ThirdParty.exe

:: Title needed
start "Third Party App" "C:\Program Files\Vendor\ThirdParty.exe"
jamesdlin
  • 81,374
  • 13
  • 159
  • 204
cg.
  • 3,648
  • 2
  • 26
  • 30
  • Now the Thirdparty app doesn't start and my command window stays open. – JeffO Feb 03 '09 at 15:18
  • Yes, you're right. When I used this I did not quote the path to my application. Please have a look at my revised answer. – cg. Feb 03 '09 at 15:43
  • 1
    Thanks to Patrick Cuff for improving my answer. I'm still new to SO and did not realize until now that enough reputation would allow you to edit other people's posts... – cg. Feb 03 '09 at 18:05
32

Create a .vbs file with this code:

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

This .vbs will run your_batch.bat hidden.

Works fine for me.

phuclv
  • 37,963
  • 15
  • 156
  • 475
jlenfers
  • 489
  • 5
  • 7
  • 1
    For me it needed to start with `WScript.CreateObject`, and if you want to pass an argument through use: `WScript.CreateObject("Wscript.Shell").Run "your_batch.bat """ & WScript.Arguments.Item(0) & """",0,True – Redzarf Aug 15 '19 at 00:54
11

Using start works fine, unless you are using a scripting language. Fortunately, there's a way out for Python - just use pythonw.exe instead of python.exe:

:: Title not needed:
start pythonw.exe application.py

In case you need quotes, do this:

:: Title needed
start "Great Python App" pythonw.exe "C:\Program Files\Vendor\App\application.py"
mzuther
  • 1,158
  • 1
  • 13
  • 24
10

Try this:

@echo off 
copy "C:\Remoting.config-Training" "C:\Remoting.config"
start C:\ThirdParty.exe
exit
Patrick Cuff
  • 28,540
  • 12
  • 67
  • 94
QAZ
  • 4,870
  • 6
  • 36
  • 50
9

Great tip. It works with batch files that are running a java program also.

start javaw -classpath "%CP%" main.Main
sachleen
  • 30,730
  • 8
  • 78
  • 73
Josy
  • 99
  • 1
  • 1
  • This worked excellent for me however all I had to do was save `start javaw mainClass` into my `.bat` file and it was great! – rfoo Sep 22 '13 at 16:09
5

You might be interested in trying my silentbatch program, which will run a .bat/.cmd script, suppress creation of the Command Prompt window entirely (so you won't see it appear and then disappear), and optionally log the output to a specified file.

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • Implemented similar solution. Looks like this question is good place to post it as additional answer. Cheers – Alex May 13 '19 at 11:57
4

Or you can use:

Start /d "the directory of the executable" /b "the name of the executable" "parameters of the executable" %1

If %1 is a file then it is passed to your executable. For example in notepad.exe foo.txt %1 is "foo.txt".

The /b parameter of the start command does this:

Starts an application without opening a new Command Prompt window. CTRL+C handling is ignored unless the application enables CTRL+C processing. Use CTRL+BREAK to interrupt the application.

Which is exactly what we want.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Egoadsr
  • 41
  • 3
3

You can create a VBS script that will force the window to be hidden.

Set WshShell = WScript.CreateObject("WScript.Shell")
obj = WshShell.Run("""C:\Program Files (x86)\McKesson\HRS
Distributed\SwE.bat""", 0)
set WshShell = Nothing

Then, rather than executing the batch file, execute the script.

phuclv
  • 37,963
  • 15
  • 156
  • 475
3

I haven't really found a good way to do that natively, so I just use a utility called hstart which does it for me. If there's a neater way to do it, that would be nice.

Oskar Duveborn
  • 2,189
  • 16
  • 20
  • HSTART is an excellent utility for replacement of the CMD window, plus it's meant to work with PowerShell and Ruby – SteveC Mar 31 '11 at 08:17
3

Using Windows API we can start new process, a console application, and hide its "black" window. This can be done at process creation and avoid showing "black" window at all.

In CreateProcess function the dwCreationFlags parameter can have CREATE_NO_WINDOW flag:

The process is a console application that is being run
without a console window. Therefore, the console handle
for the application is not set. This flag is ignored if
the application is not a console application

Here is a link to hide-win32-console-window executable using this method and source code.

hide-win32-console-window is similar to Jamesdlin's silentbatch program.

There is open question: what to do with program's output when its window does not exist? What if exceptions happen? Not a good solution to throw away the output. hide-win32-console-window uses anonymous pipes to redirect program's output to file created in current directory.

Usage

batchscript_starter.exe full/path/to/application [arguments to pass on]

Example running python script

batchscript_starter.exe c:\Python27\python.exe -c "import time; print('prog start'); time.sleep(3.0); print('prog end');"

The output file is created in working directory named python.2019-05-13-13-32-39.log with output from the python command:

prog start
prog end

Example running command

batchscript_starter.exe C:\WINDOWS\system32\cmd.exe /C dir .

The output file is created in working directory named cmd.2019-05-13-13-37-28.log with output from CMD:

 Volume in drive Z is Storage
 Volume Serial Number is XXXX-YYYY

 Directory of hide_console_project\hide-win32-console-window

2019-05-13  13:37    <DIR>          .
2019-05-13  13:37    <DIR>          ..
2019-05-13  04:41            17,274 batchscript_starter.cpp
2018-04-10  01:08            46,227 batchscript_starter.ico
2019-05-12  11:27             7,042 batchscript_starter.rc
2019-05-12  11:27             1,451 batchscript_starter.sln
2019-05-12  21:51             8,943 batchscript_starter.vcxproj
2019-05-12  21:51             1,664 batchscript_starter.vcxproj.filters
2019-05-13  03:38             1,736 batchscript_starter.vcxproj.user
2019-05-13  13:37                 0 cmd.2019-05-13-13-37-28.log
2019-05-13  04:34             1,518 LICENSE
2019-05-13  13:32                22 python.2019-05-13-13-32-39.log
2019-05-13  04:55                82 README.md
2019-05-13  04:44             1,562 Resource.h
2018-04-10  01:08            46,227 small.ico
2019-05-13  04:44               630 targetver.h
2019-05-13  04:57    <DIR>          x64
              14 File(s)        134,378 bytes
               3 Dir(s)  ???,???,692,992 bytes free

Example shortcut for running .bat script

Shortcut for starting windowless .bat file

Target field:

C:\batchscript_starter.exe C:\WINDOWS\system32\cmd.exe /C C:\start_wiki.bat

Directory specified in Start in field will hold output files.

Alex
  • 2,837
  • 5
  • 25
  • 27
2

To make the command window of a .bat file that executes a .exe file exit out as fast as possible, use the line @start before the file you're trying to execute. Here is an example:

(insert other code here)
@start executable.exe
(insert other code here)

You don't have to use other code with @start executable.exe.

phuclv
  • 37,963
  • 15
  • 156
  • 475
2

run it under a different user. assuming this is a windows box, create a user account for scheduled tasks. run it as that user. The command prompt will only show for the user currently logged in.

Joe
  • 21
  • 1
2

I used this to start a cmd file from C#:

Process proc = new Process();
proc.StartInfo.WorkingDirectory = "myWorkingDirectory";
proc.StartInfo.FileName = "myFileName.cmd";
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();
phuclv
  • 37,963
  • 15
  • 156
  • 475
Mike
  • 620
  • 7
  • 21
2

Compile the batch file to an executable using Batch2Exe http://www.f2ko.de/programs.php?lang=en&pid=b2e. Use the "Invisible Window" option.

  • 2
    There wasn't enough information about this app to make me feel comfortable in trying it. FAQ. Demo, something more than a screen-shot of one tab. – JeffO Aug 01 '11 at 01:35
  • Softpedia seems to have a good opinion about it: Bat To Exe Converter 1.6.0 - SOFTPEDIA "100% CLEAN" AWARD. Here is an alternative download link: http://www.softpedia.com/get/System/File-Management/Batch-To-Exe-Converter.shtml – Valentin Despa Nov 15 '12 at 21:06
  • I used it. It worked on my one computer, but i moved the exe files to another and they don't work on it. – Nathan F. Feb 04 '13 at 21:54
1

Please use this one, the above does not work. I have tested in Window server 2003.

@echo off 
copy "C:\Remoting.config-Training" "C:\Remoting.config"
Start /I "" "C:\ThirdParty.exe"
exit
NorthCat
  • 9,643
  • 16
  • 47
  • 50
  • 3
    What error did you get indicating the 'above' does not work? Maybe it is because you are using Windows Server? – JeffO Aug 11 '09 at 13:02
0

So below vbscript will launch the cmd/bat file in hidden mode.

strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)

'MsgBox(strFolder)

Set WshShell = CreateObject("WScript.Shell") 
WshShell.Run chr(34) & strFolder & "\a.bat" & Chr(34), 0
Set WshShell = Nothing

Now, only app window will be visible, not the cmd.exe window

uday
  • 569
  • 1
  • 6
  • 16
0

This is an easy work around for those who are fine with a dirty solution. Press win+tab, drag and drop the bat file to a new desktop and forget about it.

I occasionally will make a bat file that I'll rarely use, and it is kind of a drag to have to use tools to make the window hidden. This is no more complicated than it needs to be for me.

enter image description here

Gappy Hilmore
  • 440
  • 4
  • 13
0

Adding /min to the start command hides the window completely for me on my Windows 11 home edition

start /min C:\jdk1.8.0_221\bin\java -jar app.jar
Austin Poole
  • 652
  • 6
  • 11