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"
Related
I have 3 batch files which consume a common variable-'target'. These batch files need to be executed one after the other. I am using one main batch file where I am trying to call these three batch files. But the execution stops right after first batch job is done. If I execute these batch jobs individually within that main batch file, they gets executed fine without any issue. Not sure what's missing here.
Main batch file (MainBatch.bat) contents:
set target=OHD121
CALL C:\Users\abc\x1.bat
pause
CALL C:\Users\abc\y1.bat
pause
CALL C:\Users\abc\z1.bat
pause
I figured the reason behind the issue. in my called bat files, I have another bat file for which I wasn't using call and that's why the control wasn't returning to original call action to perform subsequent steps.
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
I want to make a automated program in .bat. The program needs to run a command. However, the command must be run from a custom CMD.
If I open a regular CMD, the commands that I will do:
C:\Hardware\bin\StartCustomCMD.bat init (This is the first thing I will type. It starts the custom CMD.)
bb autobuild (This is the second thing that I will type. The command goes into the custom CMD)
You can probably tell that I did not write these scripts. I am trying to set this up in Windows Scheduler so that the script gets run automatically every day. Any help on how can I do that?
Thanks.
Make yourself a new batch file, and embed the other things into it, and then run that.
#echo off
call C:\Hardware\bin\StartCustomCMD.bat
bb autobuild
If bb it itself a batch file, then use call on it also. What call does is execute another batch file, and then continue processing. If you don't use call, when you run one batch file from another, the latter 'takes over' and the caller does not continue.
To do this you can use the timeout and goto command. Timeout waits a period of time in seconds but it can be skipped by pressing any key while cmd is open at the top layer. If you can see cmd press its icon on the desktop until you can not see it. Then using the goto command you can go to the top line. So here would be your script:
:start
C:\Hardware\bin\StartCustomCMD.bat init
bb autobuild
timeout 86400
goto start
You already know what the first two commands do, but timeout 86400 waits exactly one day then the goto start command goes to the first command so that gets repeated. If you need to add any more commands then put them above the timeout 86400 command.
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 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