#1 20 Nov 2019 09:33

ontherocks
New Member
Registered: 20 Nov 2019
Posts: 1

Use function parameters in Delayed Expansion

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

#2 22 Nov 2019 13:47

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,101

Re: Use function parameters in Delayed Expansion

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

Board footer

Powered by FluxBB