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,842 of 68,980   
   JJ to John Stockton   
   Re: Problem with file trimming in Batch   
   01 Dec 21 14:04:29   
   
   From: jj4public@gmail.com   
      
   On Tue, 30 Nov 2021 06:03:07 -0800 (PST), John Stockton wrote:   
   > I want to copy an HTML page file to another file, omitting both the   
   > heading+introduction and the tailpiece.  For test I use a line of four   
   > hashes as separator, and a test file :   
   >   
   > head   
   > ####   
   > want   
   > ####   
   > tail   
   >   
   > My non-working test code is in :   
   >   
   > @echo PRUNE.BAT JRS 2021-11-30+    ???   
   >   
   > @echo.   
   > @if not exist zzz\nul mkdir zzz   
   > @echo 1111   
   >   
   > @SET ZC=0   
   > @echo. Start >  ZZZ\ORIG.HTM   
   > @FOR /F "eol=; tokens=*" %%J IN (PRUNE.DAT) DO @(   
   >   @echo Comment line='%%J'  ZC=%ZC%   
   >   if [%ZC%] == [1] (   
   >     echo       '%%J'  >>  ZZZ\ORIG.HTM   
   >     )   
   >   if [%%J] == [####] (   
   >     set ZC=1   
   >     echo === %ZC%   
   >     )   
   >   @echo ++   
   >   )   
   >   
   > @echo 9999   
   >   
   > @echo. Ended >>  ZZZ\ORIG.HTM   
   > @echo.   
   >   
   > type ZZZ\ORIG.HTM   
   >   
   > @echo PRUNE.BAT ends.   
   >   
   > The apparent problem is that the line     set ZC=1    has no effect, so   
   > that the wanted part is not copied. ( I intend to change it to    set /A   
   > ZC=1+%ZC%       which should mean that the tail part is not copied).   
   > Subsequently I may use a line       as an invisible separator.   
   >   
   > What's wrong?   
      
   The easiest way is to use Delayed Expansion as other have mentioned, but it   
   can produce more severe problem if the source data includes `!` character.   
      
   Without Delayed Expansion, move the code that use variables which are   
   updated within the same or parent command groups, into a subroutine. e.g. in   
   your case, change it like below.   
      
   @echo PRUNE.BAT JRS 2021-11-30+    ???   
      
   @echo.   
   @if not exist zzz\nul mkdir zzz   
   @echo 1111   
      
   @SET ZC=0   
   @echo. Start >  ZZZ\ORIG.HTM   
   @FOR /F "eol=; tokens=*" %%J IN (PRUNE.DAT) DO @(   
     @echo Comment line='%%J'  ZC=%ZC%   
     if [%ZC%] == [1] (   
       echo       '%%J'  >>  ZZZ\ORIG.HTM   
     )   
     if [%%J] == [####] (   
       set ZC=1   
       call :DispZC   
     )   
     @echo ++   
   )   
      
   @echo 9999   
      
   @echo. Ended >>  ZZZ\ORIG.HTM   
   @echo.   
      
   type ZZZ\ORIG.HTM   
      
   @echo PRUNE.BAT ends.   
   goto :eof   
      
   :DispZC   
   echo === %ZC%   
      
   --- 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