1

Please consider the following very simple batch script (the file is named test.cmd):

@echo off

set "var1=%~1"
echo %var1%

The script should be called with one command line parameter, should assign the string which is contained in that parameter to a variable, and should output the variable.

As expected, I get an error message when I call this script with a command line parameter which contains an ampersand (&):

C:\Batch>test "a&b"
a
'b' is not recognized as an internal or external command,
operable program or batch file.

The reason for this has been discussed in some other questions here and elsewhere, for example that one; the usual remedy is to use delayed expansion. So I changed the script accordingly:

@echo off

setLocal enableDelayedExpansion

set "var1=%~1"
echo !var1!

Now it works with the parameter from before:

C:\Batch>test "a&b"
a&b

But there is a new problem. When the command line parameter contains an exclamation mark (!), it will be dropped from the output:

C:\Batch>test a!b
ab

This behavior also has been discussed at several places, for example here; the crucial thing to note is that dropping the exclamation mark happens during the assignment, not during the echo.

Despite a lot of research, I did not find a question here which provided an elegant solution for both problems at once. That is, is there an elegant way to assign a command line parameter to a variable when that parameter contains an ampersand AND an exclamation mark?

It seems that I need the delayed expansion to treat the ampersand correctly, but this destroys the exclamation mark.

The only solution I currently see is to not use delayed expansion and to add code to explicitly quote all ampersands in the input string. This would be so ugly that I seriously think that I am missing something here.

As a side note, the reason for the problem actually seems to be that there (IMHO!) is no way to get the command line parameter in a delayed-expanded fashion. The syntax for the first parameter is %~1, there is no such thing as !~1.

1 Answer 1

3

Move the setLocal enableDelayedExpansion after the the set„ that's all.

@echo off

set "var1=%~1"
setLocal enableDelayedExpansion
echo !var1!
2
  • You are right. Sorry for having missed this one. One of my tests has gone wrong, so I thought that it actually was the assignment (set ...) which threw the errors if the command line parameter contained an ampersand. Now I have a problem: On one hand, given that I managed to mess up that one test which lead me to ask, my question now is kind of useless, so I'd like to delete it. On the other hand, I'd like to credit you for the correct answer. What should I do?
    – Binarus
    Commented Dec 29, 2019 at 10:31
  • @Binarus If you think the question is useless, then delete it, a few credits shouldn't be a reason to leave it open
    – jeb
    Commented Dec 29, 2019 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.