You are not logged in.
I have a function in a batch file like so
@echo off
setlocal EnableDelayedExpansion
call :myFunc input1
:myFunc
set input=!%~1:str1=str2!
echo !input!
At the line
set input=!%~1:str1=str2!
I want to substitute str1 in input1 with str2. But I am not able to get the syntax right to use %~1 here.
If I use another temporary variable it works, but I do not want to resort to using an extra variable.
Working code
@echo off
setlocal EnableDelayedExpansion
call :myFunc input1
:myFunc
set temp=%~1
set input=!temp:str1=str2!
echo !input!
Offline
You can't perform substitutions in %1 (etc); the only solution is to assign a variable as you have done in your working example.
Last edited by bluesxman (22 Nov 2019 13:47)
cmd | *sh | ruby | chef
Offline
This works here:
@echo off
setlocal EnableDelayedExpansion
set "input1=This str1 will change"
call :myFunc input1
goto :EOF
:myFunc
set input=!%~1:str1=str2!
echo !input!
Perhaps your problem is a different one?
Antonio
Offline
This works here:
@echo off setlocal EnableDelayedExpansion set "input1=This str1 will change" call :myFunc input1 goto :EOF :myFunc set input=!%~1:str1=str2! echo !input!
Perhaps your problem is a different one?
Antonio
I was using wrong way to pass the value to the function call.
I was using
call :myFunc %input1%
which you have correctly used as
call :myFunc input1
.
If I use percentage sign, I need to use a temp variable inside the function else it doesn't work.
Thanks.
Offline