I have a set of three automatically generated batch files, summed up below
arbitrary.bat
#ECHO OFF
set APPDATA=C:\Users\%USERNAME%\AppData\Minecrafts\1.9p5\
set T1=ÚÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
set T2=³ÍÍÍ 1.9p5 ÍÍͳ
set T3=ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
masterControl
masterControl.bat
varInit
[main program logic...]
varInit.bat
set U=valueOfU
set P=valueOfP
set S=valueOfS
pause
The idea is that arbitrary.bat is run and the other two are called, as well. However, the program stops right after masterControl runs its first line (all lines in varInit are run successfully). **Why doesn't this program run past the first line of masterControl?
You have to use the command CALL to call each batch file from another i.e. CALL masterControl.bat
Related
I am new to scripting and i am experiencing an issue that i can solve by my own.
I have a script that runs couple of times ( 3-10 ). I need a loop at the end of the script to run another batch file that will send mail that script run okay.
Problem is that it always sends between 3-10 mails ( because script runs mupltiple times)
I need a loop at the end of the script that says Run batch file only once, no matter how many times does main script runs
I have used this:
FOR %%A IN (1) DO (
START C:\dodavka\send_mail.bat
)
This did not work.
Set an environment variable in your sub script code on which you check during the run of this batch file if it is set.
You have to call the batchfile with the CALL keyword so that the values set in that batch-file are kept in memory.
In your main code:
If "%MYVARIABLE%" == "" THEN CALL SUBBATCHFILE
In your child code:
SET MYVARIABLE = "SET"
Is it possible to start another batch file and pass along multiple parameters with spaces, using the start command?
Here is how my program currently works:
main program starts > sees its outdated > calls updater (data1.exe) > updater copies new version over > It tries to delete the old version, but it can't. The old version is still marked as being used, from when it called the updater.
That's why the call command won't work. Do I need to use start then? How would that work?
This was the original line of code... the one that calls the updater and passes the variables along:
call "%dirofbatch%data1.exe" "%downloc%" "%dirofbatch%" "%lver%" "%lget%"
I'm stumped.
EDIT: I should mention that "data1.exe" is just an exe'd batch file.
How to read parameters in a batch file:
caller batch
start "" "%dirofbatch%data1.exe" "%downloc%" "%dirofbatch%" "%lver%" "%lget%"
called batch
set "parm1=%~1"
set "parm2=%~2"
set "parm3=%~3"
set "parm4=%~4"
echo %parm1% %parm2% %parm3% %parm4%
I have a batch file that first creates another batch file containing a ClearCase cleartool command and second, runs it:
ECHO cleartool lsactivity -long "%ACTIVITY%"^>"%OUTPUTFILE%">FILETORUN.bat
CALL FILETORUN.bat
When running the batch, FILETORUN.bat is generated with the correct format, but the CALL to it is completely ignored.
If I ECHO output after the CALL to a log file, I can see that the script just skips over it.
What could it be?
I have tried removing CALL but it makes no difference.
EDIT: SOLUTION
Thank you all for the input. I found the problem. Before the write to batch and batch call in the script there was a command that read information into a variable from a file:
SET /p FILETODELETE=<rmname_%CLEARCASE_USER%.tmp
It reads only the first line. For some reason this created a conflict with temporary batch file, and I have no idea why. I used a different solution for reading the first line from a file and the conflict doesn't happen anymore:
(set FILETODELETE=)
for /f "delims=" %%q in (rmname_%CLEARCASE_USER%.tmp) do if not defined FILETODELETE set FILETODELETE=%%q
If anyone can shed some light it would be great!
SET /P waits for user input, so it actually will finish the command with what you are trying to execute after that and consume the input buffer, which might produce different results on each machine.
See set command reference for more details
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.
I understand how to call nested batch files from within a parent file using the call command, as there are plenty of resources on that:
CALL
CALL (SS64)
Bat file termination
However, I don't understand why calling another batch file from another terminates the parent.
For a less abstract example, suppose I have a batch file that "links" together separate batch files, and I erroneously didn't prepend call to each line:
foo.bat
bar.bat
This would only execute foo.bat and then exit. To correctly execute both commands, I would have to prepend call before each statement:
call foo.bat
call bar.bat
Why does the first functionality still exist? Why hasn't it been changed? I noticed that call was introduced in MS-DOS 3.3, which was released in the late 1980s, so is this functionality still here for reverse compatibility?
I can't think of any (practical) usages of it, but perhaps I'm too used to "new" programming techniques.
DOS used simple text processing (back when you had things like FILES=20 in config.sys to allow 20 file handles), so opened the file, read the next line, closed the file, then executed the line just read. If the file called another, then the processing continued with that file, so only 1 file handle would be required for a batch file.
Until Microsoft put in the call command, there was no way to get back to the original file (without using tricks like giving the name of the previous file as a parameter, and using temporary files to let the original batch file know it had dome some processing, and could then GOTO the next part of the file).
As Sean Cheshire wrote, it's necessary for backward compatibility.
But starting a batch file from a batch file without using CALL does not terminate the parent!
It looks that way, as the parent normally will not executed further after the second batch exits.
But using a call before starting the second.bat, will show that the first batch isn't terminated.
parent.bat
echo parent.bat
call :myLabel
echo back in parent.bat main
exit /b
:myLabel
second.bat & echo back in parent.bat
exit /b
second.bat
echo second.bat
exit /b
I use here the the secpond.bat & echo back ... to avoid another bug/feature of cmd.exe.
If you use second.bat without any extras it will start second.bat AND jump to the label :myLabel in second.bat!
Call is basically saying "go execute this other batch file, and then come back here and continue". It has been there since DOS 3.3 or so, and if it were removed now would break all backward-compatibility (which is why people are still using batch scripts). It can also be used to branch to :link locations.
For info on the use and syntax (for future reference for others), you can see this MS TechNet link
If you need new functionality, use CMD scripts or PowerShell scripts instead.