Prevent Call Statements From Running At The Same Time - Batch - batch-file

I am using a .bat file to open up multiple windows for a program to expedite software installation and computer setup. The result I am looking for is that when I click the "X" to close out of one window, I want the next call statement to execute and open the next statement on the list. This works fine when the call statements have the same filetype. Example:
#echo off
call "New Text Document.txt"
call "New Text Document (3).txt"
#pause
But let's say I add a statement that calls a web browser in between my two text file calls.:
#echo off
call "New Text Document.txt"
call "a.url"
call "New Text Document (3).txt"
#pause
Now is when I experience the issue. When I close my first text file it will open up "a.url" and "New Text Document (3).txt" at the same time instead of one sequentially after I close out of one of them like it does when I just have two text files.
I understand that if I put #pause in between my call statements I won't have this problem and it would still speed up the software/setting setup process greatly, but I would really like to have it open things sequentially after closing a window instead. What can I do to resolve this?
Thanks in advance for all help offered.

Try to replace your call commands with start
start "" /wait "new document.txt"
start "" /wait cmd /c "something.cmd"
start "" /wait calc.exe

Related

batch file to exe to auto run when computer start

I am a new programmer, i have created a batch file to open a website but i want that batch file to run when the computer starts.
Here's the code:
#echo off
:top
start iexplore.exe http://www.website.com
timeout 3
goto top
While i do think Arescet's answer will work, i am more in favor of using Windows' Task Scheduler.
Simply create a new task :
Assign it's trigger to be At Startup :
and add a new action to Start a program giving it the path to your batch file:
I believe this is a cleaner approach which also provides you with logging and history should you decide to do more things in your batch file later on
Type run into windows search, type shell:startup in the prompt, press enter, Then, create a shortcut of your program, and move the shortcut into the startUp folder.
On note of your code, the start command should have a blank title as "", and quotes around the application name and parameters passed, like:
start "" "iexplore.exe" "http://www.website.com"

Starting a program in batch file without stealing focus

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)

Running batch file, call another batch, ctrl+c out of that, then return to the first?

I have a batch script which mount's a .vhd file and creates a junction point, the it call's another batch script which runs some things on the mounted vhd/junction point. When that script finishes, it then comes back to the 1st batch script and deletes junction point, unmounts .vhd then exits.
The problem is the second script needs to exit properly in order to run the rest of the first script, when most of the users will just ctrl+c and close the second script.
Is it possible to prevent this from happening, either by not allowing the ctrl+c cancel or somehow restart the first batch file after the call has happened?
try this to start the 2nd batch file:
start /b /w "" 2nd-batch.bat
Thanks to Endoro's answer, even though it didn't work for me, it made me remember start, i always forget start and just use call. anyway the solution i found was was to do this:
2>nul (
echo N|start /wait "" cmd /c C:\2nd-batch.BAT
)
it creates a new window for the 2nd-batch.bat which allows the dumbuser to even exit it via the X button and it still completes the rest of the 1st-batch.bat

Run batch files sequentially

I want to ask you all how to run batch files sequentially in Windows.
I have tried :
start /w batchfile_1.bat
start /w batchfile_2.bat
..
start /w batchfile_n.bat
but I have to close the previous .bat file process manually (e.g. by clicking) before continuing into the next one.
Is there any solution to do this automatically without me doing the manual closing previous .bat program every time?
Thanks a lot.
I would check the solutions to this question: Run Multiple batch files
Taken from the answer in the link.
Use call:
call bat1.cmd
call bat2.cmd
By default, when you just run a batch file from another one control will not pass back to the calling one. That's why you need to use call.
Basically, if you have a batch like this:
#echo off
echo Foo
batch2.cmd
echo Bar
then it will only output
Foo
If you write it like
#echo off
echo Foo
call batch2.cmd
echo Bar
however, it will output
Foo
Bar
because after batch2 terminates, program control is passed back to your original batch file.
If you are in love with using START, you could have your batch files end with the EXIT command. That will close the windows created by the start command.
#echo off
.
.
:: Inspired coding
.
.
exit
I'm not sure but based on your comments, the following seems to be happening when you run that sequence of START commands:
A START /W command is invoked and starts a batch file.
The batch file starts executing and runs a program.
The batch file finishes and its console window remains open, but the program continues running.
The START /W command that was used to run the batch file is still executing because the console window remains open.
You wait until the program terminates, then you close the console window, and then the next START /W command is invoked, and everything is repeated.
Now, if you place EXIT at the end of every batch file you want to run sequentially, that makes situation worse because it causes the console window to close after the batch script completes, and that in turn ends the corresponding START /W command and causes another one to execute, even though the program invoked by the batch script may still be running. And so the effect is that the batch scripts (or, rather, the programs executed by them) run simultaneously rather than sequentially.
I think, if this can be solved at all, you need to move the START /W command and put it in every batch file in front of (every) command that that batch file executes and doesn't wait for the termination of. That is, if your batchfile_1.bat runs a program.exe, change the command line to START /W program.exe, and similarly for other relevant commands in other batch files.

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