批处理文件中调用子批处理文件不予处理N paramenters和未使用的文件返回 [英] Batch file call sub-batch file to pass n paramenters and return without use of file

查看:130
本文介绍了批处理文件中调用子批处理文件不予处理N paramenters和未使用的文件返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我寻找使用Windows批处理文件,调用哪个传递1-9参数子批处理文件,并返回值(字符串),而不需要保存的返回值成文件/ etc的方法。返回值我保存到变量像@FOR /˚F

我在看

  @FOR / F令牌= *%%我(%find_OS_version%')DO SET OS_VER = %%我

 呼叫功能/批%ARG1%%ARG2%

我没有看到我怎么可以设置要做到这一点

修改:dbenham有些回答我的问题。他的例子是批处理文件主要部分和功能之间。我的问题是两个不同的批处理文件之间。基地起飞dbenham回答这个问题是后续的布局。

主要批处理文件

  CALL sub_batch_file.bat return_here第二个参数输入REM回声第二个参数输入
ECHO%return_here%
主批处理文件REM结束

sub_batch_file.bat

  @ECHO OFF
SETLOCALREM〜删除
SET输入=%〜2

    ENDLOCAL
    SET%1 =%输入%

退出/ B
子批处理文件REM结束


解决方案

一般来说一批功能两种方式之一返回值:

1)一个整数值可通过错误级别使用 EXIT / B返回ñ,其中n =一些数字。

 关闭@echo
SETLOCAL
致电:1之2
回声答案%ERRORLEVEL%
退出/ B:和
SETLOCAL
集/ ARTN =%1 +%2
退出/ B%RTN%

2)更灵活的方法是使用环境变量返回一个或多个值

 关闭@echo
SETLOCAL
致电:测试1 2
回声总和%总和%是%类型%
致电:测试1 3
回声总和%总和%是%类型%
退出/ B:测试
集/ A总和=%1 +%2,类型= SUM %% 2
如果%键入%== 0(套式=偶数),其他(套式=奇)
退出/ B

,其中答案是要存储的变量的名称可以被传递作为参数!与中间值可以从主程序被隐藏。

 关闭@echo
SETLOCAL
致电:测试1 2 SUM1 TYPE1
致电:测试1 3 2型SUM2
回声一号总和%SUM1%是1型%%
回声第二总和%SUM2%是2型%%
退出/ B:测试
SETLOCAL
集/ A总和=%1 +%2,类型= SUM %% 2
如果%键入%== 0(套式=偶数),其他(套式=奇)
(ENDLOCAL
  设置%3 =%金额%
  集%4 =%类型%

退出/ B

有关的最后一个例子是如何工作的完整说明,请阅读DOStips这个优秀的批处理功能教程

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

解决方案

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.

这篇关于批处理文件中调用子批处理文件不予处理N paramenters和未使用的文件返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆