home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   alt.msdos.batch.nt      Fun with Windows NT batch files      68,980 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 67,148 of 68,980   
   JJ to All   
   Problem passing `&` character to a local   
   02 Feb 18 00:49:51   
   
   From: jj4public@vfemail.net   
      
   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]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca