How to run a batch file without launching a "command window"?

518,465

Solution 1

Save the following as wscript, for instance, hidecmd.vbs after replacing "testing.bat" with your batch file's name.

Set oShell = CreateObject ("Wscript.Shell") 
Dim strArgs
strArgs = "cmd /c testing.bat"
oShell.Run strArgs, 0, false

The second parameter of oShell.Run is intWindowStyle value indicating the appearance of the program's window and zero value is for hidden window.

The reference is here http://msdn.microsoft.com/en-us/library/d5fk67ky.aspx

Solution 2

This is just a simplification of Shaji's answer. You can run your batch script through a VBScript (.vbs) script like this:

HideBat.vbs

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

This will execute your batch file with no command window shown.

Solution 3

Just to expand on the "Use Windows Scripting" answers (which I consider best because it's built-in already) here's how to do it by using a single wrapper script and passing the name of the "real" batch file as a parameter. Additional parameters will be passed on to the batch file.

If WScript.Arguments.Count >= 1 Then
    ReDim arr(WScript.Arguments.Count-1)
    For i = 0 To WScript.Arguments.Count-1
        Arg = WScript.Arguments(i)
        If InStr(Arg, " ") > 0 Then Arg = """" & Arg & """"
      arr(i) = Arg
    Next

    RunCmd = Join(arr)
    CreateObject("Wscript.Shell").Run RunCmd, 0, True
End If

So e.g. save the above file as NoShell.vbs somewhere then call:

NoShell.vbs c:\foo\my_batch_file.bat

Finally, if you're looking to run this from somewhere that doesn't understand the .vbs file (such as an "External Tools" in Visual Studio), you'll want to call C:\Windows\System32\wscript.exe with the vbs file as its first parameter and your batch file as the second.

Solution 4

Use start with the '/B' option. For example:

@echo off
start /B go.bat

Solution 5

You can change the properties of the shortcut to run minimized.

To run it completely invisibly you'll need something else, like Windows Scripting.

Share:
518,465

Related videos on Youtube

Jaksa
Author by

Jaksa

Updated on September 17, 2022

Comments

  • Jaksa
    Jaksa over 1 year

    On Windows XP, can I run a batch (.bat or .cmd) file, via a shortcut, without a "black window"?

    • Eric
      Eric about 14 years
      Are you asking if you can prevent the command window from showing up when you run a bat file?
    • Tobias J
      Tobias J over 10 years
      Yes, you can easily change the default color of the output window by using the Properties on a cmd.exe shortcut. (yes this is a joke...)
  • Joey
    Joey about 14 years
    start /b will just run the program in the currently-allocated console instead of spawning a new one. You'll get a new one anyway since the batch has to run with cmd (which, in turn [surprise], opens a console).
  • martineau
    martineau about 12 years
    Your first suggestion is the way I've always done it.
  • Rolo
    Rolo over 9 years
    For those that dislike the idea & concept of having to create a new vbs file for every new bat file one creates, as one must using either of the top two answers, this is the perfect solution. Instead of multiple vbs files, you'll have multiple shortcuts. For every new bat file, create a shortcut of the vbs file, right click on it, choose properties > Shortcuts tab, & in the Target box enter the path of the bat file after the path that's already there, just as shown above or like this if the shortcut isn't in the same place as the vbs file: "C:\My Files\HideCmd.vbs" c:\foo\my_batch_file.bat.
  • Rolo
    Rolo over 9 years
    If the file path of the bat file has a space, how can it be called via a shortcut to the vbs file? For example: "C:\My Files\HideCmd.vbs" c:\my things\my_batch_file.bat. No matter what I try for the path--double quotes around it all, %20 in place of the space, or a combination of those two, nothing works. I get an error message telling me that the file cannot be found or nothing happens. Is this due to user error on my end, a Windows limitation with the Target field for shortcuts, or a problem with the vbs script in which it doesn't handle file paths with spaces properly?
  • nixda
    nixda over 9 years
    This is not a solution for the OP's problem. There's a difference between Minimizing and Hiding
  • Tobias J
    Tobias J over 9 years
    @Rolo I've updated the script so that if any of the incoming parameters have spaces, it will surround them with quotes. So you'll want to wrap the path to the batch file with quotes when calling it, then it will be considered a single argument and re-wrap before being called.
  • Whoever
    Whoever over 9 years
    If you change shortcut to use the right icon, the minimized splash on the task bar is actually a nice reminder something is starting.
  • Tomáš Zato - Reinstate Monica
    Tomáš Zato - Reinstate Monica over 8 years
    @Joey yes, but this is very useful for launching batch files from other applications through shell execution (like SYSTEM in C/C++).
  • Joey
    Joey over 8 years
    @TomášZato: You can just call the batch file directly in that case, or, if whatever you use to spawn a process doesn't create a shell first, use cmd /c foo.cmd. There's absolutely no need to use start in those scenarios, and, I'd argue that system should not be used in favor of CreateProcess. By launching a batch file, you're dependent on one platform already, you might as well use the better tools for the job.
  • Dan Z
    Dan Z over 7 years
    Would the third line be: strArgs = "cmd /c FILEPATH\testing.bat"?
  • T_D
    T_D about 7 years
    @Joey Thanks a bunch, never heard of cmd /c before. The cmd command is what I needed in my case, but all the stackexchange answers I found so far, about a batch file running another batch file, have top answers to use start...
  • Laurens Swart
    Laurens Swart about 7 years
    Any way to run the bat in elevated mode while still remaining invisible?
  • Guru Josh
    Guru Josh almost 7 years
    This is a very nicely designed and useful application.
  • Eryk Sun
    Eryk Sun almost 7 years
    start /b go.bat will not allocate a new console (i.e. an instance of conhost.exe -- or a thread in csrss.exe before Windows 7). However it will make a mess since the batch script is run with a new instance of cmd.exe that's attached to the same console. Both instances of the shell will compete to read input from the console, typically alternating. It needs /w (e.g. start /b /w go.bat) in order to wait on the second instance, which is similar to using cmd /c if using system. Also, if you're calling CreateProcess, run go.bat directly. There's no need for cmd /c.
  • dmitry_romanov
    dmitry_romanov almost 6 years
    That is a nice and clean solution for my problem! Thank you!
  • Mehdi Dehghani
    Mehdi Dehghani almost 6 years
    How to run .bat file as administartor using this method?
  • Mehdi Dehghani
    Mehdi Dehghani almost 6 years
    How to run .bat file as administrator using this method?
  • gumuruh
    gumuruh over 5 years
    minimizing... hehe.
  • Mister SirCode
    Mister SirCode almost 5 years
    @MehdiDehghani properties > security > run as admin. If that only works for exe or apps, then oh wells.
  • Fabian Röling
    Fabian Röling over 3 years
    This was the only thing that worked for me. I wanted to use this script to maximise another window, but whenever I followed the usual instructions for hiding task windows, like making a shortcut with "minimised" selected, using the "hidden" option and "whether logged in or not", etc., I always either still got a window or the script didn't work, probably because it no longer ran as the correct user.
  • lyrica
    lyrica almost 3 years
    @Whoever Can you clarify what you mean?