How to run Windows batch files silently in the background

There are times when we want to run a batch file, program or command from the Windows command prompt but we don’t want the command prompt window to appear or to hang around on the desktop. For example, I have a batch script which backs up a load of files and it is triggered by the Task Scheduler every day, I don’t want a command prompt window suddenly appearing and staying open on the desktop every time this backup starts. e.g. I’m watching a movie on my HTPC and a window pops up and interrupts.

There is a fairly simple way to do it using a vbs script…

Set the scheduled task to run your command line from a batch file but using wscript as follows:

As an example, using notepad create a .bat file called date.bat  with this line:

echo %date% > date.txt

Save date.bat wherever you want. Double click it and it will open a command prompt window briefly, run the command and close the window again. You will now have a new file in the same location as date.bat called data.txt with today’s date in it.

Ok, this is a very short and simple command but you can imagine that if you have a command which takes a while to run and has a lot of on-screen output then the command prompt window may get in the way. So how do we get rid of it and still let the program run?

Create a new file in notepad with the following content:

sTitle = "Batch launcher"

Set oArgs = WScript.Arguments
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("WScript.Shell")

If oArgs.Count <> 1 Then
' Will die after 10 seconds if no one is pressing the OK button
oShell.Popup "Error: You need to supply a file path " _
& "as input parameter!", 10, sTitle, vbCritical + vbSystemModal

Wscript.Quit 1
End If

sFilePath = oArgs(0)

If Not oFSO.FileExists(sFilePath) Then
' Will die after 10 seconds if no one is pressing the OK button
oShell.Popup "Error: Batch file not found", _
10, sTitle, vbCritical + vbSystemModal

Wscript.Quit 1
End If

' add quotes around the path in case of spaces
iRC = oShell.Run("""" & sFilePath & """", 0, True)

' Return with the same errorlevel as the batch file had
Wscript.Quit iRC

Now save it as Launcher.vbs

If you want the command prompt window to be visible but minimised you can change the ‘0’ in line 26 above to a ‘7’:

iRC = oShell.Run("""" & sFilePath & """", 7, True)

 

You can now run your date.bat file silent in one of two ways…

  1. Through Task Scheduler

    Create a new task to run at your appointed date/time with the following action:
    wscript "<your file location>\Launcher.vbs" "<your file location>\date.bat"

     

  2. Create Another .bat File
    It may seem counter intuitive to create another .bat file to run the first one but this second one will open a command prompt window very quickly, call the vbs script then close the command prompt window while the vbs script runs the first file in the background.So while you do end up with another command prompt popping up then disappearing, you don’t get a window open for the duration of your first task.

    This is my preferred way of doing it when, for example, I have to run a startup program which runs my LED Ambilights. The Ambilight program runs in a command window and if I didn’t use this trick then the command prompt running the lights would stay open the whole time my computer was on. This way there is no persistent annoying window.

    So create another .bat file and call it whatever you want. Then populate it with the same command as you would have used for the Task Scheduler action:

    wscript "<your file location>\Launcher.vbs" "<your file location>\date.bat"

 

Obviously you can use the same ‘Launcher.bat’ file to run any other .bat files just by amending the .bat file you call in your scheduled task or other.bat file.

 


 

Note that I did find this info on the internet some time ago but I can’t remember where or I would have given credit! If it was you let me know in the comments.

Leave a Reply

(email optional)


Warning: Undefined array key "rerror" in /home/public/blog/wp-content/plugins/wp-recaptcha/recaptcha.php on line 291