2

The article "Passing by Reference" in http://ss64.com/nt/syntax-args.html mentions the following:

In addition to passing numeric or string values on the command line, it is also possible to pass a variable name and then use the variable to transfer data between scripts or subroutines.

But how do I do it? When I set the value of a variable and pass it's name as in

set parm=42
call sub.bat parm

how do I use it in sub.bat?

mkluwe
  • 3,483
  • 1
  • 25
  • 44

2 Answers2

7

Via delayed exapansion

@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

-- OUTPUT --

var1=value1
var2=value2
dbenham
  • 123,415
  • 27
  • 239
  • 376
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)

Alex K.
  • 165,803
  • 30
  • 257
  • 277
  • That's clear to me, but the text I cited mentions "passing a variable name" (but without giving an example). – mkluwe Jan 15 '13 at 13:55