Command Prompt with Delayed Expansion Problems

  • Thread starter =?ISO-8859-1?Q?Allan_Bo_J=F8rgensen?=
  • Start date
?

=?ISO-8859-1?Q?Allan_Bo_J=F8rgensen?=

Hi,

I'm using W2K SP4 and I'm trying to pass an argument to a program from a
batch-file, that runs in a command prompt with delayed expansion
activated (with /V). The problem is that I wish to have one of these
parameters contain an exclamation point, but I can not figure out how to
do this. I tried with a caret before it and with a double exclamation
point, since this would seem logical from how I understand that the
percent character is treated. It just seems to get "filtered away" no
matter what.

The weird thing is that I can do this directly from the command line
with no problems - it's just inside a batch file that it fails.

You can reproduce the problem simply by making a small bat-file (I'll
call it testset.bat) containing just @SET AA=a!b, and then running

testset & set AA

from the command line. When you use delayed expansion this gives AA=ab
as output, whereas not using delayed expansion gives AA=a!b as output.
I'd very much like to know how to make it give AA=a!b with delayed
expansion ON.

ANY idea would be welcome - thanks in advance,

Allan Bo Jørgensen
 
P

Pegasus \(MVP\)

Allan Bo Jørgensen said:
Hi,

I'm using W2K SP4 and I'm trying to pass an argument to a program from a
batch-file, that runs in a command prompt with delayed expansion
activated (with /V). The problem is that I wish to have one of these
parameters contain an exclamation point, but I can not figure out how to
do this. I tried with a caret before it and with a double exclamation
point, since this would seem logical from how I understand that the
percent character is treated. It just seems to get "filtered away" no
matter what.

The weird thing is that I can do this directly from the command line
with no problems - it's just inside a batch file that it fails.

You can reproduce the problem simply by making a small bat-file (I'll
call it testset.bat) containing just @SET AA=a!b, and then running

testset & set AA

from the command line. When you use delayed expansion this gives AA=ab
as output, whereas not using delayed expansion gives AA=a!b as output.
I'd very much like to know how to make it give AA=a!b with delayed
expansion ON.

ANY idea would be welcome - thanks in advance,

Allan Bo Jørgensen

You can usually avoid delayed expansion by placing your
code into a subroutine inside the batch file. Post your code
here if you need further details.
 
?

=?ISO-8859-1?Q?Allan_Bo_J=F8rgensen?=

Pegasus said:
You can usually avoid delayed expansion by placing your
code into a subroutine inside the batch file. Post your code
here if you need further details.

Basically all I want it to do is this:

...\tools\texts.exe Param1 Param2 Param3 Param!WithExclamation

The batch file contains LOTS of other stuff, and some of that requires
delayed expansion to work. As a dirty trick I've placed the line above
in its own batch-file in the same directory as the main batch file like
this (named copyTexts.bat)

%1\..\tools\texts.exe Param1 Param2 Param3 Param!WithExclamation

and then I call it with

cmd /v:blush:ff /c %CD%\copytexts.bat %CD%

This does NOTHING to improve readability and maintenance. It does work,
however :) If I understood you correctly, there's a better way?
 
P

Pegasus \(MVP\)

Allan Bo Jørgensen said:
Basically all I want it to do is this:

..\tools\texts.exe Param1 Param2 Param3 Param!WithExclamation

The batch file contains LOTS of other stuff, and some of that requires
delayed expansion to work. As a dirty trick I've placed the line above
in its own batch-file in the same directory as the main batch file like
this (named copyTexts.bat)

%1\..\tools\texts.exe Param1 Param2 Param3 Param!WithExclamation

and then I call it with

cmd /v:blush:ff /c %CD%\copytexts.bat %CD%

This does NOTHING to improve readability and maintenance. It does work,
however :) If I understood you correctly, there's a better way?


Let's have an example where delayed expansion is mandatory!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top