Batch output parameters

Microsoft Windows xp professional editio...
November 15, 2009 at 05:38:04
Specs: Windows XP
Hi all,

I am trying to build some sort of API made of batch files. Each of them needs to be able to output one or more parameters. These batch files will be called from a "main" batch file. What I am doing is more complex than this example, but to illustrate:

GetFamilyMembersByName.bat: returns a list of family members according to a name given as an input parameter.

GetWeightByName.bat: returns the weight according to a name given as an input parameter

GetPersonInfo.bat: Calls GetFamilyMembersByName.bat, GetWeightByName.bat and outputs all person information.

The question is:
How do I make a batch file return one or more parameters?

I have read about using Exit /B <value> to store on the errorlevel, but this is not an option, as it allows only one value and it must be a number.

I don't mind if the output is a single string with several parameters separated with spaces or tabs that can be parsed afterwards.

I have googled for a solution of this but I couldn't find an answer.

Thanks in advance!

See More: Batch output parameters

Report •

November 15, 2009 at 05:47:30
this does not answer your problem, but my suggestion is, learn to use a proper language with powerful data structures etc, eg Python. It has ability to return more than 1 parameters from a function, even arrays. The same with Perl. Besides these, you have the flexibility to use ready made modules that can help you with your task more easily.

GNU win32 packages | Gawk

Report •

November 15, 2009 at 06:58:03
I agree with ghostdog's advice as I feel you are going on a rough lane that leads to nowhere. Batch scripts are not suited to replace facilities better performed by true programing/scripting tools.

Anyway a batch subprogram can not return any parameter but the numeric return code via the EXIT statement.

The called batch can just set up environment variables to return string values. You can code something as:

Calling statement

call GetFamilyMembersByName Member George

Called GetFamilyMembersByName.bat

set %1=Result from processing %2

Then in the calling batch you can access the variable Member as usual

echo.Member George is %Member%

I higly recommend you evaluate ghostdog's advice.

Report •

November 16, 2009 at 00:27:49
I too agree that other languages are probably the way to go.

Another way to do a similar thing to what IVO suggested would be to have all of the functions echo their output and call them inside of a for loop, that way you could use multiple lines and/or tokens to get the values.

Batch Variable how to

Report •

Related Solutions

November 17, 2009 at 07:18:27
Thanks a lot for your suggestions.
I need to get this done with built in Windows tools, so unfortunately Python is not an option. Maybe VBScript, but I don't quite know how to implement this.

I actually tested the environment variable option and it looks like it can work. The only thing I am missing is how to delete them after I don't need them.

Anyone happens to know how I can delete an environment variable after I create it using setx?

Report •

November 17, 2009 at 12:06:54
Why do you use setx to create environment variables in a batch script?

Setx is not a batch native command, it is a tool part of XP extensions and so not available on standard XP systems. More it creates variables persistent in the system environment, a harmful practice.

Variables generated by coding the set statement are local to the batch execution and vanish after the script ending.

Anyway to clear a variable code set(x) var=

Report •

November 18, 2009 at 00:11:33
Hi Ivo,
Thanks for your input.
I am aware that the variables are local to the batch execution, that is why I am creating environment variables. I need them to be available outside the execution for later use by other batch routines and that is why I am using setx.

Do you have any suggestion to implement this differently?


Report •

November 18, 2009 at 02:20:17
I have no practical suggestions to avoid the use of SETX stated your purposes. That enforced my opinion you are going on a dangerous lane. Take time to learn VBScript.

Report •

November 18, 2009 at 13:10:21
or just write the information to a squib file using echo, then the other batch script can read the file to get the data?

Report •

Ask Question