Avatar of deepak singla
deepak singla
 asked on

Return result to caller batch file

I have two batch files A.bat and B.bat. A.bat is calling to B.bat.
B.bat is doing some processing, and return back the result to A.bat. How A.bat can accept the return?

A.bat
@echo off
set str1=1,2,3,4,5
set str2=6,7,8,9
call B.bat %str1% %str2%

B.bat
@echo off
set str1=%1
set str2=%2
set str3=%str1%,%str2%

Expected that B.bat return %str3% (means 1,2,3,4,5,6,7,8,9) back to A.bat file So that A.bat used the result for further processing.
Windows Batch

Avatar of undefined
Last Comment
ste5an

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ste5an

oBdA write it already: there is no function in a command shell batch. You can only call/jump to methods. But you can set global variables.

In the case of different batch files: The process is called piping. Thus the use of Echo to output the result to the standard pipe is the correct approach.

So I would thus use a slightly different syntax for the "function" result in a closed batch, call it a different flavor:

@Echo Off
SetLocal

Set Str1=1,2,3,4,5
Set Str2=6,7,8,9
Call :JoinStrings "%Str1%" "%Str2%"
Echo Result: %_Result%

EndLocal
GoTo :Eof

:JoinStrings  <String 1> <String 2>
SetLocal

Set Str1=%~1
Set str2=%~2
Set Result=%Str1%,%Str2%

EndLocal & Set _Result=%Result%
GoTo :Eof

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23