CMD DOS BATCH – Example of Variable passed to subroutine-Function
as reference
Quick example of how a variable passed to a DOS subroutine
(or function) is by reference.
Steps:
1.
Begin with original value “MyOriginalString”
2.
Pass variable with original string
3.
Subroutine appends the value “_MyAppendedValue”
4.
Echo out the revised variable in the calling
function echos as “MyOriginalString_MyAppendedValue”
Test.cmd:
@echo off
cls
setlocal enabledelayedexpansion
set myVar=MyOriginalString
echo myVar 1 =!myVar!
call :mysub myVar
echo myVar 2 =!myVar!
goto:eof
:mysub
setlocal enabledelayedexpansion
echo local is set here
echo The NAME of the passed argument is:%1
echo The VALUE of the passed argument is:!%1!
set "holdValue=!%1!"
echo local is ended here
endlocal & set "%1=%holdValue%_MyAppendedValue"
goto:eof |
Output example:
myVar 1 =MyOriginalString
local is set here
The NAME of the passed argument is:myVar
The VALUE of the passed argument is:MyOriginalString
local is ended here
myVar 2 =MyOriginalString_MyAppendedValue |
No comments:
Post a Comment