Search code examples
functionbatch-filereturnargs

Can't set return value in batch file


I'm trying to get a batch file containing a simple function that get parameter/s and return values. Actually i'm not able to do this!

test.bat:

@echo off
setlocal EnableDelayedExpansion

set arg=bar
echo my args %*
echo my new value %arg%

endlocal&set %~1=%arg%

main.bat:

@echo off
setlocal EnableDelayedExpansion

set var1=foo
echo Old value: %var1%
call test.bat %var1%
echo New value: %var1%

This is what the console return:

Old value: foo
my args: foo
my new value: bar
New value: foo

Solution

  • In main.bat you need to echo the value content of %foo% not the value content of %var1%:

    Change:

    echo New value: %var1%
    

    To:

    Echo New value: !%var1%!