简体   繁体   中英

How to pass parameter ending with “!” to another bat file?

I have two bat files and i need to pass three parameter from one bat file to the other. However if the parameter ends in an "!" the parameter is not receieved

caller.bat

impl.bat param1 param2! param3

impl.bat

echo %1
echo %2
echo %3

Expected Result after running caller.bat :

param1
param2!
param3

Actual Result after running caller.bat :

param1
param2
param3

Can anyone help on how i can achieve the actual result?

As the other answers said, it's possible to disable delayed expansion, but it isn't necessary to display an exclamation mark.

It's important that you get the exlamation mark (and other special chaacters) into a variable.
Then you can use it perfectly with delayed expansion

Sample

@echo off
set "param1=%~1"
set "param2=%~2"
setlocal EnableDelayedExpansion
echo Works: !param1! !param2!
echo FAILS: %param1% %param2%

Calling this with

myBatch.bat arg1! "arg2!<>&"

The advantage of delayed expansion is that any content can be handled without side effects, as the expanded content will not be parsed anymore.
Percent expansion will fail in many cases, as the content will be parsed also for special characters

In fact, the explanation mark is received by the destination batch file, but is discarded when the echo command is processed. You can see this if you have echoing turned on. (However, if you use the call command to run the batch file, the explanation mark is not passed to it.)

The explanation mark is only discarded if delayed expansion is enabled. You can correct it by turning delayed expansion off temporarily:

setlocal disabledelayedexpansion
impl.bat param1 param2! param3

You can also work around it by quoting the special character with the caret, though since the argument goes through several levels of processing you have to quote it multiple times:

impl.bat param1 param2^^^^^! param3

or

call impl.bat param1 param2^^^^^^^^^^! param3

This approach may not be reliable, as it depends on what the child is doing with the argument. It is preferable to turn delayed expansion off, although that won't work if the child turns it back on again.

One of the few cases where the solution is to disable delayed expansion. I assume you are enabling it either when launching your cmd.exe , or as a setlocal line in your batch file. Either way, the shortest answer is to stop turning that on - by default cmd parameters do exactly what you want.

If you rely on delayed expansion elsewhere and can't turn it off globally, then you can use setlocal to disable it for a chunk of code.

echo %1

setlocal disabledelayedexpansion
echo %2
endlocal

echo %3

Just note that anywhere you expand a variable with the bang symbol, delayed expansion must be turned off. This includes if you set it to a variable and use that variable later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM