Reputation: 3958
I have a subroutine which uses parameter to return a string. As part of my logging I want to display the returned parameter value before I exit the routine.
Easier to explain in code!
@ECHO OFF
SET MYVAL=1
ECHO Before MYVAL=%MYVAL%
CALL :SETMYVAL MYVAL
ECHO After MYVAL=%MYVAL%
PAUSE
EXIT /b 0
:SETMYVAL
SET "%~1=2"
ECHO SETMYVAL called, returning %~1=%~1
GOTO :EOF
The above code produces:
Before MYVAL=1
SETMYVAL called, returning MYVAL=MYVAL
MYVAL=2
Press any key to continue . . .
But I want the last line ofthe subroutine to output
SETMYVAL called, returning MYVAL=2
Any ideas (without moving the line, or using extensions or delayed expansion)
Upvotes: 2
Views: 104
Reputation: 3958
Thanks foxidrive, the best I could come up with is:
:SETMYVAL
SET "%~1=2"
ECHO ECHO %%%~1%%>"%TEMP%\SETMYVAL.bat"
FOR /F "tokens=*" %%A IN ('CALL "%TEMP%\SETMYVAL.bat"') DO (CALL :SETMYVALFROMFILE %%A)
ECHO SETMYVAL called, returning %~1=%MYTEMP%
GOTO :EOF
:SETMYVALFROMFILE
SET MYTEMP=%~1
GOTO :EOF
Nasty eh?
Upvotes: 0
Reputation: 41234
Your case is artificial, so you may not get a real life solution.
@ECHO OFF
SET MYVAL=1
ECHO Before MYVAL=%MYVAL%
CALL :SETMYVAL MYVAL
ECHO After MYVAL=%MYVAL%
PAUSE
EXIT /b 0
:SETMYVAL
SET "val=2" & call SET "%~1=%%val%%"
ECHO SETMYVAL called, returning %~1=%val%
GOTO :EOF
Upvotes: 1
Reputation: 57252
ECHO SETMYVAL called, returning %~1=2
This should do it....
Or
ECHO SETMYVAL called, returning %~1=%MYVAL%
or
call ECHO SETMYVAL called, returning %~1=%%%~1%%
Upvotes: 1