Run batch file in background

I have a batch file, this batch file does not start automatically, it will work only when you double-click on it.
Can I run the batch file in the background by double-clicking on it.

+6
batch-file
source share
4 answers

Well, you can start minimizing it with start , if that's enough. It’s actually hard to hide (although I can think of a choice right now).

Basically you need to determine if a batch has been started by double-clicking it. You can do this by specifying 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 is not defined (which is probably the default almost everywhere), this batch will start again, but the variable will be defined first. Environments are passed to subprocesses, so this works.

+7
source share

you usually need something else to run the script on this estate β€” that is, create a shortcut and set the Run field to the Minimized shortcut.

+1
source share

As soon as you click or close the cmd.exe window that the batch file is working, it is "in the background" - I'm not sure what you want, but it looks like you can ask how to run the batch file without displaying the cmd.exe window.

If this is the case, I can think of two ways: firstly, you can create a shortcut for the batch file, right-click it, and in the properties set the shortcut to run with the minimum value (there should be a drop-down option next to Start),

You can also transfer a batch file call to a VBScript file using the Windows Script host shell object ( invoking the launch method ) to run the batch file invisibly. Passing 0 as an intWindowStyle parameter will suppress the display of the window or something else.

+1
source share

@ Ghyat Sergal I used cmdow to do this in another program, this is an external application that can be used to change the command line. To use it, you need to either enter this code (see below) in its own batch file, or in the command line, where it will run "BatchFile.bat" with a hidden terminal window. I did not find a way to use this in one batch file, but I only found out about it today.

cmdow /run /hid 'BatchFile.bat'

Hope this helps.

+1
source share

All Articles