Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file call sub-batch file to pass n paramenters and return without use of file

Tags:

batch-file

I looking for a way using windows batch file that call sub-batch file which pass 1-9 parameters and return value (string) without the need of saving the return value into file/etc. The return value I save into variable like did in @FOR /F

I look at

@FOR /F "tokens=*" %%i IN ('%find_OS_version%') DO SET OS_VER=%%i 

and

Call function/batch %arg1% %arg2% 

I'm not seeing how I can setup to do this

EDIT: dbenham somewhat answer my question. His example was between batch file main part and function. My question was between two different batch files. Base off dbenham answer this is the follow layout.

Main batch file

CALL sub_batch_file.bat  return_here "Second parameter input"  REM echo is Second parameter input ECHO %return_here% REM End of main-batch file 

sub_batch_file.bat

@ECHO OFF SETLOCAL  REM ~ removes the " " SET input=%~2 (     ENDLOCAL     SET %1=%input% ) exit /b REM End of sub-batch file 
like image 571
TheMadCat Avatar asked Jul 14 '12 04:07

TheMadCat


People also ask

How do I pass parameters to a batch file?

You simply substitute the parameter for 1 (e.g., %~f2 for the second parameter's fully qualified path name). The %0 parameter in a batch file holds information about the file when it runs and indicates which command extensions you can use with the file (e.g., %~dp0 gives the batch file's drive and path).

What does %% do in batch file?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

What is %~ n0 in batch file?

%0 is the name of the batch file. %~n0 Expands %0 to a file Name without file extension.

What is %% g'in batch file?

%%parameter : A replaceable parameter: in a batch file use %%G (on the command line %G) FOR /F processing of a command consists of reading the output from the command one line at a time and then breaking the line up into individual items of data or 'tokens'.


1 Answers

Generally batch functions return values in one of two ways:

1) A single integer value can be returned via the errorlevel by using EXIT /B n where n = some number.

@echo off setlocal call :sum 1 2 echo the answer is %errorlevel% exit /b  :sum setlocal set /a "rtn=%1 + %2" exit /b %rtn% 

2) The more flexible method is to use environment variables to return one or more values

@echo off setlocal call :test 1 2 echo The sum %sum% is %type% call :test 1 3 echo The sum %sum% is %type% exit /b  :test set /a "sum=%1 + %2, type=sum %% 2" if %type%==0 (set "type=even") else (set "type=odd") exit /b 

The name of the variable where the answer is to be stored can be passed in as a parameter! And the intermediate values can be hidden from the main program.

@echo off setlocal call :test 1 2 sum1 type1 call :test 1 3 sum2 type2 echo 1st sum %sum1% is %type1% echo 2nd sum %sum2% is %type2% exit /b  :test setlocal set /a "sum=%1 + %2, type=sum %% 2" if %type%==0 (set "type=even") else (set "type=odd") ( endlocal   set "%3=%sum%"   set "%4=%type%" ) exit /b 

For a complete explanation of how the last example works, read this excellent batch function tutorial at DOStips.

Update

The above still has limits as to the content that can be returned. See https://stackoverflow.com/a/8254331/1012053 for an alternative method based on FOR that supports a wider range of values. And see https://stackoverflow.com/a/8257951/1012053 for a "magical" technique that can safely return absolutely any value with length < ~8190, under any circumstance.

like image 124
dbenham Avatar answered Sep 22 '22 22:09

dbenham