WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

How To
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Winbatch Findstr Equivalent


Question:

I use the findstr in DOS quite a bit to output all occurances of a string to a different file. For example:
findstr /i mystuff *.* >C:\myfile.txt
This runs very quick. Is there a similar way to do the same thing in Winbatch using binary functions?

Answer:

Yes, binary buffers would be the way to find just the lines that contain matching strings.

While 1    ;startofdialog   
   MyDialogFormat=`WWWDLGED,5.0`   
   MyDialogCaption=`Winbatch GREP Utility`   
   MyDialogX=287   
   MyDialogY=45
   MyDialogWidth=150   
   MyDialogHeight=99   
   MyDialogNumControls=5   
   MyDialog01=`64,16,64,DEFAULT,EDITBOX,fname,"c:\bin\eaprint.txt"`
   MyDialog02=`12,16,38,DEFAULT,STATICTEXT,DEFAULT,"Enter Filename"`
   MyDialog03=`12,42,38,DEFAULT,STATICTEXT,DEFAULT,"Search String"`
   MyDialog04=`64,42,64,DEFAULT,EDITBOX,str,""`
   MyDialog05=`40,70,64,DEFAULT,PUSHBUTTON,DEFAULT," Search",1`   
   
   ButtonPushed=Dialog("MyDialog")      
   
   If buttonpushed == 1
      ; Find line number of line in file where string occurs
      ;fname = askline("Filename", "Enter Filename", "")
      ;str = askline("String", "Enter search string", "")
      fs1 = FileSize( fname )      
      binbuf1 = BinaryAlloc( fs1 )
      a1 = BinaryRead( binbuf1, fname)      
      a = 0      
      count = 0
      ClipPut("")            
      
      While  1    ;searchagain
         If a > 0 Then a = a + 1
            a = BinaryIndexNc( binbuf1, a, "%str%", @FWDSCAN ) ; find the string
            ; Single out beginning of line which contains the string, skipping over the @crlf.
            linebegin = BinaryIndex( binbuf1, a, @CRLF, @BACKSCAN) + 2
            ;
            ; Search for the end of the line which contains the string.
            lineend = BinaryIndex( binbuf1, a, @CRLF, @FWDSCAN)
            linelen = lineend-linebegin+1                        
            
         If a > 0
            c = BinaryStrCnt( binbuf1, 0, a, @CRLF) + 1
            count = count + 1            ; Extract the line with the string.
            linedata=BinaryPeekStr(binbuf1, linebegin, linelen)
            ClipAppend(StrCat("Line %c%: ",linedata, @CRLF))
            ; Message("Hmmm", "%str% found on line %c%")
            Continue    ; from searchagain         
         Else            
            If count == 0
               Message("GREP WBT", "%str% not found in file")            
            EndIf
         EndIf         
         Break      
      EndWhile   ; from searchagain
      ; free up the memory...      
      binbuf1 = BinaryFree(binbuf1)      
      ; format the output to the clipboard...      
      clipdata = ClipGet()
      clipdata = StrCat("Found %count% matches...", @CRLF, clipdata)
      ClipPut("")      
      ClipPut(clipdata)      
      Message("GREP WBT", "%str% found %count% times")   
   EndIf
EndWhile   ; from startofdialog 

User Reply:

Thanks, that is what I was looking for!
Article ID:   W16486
File Created: 2005:02:18:12:20:54
Last Updated: 2005:02:18:12:20:54