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

ECHO file names containing '&'

50 views
Skip to first unread message

Alexander Mueller

unread,
May 16, 2013, 8:03:33 AM5/16/13
to
Hi

In a batch file I have a line like

FOR %%A in (%1) do echo %%~A

supposed to remove the possibly surrounding double quotes from the
first parameter.
When the file's name passed in as %1 is e.g. 'Butch & Spencer.txt"

I get: unkown command 'Spencer.txt'

OK I know '&' is kinda an operator for command concatenation
with high priority, so the error dumped out is consistent.

Is there a way to remove the double quotes in a FOR-loop
that can handle parameters containing '&'?

Thx
Alex

foxidrive

unread,
May 16, 2013, 8:37:01 AM5/16/13
to
Commonly you would use this with "%~1" quoted to remove any surrounding quotes, and then quote the term.

FOR %%A in ("%~1") do echo "%%A"



--
foxi

frank.w...@gmail.com

unread,
May 16, 2013, 10:25:47 AM5/16/13
to
From Alexander Mueller :
>In a batch file I have a line like
>FOR %%A in (%1) do echo %%~A


>Is there a way to remove the double quotes in a FOR-loop
>that can handle parameters containing '&'?

With your query as stated:

FOR %%A in ("%~1") do ECHO %%~A

Quotes may exist so it is best to dequote (with the tilde: ~) to avoid ""double quoting"".

But FOR is not necessary for ECHO:

ECHO;%~1
ECHO(%~1

Use the second form if %~1 might contain "/?".

Frank

Tom Del Rosso

unread,
May 20, 2013, 6:32:09 PM5/20/13
to
If a literal string with & is substituted...

echo(aaaaa&bbbbb

it returns...

aaaaa
'bbbbb' is not recognized as an internal or external command,
operable program or batch file.


--

Reply in group, but if emailing remove the last word.


frank.w...@gmail.com

unread,
May 21, 2013, 8:03:54 AM5/21/13
to
From "Tom Del Rosso" :
>frank.w...@gmail.com wrote:
>> ECHO;%~1
>> ECHO(%~1

>If a literal string with & is substituted...

>echo(aaaaa&bbbbb

>it returns...

>aaaaa
>'bbbbb' is not recognized as an internal or external
>command,
>operable program or batch file.


True, thanks; I lost track of the problem and I didn't
perform a simple test which would have caught my error.
I'll blame it on sending it from an Android.

Frank

Tom Del Rosso

unread,
May 21, 2013, 10:38:21 PM5/21/13
to
It should be possible to do a substitution like

set "name=%name:&=^&%"

jeb

unread,
Jun 5, 2013, 5:12:45 AM6/5/13
to
You have nearly the solution

FOR %%A in ("%~1") do echo %%~A

This works, as %~1 stripps quotes from the input, the surrounding quotes "%~1" avoids problems with special characters, and the %~A remove then again the quotes before echoing the text
0 new messages