sambul35 wrote:I want to keep using the same variable name "opt" throughout the batch - before and after calling any function.
First of all, thank you for the illustrative code. I, for one, find that this the first time in this topic to have a chance at understanding what you wanted. Now, perhaps the better minds can offer you more than pointers to sources for the rules.
Since you do not seem to be accepting values for OPT in the call, there must be an action plan if the string "%opt%" as the first argument. You do have an awareness of the name OPT because you want to use that name and want to set it to a value if that string is inputted. After calling :fun0 and :fun1 you have used that name to check the value.
If the variable OPT is undefined, the quoted string is passed from the command line. If OPT has a value of ANYTHING, then the passed value is "ANYTHING" not the string "%OPT%". These rules do not apply within a batch script, because substitution always takes place and "%opt%" will change to "" after substituting an undefined value. The function :fun0 cannot get at the value OPT, only at the value of OPT, as could be seen with ECHO ON before the CALL.
Any attempt to Set a substring will operate on the value 't' in your example, not the desired string. Incidentally the statement - set "var2"="%var1:~2,3%" - is not setting a value for var2 but for var2". The variable name is everything before the = symbol and var2 remains undefined. I also note that you access %opt% in the last ECHO in both :fun0 and :fun1. This works fine for :fun1, but :fun0 is doomed to return the previous value of 'opt' unless !opt! is used because the ECHO is compounded to the SET statement with '&', but stands alone in :fun1.
The previous comments were recovered from a draft created on Friday. I have done some experimentation in the interim.I have modified your illustrative code:
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "opt=t"
@echo on
call :fun0 %%%1%% ""
echo. & echo Result: %opt% !opt!
Set AnyVar
Set %AnyVar%
echo. & echo Result2: %opt2% !opt2!
@echo on
call :fun1 "%opt%" "opt"
echo. & echo Result: %opt% !opt!
echo. & echo Result2: %opt2% !opt2!
pause
:end
exit /b
:fun0
@echo off
echo. & echo One argument: & echo Originals: %~1 %opt%
set "AnyVar=%1"
echo Substituting: '!AnyVar!'
Set %AnyVar%=8 & echo Changing AnyVar does so change "%AnyVar%": !AnyVar! !opt!
exit /b
:fun1
@echo off
echo. & echo Two arguments: & echo Originals: %~2 %opt%
set "%~2=5"
echo Changing %%2 changes "opt": %~2 %opt%
(goto) 2>nul
exit /b
The magic expression is %%%~1%%. This causes "%opt%" to become %"%opt%"% when received by :fun0 and the onion is peeled by variable substitution. The %"% at the beginning and end become null because the environmental variable " is not defined and all that is left is opt, the desired result.
This expression allows a string other that opt and of unknown length to be introduced by an argument and that name will be set to a value and can be examined using AnyVar and manipulated using AnyVar or its own name.
John A.