As I understand the OP, the answers of Aacini and stackoverflow are wrong.
A deferred expansion solution (Aacini) can only work if num defined before the batch starts, otherwise %mnm% will never expand.
C:\>set "num=" C:\>myBatch.bat user%num%
user%num% output user%num%
Another solution (stackoverflow) works a little better, but fails when num defined
I added a second set num=2 to demonstrate it
@ECHO off SET param=%1 SET num=1 CALL SET x=%param% ECHO %x% set num=2 CALL SET x=%param% ECHO %x%
A call two times shows the problem
C:\>myBatch user%num% user1 user2 C:\>myBatch user%num% user2 user2
In the second run, you got 2 two times, since the result is fixed when the package starts.
A good idea is to echo the %1 content to see if the format string is actually present or if %num% already expanded.
As I understand OP (maybe I don’t understand this!), The question is to use placeholder,
but this is difficult with percentages, since it only works on the command line (not from another batch), and only works if the variable is not defined at that moment.
Since, if a variable is defined, then %num% will be expanded immediately, and the contents of %1 is user2 , not user%num% (provided that num = 2).
The fact that it sometimes works is only a side effect of the cmd-string analyzer, because it does not delete the undefined variable (as inside a batch file), and the extension of the undefined variable will not change in any way.
echo "%thisIsUndefined%" - from cmd-line this outputs `"%thisIsUndefined%"` echo "%thisIsUndefined%" - from a batch file this outputs `""`
But as a side effect, there is no way to avoid the percent sign in the context of the cmd line.
There is only a workaround for pseudo security.
mybatch user%num^%
This does not really speed up the percentage, but basically there will be no variable named num^ .
Then the stackoverlow solution will work with:
myBatch user%num^%
But I would prefer the delayed extension mentioned by Aachini.
You would call a package and then exclamation points rather than percentages, as a rule, this works well, since delayed extension is disabled by default.
myBatch user!num!
The party itself will look like this:
@echo off set "param=%1" Setlocal EnableDelayedExpansion set num=1 echo %param% set num=2 echo %param%