JJ
unread,Feb 1, 2018, 12:49:51 PM2/1/18You do not have permission to delete messages in this group
Sign in to report message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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]