0

I am creating a generic script that will parse a statement and return the result. I have done so in shell, but don't know how this can happen in batch scripting

Script 1 Main script (main.bat).

SET Myresult=CALL child.bat "Statement"

Now Myresult should store the answer whatever I want to return in this variable.

Solution 1 : SET Myresult in Child.bat and use it in main.bat but now what if the user does not know what the variable name is .

So is their a way to return a value like in java return xyz

xyz gets captured in the call statement elsewhere.

-------------------------------xxxxxxxxxxxxxxxxxxxxxxx-----------

PART 2

Here are the details of what I am doing .

The so called child script is getSQLResult.bat

What is does is

getSQLResult.bat -q "Select a from abc" 

Now this above call statement can be used by anyone any how in any batch script .

So apart from passing a variablename(return name) or writing a for loop to parse the result set is there any simple straight forward way .

2 Answers 2

1

Option 1 - Pass the variable name

main.bat

call child.bat myresult "Statement"
echo %myresult%

child.bat

set "%~1=argument was %~2"

Option 2 - Process output of child process

main.bat

for /f "delims=" %%a in ('child.bat "Statement"') do set "myresult=%%a"
echo %myresult%

child.bat

@echo off
echo Argument was %~1

or you can use temporary files, the registry, the clipboard, ... to pass the information, but in any case if you are coding a element (your child.bat) to be reused by you or another person, you are creating an interface between this element and the rest of the code, a expected set of input arguments and a way to return information. Your question

... what if the user does not know what the variable name is?

is answered by the documentation of this interface.

Sign up to request clarification or add additional context in comments.

2 Comments

Thnks MC ND but my question is a bit different .check out the edited question part2
@jjames, I've readed the modification. The statement execution will generate from 0 to n output lines. Environment variables content is limited to 8191 characters. The option to store the result into a variable is not safe/robust/convenient. Best option is to send statement execution to stdout from child and use the for /f in parent to process the output.
0

This is the way to do it in batch:

for /f "delims=" %%a in ('echo hello world') do set myresult=%%a
echo it returned: %myresult%

(echo hello world is just an example)

Note: this will return the last (or only) line only. It can be modified to process more lines, but your example implies, there is only one line of output)

EDIT If I understand your edit correct, you want to pass just a statement to an external batchfile, which does the work and gives back the result into a variable, which is defined with the call.

REM child.bat
@echo off
set var=%1
for /f "tokens=1,*" %%i in ("%*") do (
  set var=%%i
  set statement="%%~j"
)
for /f "delims=" %%a in ('getSQLResult.bat -q  %statement%') do set "%var%=%%a"

use it with:

call child.bat result "Select a from abc"
echo %result%

of course it will still return the last (or only) line. Can be easily modified to get the first line. If the output could be more lines, I suggest using an "array" with an counter to avoid the problems, MC ND adresses in his comment to your question. Maybe returning a filename to a file with the multiline output could be another solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.