2

http://ss64.com/nt/syntax-args.html中的文章“通过引用传递”提到以下内容:

除了在命令行上传递数字或字符串值外,还可以传递一个变量名,然后使用该变量在脚本或子程序之间传输数据。

但是我该怎么做呢?当我设置变量的值并传递它的名称时

set parm=42
call sub.bat parm

我如何使用它sub.bat

4

2 回答 2

7

通过延迟扩展

@echo off
setlocal
set var1=value1
set var2=value2
call :sub var1
call :sub var2
exit /b

:sub
setlocal enableDelayedExpansion
echo %~1=!%~1!
exit /b

- 输出 -

var1=value1
var2=value2
于 2013-01-15T14:04:53.387 回答
0

Reference them by name, 2.bat run from 1.bat will inherit the same environment block, so

1.BAT

set parm=42
echo parm is '%parm%'
call 2.bat
echo parm is '%parm%'

2.BAT

set parm=XXX%parm%XXX

Would print:

parm is '42'
parm is 'XXX42XXX'

(Using call sub.bat %parm% would make a copy of parm available to sub.bat in %1)

于 2013-01-15T13:28:48.757 回答