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

Problem passing `&` character to a local CALL for file output

41 views
Skip to first unread message

JJ

unread,
Feb 1, 2018, 12:49:51 PM2/1/18
to
I need to create a code for generating duplicated lines from a variable text
into a file. So I use a loop in a local batch label and CALL it by passing
the number of lines to make, and the text to duplicate. The code i.e.:

[code]
@echo off
setlocal
rem.>temp
call :emit 2 "abc ^^^& xyz"
echo.
echo result:
type temp
del temp
goto:eof

:emit
echo count=%1; text=%2
set a=%1
:loop
if %a% leq 0 goto :eof
echo generating...
>>temp echo %~2
set/a a=a-1
goto loop
[/code]

The above example batch file is supposed to create the `temp` file with two
duplicate lines of: abc & xyz

However, it always fails when ECHOing the given parameter because the `&`
character isn't escaped - no matter how many `^` escape characters I use in
the parameter.

As a workaround, I replaced the use of CALL's second parameter with a
variable (see below). It works, but I want to know if there's a solution for
the above problem.

[code]
@echo off
setlocal
rem.>temp
set t=abc ^^^& xyz
call :emit 2
echo.
echo result:
type temp
del temp
goto:eof

:emit
echo count=%1; text=%t%
set a=%1
:loop
if %a% leq 0 goto :eof
echo outputing...
>>temp echo %t%
set/a a=a-1
goto loop
[/code]
0 new messages