Ghyath Serhal
Ghyath Serhal

Reputation: 7632

Run batch file in the background

I have a batch file, this batch file will not start automatically, it will only run when i double click on it.
Can I run the batch file in the background when I double click on it.

Upvotes: 6

Views: 13964

Answers (4)

AvahW
AvahW

Reputation: 2201

@Ghyath Serhal I have used cmdow to do this on another program, it is an external application that can be used to modify the command prompt. To use it, you will need to either enter this code (see below) into it's own batch file, or into the command prompt, where it will run 'BatchFile.bat' with a hidden terminal window. I haven't found a way to use this in a single batch file, but I only found out about this today.

cmdow /run /hid 'BatchFile.bat'

Hope this helps.

Upvotes: 1

Joey
Joey

Reputation: 354864

Well, you can start it minimized with start, if that is enough. Really hiding it is difficult (although I can think of an option right now).

Basically you need to determine whether the batch has been started by double-clicking it. You can do this by defining a special variable and look for it:

@echo off
if not defined FOO (
  set FOO=1
  start /min "" %~0
  exit /b
)

rem here whatever you wanted to do originally in the batch

As long as the FOO variable isn't defined (which is probably the default almost everywhere), this batch will launch itself minimized again, but with the variable defined first. Environments are passed to subprocesses, which is why this works.

Upvotes: 7

user197015
user197015

Reputation:

Once you click or tab away from the cmd.exe window that the batch file is running it, it's "in the background" -- I'm not really sure what you want but it sounds like you might be asking how to run the batch file without displaying the cmd.exe window.

If so I can think of two ways: first, you can create a shortcut to the batch file, right click it, and in the properties there set the shortcut to run minimized (should be a drop down option next to Run).

You can also wrap invocation of the batch file in a VBScript file using Windows Script Host's shell object (calling the Run method) to run the batch file invisibly. Passing 0 as the intWindowStyle parameter will suppress display of a window or anything.

Upvotes: 1

Hugoagogo
Hugoagogo

Reputation: 1646

you would generally need something else to run the script in that manor i.e. Create a shortcut, and set the “Run” field for the shortcut to “Minimized’.

Upvotes: 1

Related Questions