How to transfer variables from one batch file to another batch file?

How to write a batch file that receives an input variable and sends it to another batch file that needs to be processed.

Package 1

I don't know how to send a variable to package 2, which is my problem here.

Package 2

if %variable%==1 goto Example goto :EOF :Example echo YaY 
+5
source share
3 answers

You do not have to do anything. The variables set in the batch file are displayed in the batch file that it invokes.

Example

test1.bat

 @echo off set x=7 call test2.bat set x=3 call test2.bat pause 

test2.bat

 echo In test2.bat with x = %x%. 

Exit

... when test1.bat is executed.

 In test2.bat with x = 7. In test2.bat with x = 3. Press any key to continue . . . 
+5
source

You can pass batch1.bat variables as arguments to batch2.bat.

arg_batch1.bat

 @echo off cls set file_var1=world set file_var2=%computername% call arg_batch2.bat %file_var1% %file_var2% :: Note that after batch2.bat runs, the flow returns here, but since there's :: nothing left to run, the code ends, giving the appearance of ending with :: batch2.bat being the end of the code. 

arg_batch2.bat

 @echo off :: There should really be error checking here to ensure a :: valid string is passed, but this is just an example. set arg1=%~1 set arg2=%~2 echo Hello, %arg1%! My name is %arg2%. 

If you need to run scripts at the same time, you can use a temporary file.

file_batch1.bat

 @echo off set var=world :: Store the variable name and value in the form var=value :: > will overwrite any existing data in args.txt, use >> to add to the end echo var1=world>args.txt echo var2=%COMPUTERNAME%>>args.txt call file_batch2.bat 

file_batch2.bat

 @echo off cls :: Get the variable value from args.txt :: Again, there is ideally some error checking here, but this is an example :: Set no delimiters so that the entire line is processed at once for /f "delims=" %%A in (args.txt) do ( set %%A ) echo Hello, %var1%! My name is %var2%. 
+11
source

You can find the solution below -

 variable goes here >> "second file goes here.bat" 

What this code does is that it writes the variables to the second file, if it exists. Even if it does not exist, it will create a new file.

0
source

Source: https://habr.com/ru/post/1209676/


All Articles