home bbs files messages ]

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

   alt.msdos.batch      Fun with MS-DOS batch files      42,547 messages   

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

   Message 41,801 of 42,547   
   Herbert Kleebauer to Jos@noreply.nl   
   Re: Search for patern in HTML and return   
   11 Dec 19 01:11:51   
   
   From: klee@unibwm.de   
      
   On 10.12.2019 18:10, Jos@noreply.nl wrote:   
      
   > I want to search this line:   
   >   
   > 49.99   
   >   
   > for the text   
   >   
   > ""   
   >   
   > and return what comes after this text.   
   > So the script should return:   
   >   
   > "49"   
   >   
   > Maybe there should be a delimiter, so in this case the end of the   
   > search should be:   
   >   
   >    
   >   
   > Is this possible to achive in a script?   
      
   Batch code surely isn't the proper language to do this. But   
   just for fun I tried it. But it is extremly slow, so it is   
   of no practical use.   
      
      
      
      
   :: search in infile for the text "head" (given in hex) and   
   :: write all following characters to outfile until the text "tail"   
   :: (also given in hex) is found   
      
   @echo off   
   setlocal enabledelayedexpansion   
      
   set infile=test.htm   
   set outfile=test.txt   
      
   set tmp0=_tmp0.tmp   
   if exist %tmp0% del %tmp0%   
      
   set head=3c 21 2d 2d 20 72 65 61 63 74 2d 74 65 78 74 3a 20 35 32 20 2d 2d 3e   
   set tail=3c 21 2d 2d 20 2f 72 65 61 63 74 2d 74 65 78 74 20 2d 2d 3e   
      
   call :sub2 head1 %head%   
   call :sub2 tail1 %tail%   
      
   call :fopen %infile%   
   echo %infile% opened as input file   
      
   :::::::::::::::::::::::::::::::::::::::::::::::::::::   
      
   set n=-1   
      
   :l1   
   set /a n=n+1   
   call :getc_hex char %n%   
   if -1==%char% echo text not found & goto :end   
   if not %head1%==%char%  goto :l1   
      
   echo testing character %n% of %fsize%   
      
   set /a m=n   
   set found=1   
   for %%i in (%head%) do call :sub1 %%i   
   if %found%==0 goto :l1   
      
   echo head found   
   set /a n=m-1   
      
   :l2   
   set /a n=n+1   
   call :getc_hex char1 %n%   
   if -1==%char1% echo text not found & goto :end   
   if  %tail1%==%char1%  goto :l4   
   echo %char1%>>%tmp0%   
   goto :l2   
      
   :l4   
   echo testing character %n% of %fsize%   
      
   set /a m=n   
   set found=1   
   for %%i in (%tail%) do call :sub1 %%i   
   if %found%==1 goto :l3   
   echo %char1%>>%tmp0%   
   goto:l2   
      
   :l3   
   echo tail found   
      
   :: convert hexfile tmp1 to binary outfile   
   certutil -f -decodehex %tmp0% %outfile% >nul   
      
   :end   
   if exist %tmp0% del %tmp0%   
   call :fclose   
   goto :eof   
      
      
   :sub1   
   if %found%==0 goto :eof   
   call :getc_hex char %m%   
   if not %1==%char% set found=0 & goto:eof   
   set /a m=m+1   
   goto:eof   
      
   :sub2   
   set %1=%2   
   goto :eof   
      
      
      
      
   :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   
   :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   
   :fopen   
   :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   
   set tmp1=_tmp1.tmp   
   set tmp2=_tmp2.tmp   
   set fsize=%~z1   
   set cache=-1   
   certutil  -f -encodehex %1 %tmp1% 4 >nul   
   set n=0   
   if exist %tmp2% del %tmp2%   
      
   for /f "tokens=1-16" %%a in (%tmp1%) do (   
     set /p =%%a%%b%%c%%d%%e%%f%%g%%h%%i%%j%%k%%l%%m%%n%%o%%p>%tmp2%   
     set /a n=n+1   
     if !n!==64 echo.>>%tmp2% & set n=0)   
      
   goto :eof   
      
   :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   
   :fclose   
   :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   
   del %tmp1%   
   del %tmp2%   
   goto :eof   
      
      
   :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   
   :getc_hex   
   :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   
   if %2 GEQ %fsize% set /a %1=-1 & goto :eof   
   set /a i=%2/1024 & set /a j=2*(%2%%1024)   
   if %i%==%cache% goto :skip   
   echo                                       read cache line %i%   
   set cache=%i%   
   set skip=   
   if %i% GEQ 1 set skip="skip=%i%"   
   for /f %skip% %%i in (%tmp2%) do (   
      set line=%%i & goto :skip)   
   :skip   
   call set %1=%%line:~%j%,2%%   
   goto :eof   
      
   --- 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