Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using ampersand in a command-line parameter

681 views
Skip to first unread message

Rick Charnes

unread,
Feb 6, 2007, 12:06:27 PM2/6/07
to
One of the parameters to my command file is a string that includes an
ampersand (&) at some random location within the string. I also need to
grab this string for later use and assign it to a variable. However,
the command processor of course interprets ampersand as a special
character. When entering the string on the command line, I can escape
it with ^ as in "my^&string", but that string can't be stored into a
variable. Any thoughts on how to make this work? Maybe it's hopeless?
Thanks much.

foxidrive

unread,
Feb 6, 2007, 12:14:19 PM2/6/07
to

Any good?

@echo off
for /f "delims=" %%a in ('type command.file') do set variable="%%a"
echo %variable%

Rick Charnes

unread,
Feb 6, 2007, 1:04:48 PM2/6/07
to
In article <uqdhs2dtfu2qvl55v...@4ax.com>,
got...@woohoo.invalid says...

> @echo off
> for /f "delims=" %%a in ('type command.file') do set variable="%%a"
> echo %variable%
>
Sorry--I don't get it. I need to enter a string (whose value varies)
that includes an ampersand as a parm to my cmd file, then in the cmd
file copy that string/parameter into a variable. Maybe I'm not seeing
your solution? Not sure what the 'type command.file' refers to. Thanks
much.

Eman

unread,
Feb 6, 2007, 1:16:03 PM2/6/07
to

You can safely assign

set MyVar=%1

or (in the case of FOR)

set MyVar=%%A

but

set MyVar2=%MyVar%

will generate an error (since command interpreter first
evaluates %MyVar% to "real string", so that you get

set MyVar2=Boogie & Woogie

and then "'Woogie' is not recognized as an internal or
external command, operable program or batch file.").

But things are different in EnableDelayedExpansion mode.
The command

set MyVar2=!MyVar!

will succeed in spite of MyVar' contents. The only
fatal (?) thing will be exclamation mark in assignments
like "set MyVar=%1" or "set MyVar=%%A".

0 new messages