Starting a program in batch file without stealing focus - batch-file

I know how to do this in C++, C#, etc, but I've got a simple BAT file that does a couple operations on a file, opens it in Notepad++, and proceeds to the next file. Because I want to wait for it to finish running on 2 to 20 files, I want it to run in the background until it's finished.
Here's my line to open the file in Notepad++:
start "" /b "C:\Program Files (x86)\Notepad++\notepad++.exe" "%filepath%"
Is it possible to START a program so that it runs without stealing focus?

This is not a general solution, but for your specific requirement (notepad++) you could try command line option of notepad++.exe -systemtray
from notepad cmd line spec:
-systemtray
Start Notepad++ minimised in the system tray, aka notification area
(There is an option to start program minified start /min, but n++ does not honor it... it works with regular notepad, though)

Related

Trying to start multiple .exe files in order with a bat file but first line fails and second line is successful

My code in the .bat looks like this.
"C:\users\reshade injector.exe"
C:\pathtogame\game.exe "added argument"
"c:\users\reshade injector.exe" <- if I open the actual file, it opens a command window and waits for my other application to launch. Is this a console app?
C:\pathtogame\game.exe "added argument" <- this works and launches my game
Another issue is that when launching the first .exe, a command window stays open. How do I make the command window not appear?
Can anyone help me with these two problems?
*edit for clarity
by opening the actual file, I mean double-clicking on the original reshade injector.exe.
When opening reshade injector.exe, two cmd windows open. They close as soon as I launch the game.exe.
I can't seem to get the reshade injector.exe to launch through the .bat file.
Batch will run any application it finds. If the application does not terminate (eg. it's waiting for user-input) then it will progress to the next instruction.
Some applications work by launching another (the user-interface) and then terminating, leaving the second application resident.
There's no way to tell without observing which approach any random application may take.
So - your batch should execute a file named c:\users\reshade injector.exe.
You say that "fails". Is there an error message generated? You would likely need to run the batch from the "command prompt" to see that message. Note that the Space between reshade and injector must be in the actual filename. If injector.exe is a file in a subdirectory called reshade, then you need a backslash between reshade and injector.
Having resolved the actual name of the first executable, try
#echo off
setlocal
start "" "firstexecutablename"
start "" "secondexecutablename" "arguments"
The first two lines prevent "command echoing" which is used for debugging and establish a "local environment" which ensures the "environment" for the batch file remains unchanged when the batch terminates. In the current case, these two lines are probably unnecessary, but it's SOP for batch files.
The batch should then close its session and window once it has attempted to launch the two applications.
The "" following start becomes the window-title. It can be any quoted string you like, but should not be omitted as start uses the first quoted string it finds as a window-title and may not recognise it as part of the executable/arguments.

Start a batch file and close Cmd once it is finished

I have my application spread over multiple directories, each containing a part (e.g. web frontend, mobile application, administration, middleware, backend, ...).
In each of the directories I have one part, and a file compile.cmd which compiles that part and looks roughly like this:
#ECHO OFF
compiler prepare thisPart
compiler compile thisPart
copy resultingFile1 ../bundleDirectory
...
copy resultingFileN ../bundleDirectory
pause
The "pause" is so I can check the compiler output, whether compile failed and which error messages occurred, and then close the window with a single keystroke (mostly, space key).
I now want to have a batch file that calls all these batch files for different application parts in parallel, so I guess I have to open a new shell window for each.
So I wrote CompileAllParts.cmd like this:
cd part1
start "Compile part 1" compile.cmd
cd ../part2
start "Compile part 2" compile.cmd
cd ../part3
start "Compile part 3" compile.cmd
Positive is that I can influence the window title, but the drawback is that new Cmds are spawned which do not automatically close.
This is also part of the documentation of start:
If command is an internal cmd command or a batch file then the command processor is run with the /K switch to cmd.exe. This means that the window will remain after the command has been run.
Is there a hidden parameter to explicitly disable this behaviour?
combine start with cmd. First to set title and working folder, second to use /C and execute the command:
start "Compile part 1" /d "part1" cmd /c "d:\proper folder\compile.cmd"

Winzip Self Extracting exe would end abruptly without showing "All Done" in cmd

I have got a batch script that ends with the following:
TITLE ALL DONE. You can close this window
ECHO.
ECHO ALL DONE. You can close this window
ECHO.
Pause
EXIT
it works fine when i run just the bat file (see below)
All Done! You can close this window.
Press any key to continue . . .
But when i create a self extracting exe out of the same bat script it wouldn't show this window after the execution and would just exit without any proper message.
Since the script works fine by itself, i am a bit lost on what could be causing this? How could i make the self extracting exe to show that window and prompt the user to close it like above.
It depends on the content of the executable, if your including other files to your archive be sure the program you run after extraction is your batch file.
If your only including that file i suggest you use BatToExe Converter
much more efficient way to build it, also it supports commandline, including other files, product info, admin manifest etc

Batch Files - Closing Opened Text Files

I currently have a Windows batch file that runs an .exe file that uses a text file. I am trying to have the Windows batch file run the .exe file multiple times. This, however, requires the use of the same text files to read from. The command prompt gives me the error that the ".txt could not be opened" (I assume from this that it is already open.)
I am trying to see if there is a way in a .bat file to system call to kill that specific text file. The suggestions I see online are to use 'taskkill notepad.exe', but that returns "invalid argument" because the program doesn't open Notepad to use the text file.
Any suggestions would be appreciated.
It sounds like your existing script fails because the first instance of the exe is still open when the second instance starts.
One thing worth trying (and this depends on the nature of the application you are invoking) is to start the executable using the START /WAIT /B ... command. This makes the command interpreter wait for the program to exit before it moves onto the next command, so as long as nothing else is locking the text files you should be OK to move onto the next command.

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.
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.
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’.
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.
#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.

Resources