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

Sample code
plus

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

Sample Code to Edit a Massive Number of Files

Keywords: 	  massiveedit.wbt

Question:

This program will find all the html files in one directory (minor change to srchInit for a whole directory tree) and change all occurances of cat to poodle.

Edited files are saved with a .edit extension instead of .html as it really is not a good idea to destroy your source files until you are 120% happy with the results.

Answer:

thedir="D:\WorldWideSchool\LibraryMasters\zzz\"
themask="*.html"
outmask="*.edit"

infind="cat"
outReplace="poodle"



AddExtender("wsrch34i.dll") ; File Searcher Extender
BoxOpen("Init",0)
handle=SrchInit(thedir,themask,"","",0)

count=0
While 1
   infilename=SrchNext(handle)
   If infilename=="" Then Break
   count=count+1
   outfilename=FileMapName(infilename,StrCat(thedir,outmask))
   BoxTitle(count)
   BoxText(outfilename)
   GoSub editonefile
EndWhile

SrchFree(handle)
Message("All","Done")
Exit




;===================================cut===============================

:editonefile
      foundone=0
      insize=FileSize(infilename)
      outsize=insize*2+10000   ; Guess at how big output buffer needs to be


      inbuffer= BinaryAlloc(insize)
      outbuffer=BinaryAlloc(outsize)
      BinaryRead(inbuffer, infilename)

      ;BIG BIG BIG IMPORTANT ASSUMPTION
      ;THIS CODE ***ASSUMES*** "infind" will NOT occur at beginning of file

      outptr=0
      inptr=0
      While 1
           tempptr=BinaryIndexNc(inbuffer,inptr,infind,@FWDSCAN)
           If tempptr==0 Then Break    ; code simplification  See above
         foundone=1 
           BinaryCopy(outbuffer,outptr,inbuffer,inptr,tempptr-inptr)      
           outptr=outptr+tempptr-inptr
           BinaryPokeStr(outbuffer,outptr,outReplace)
           outptr=outptr+StrLen(outreplace)
           inptr=tempptr+StrLen(infind)
      EndWhile
  

      BinaryCopy(outbuffer,outptr,inbuffer,inptr,insize-inptr)


      If foundone==1 Then BinaryWrite(outbuffer,outfilename)
      BinaryFree(inbuffer)
      BinaryFree(outbuffer)
      Return

;===================================cut===============================

Article ID:   W13753
Filename:   Massive Edit for Many Files.txt
File Created: 2003:05:28:10:22:42
Last Updated: 2003:05:28:10:22:42