Wilson WindowWare Tech Support

WinBatch WinBatch+Compiler WebBatch
Home | Tech Database | Tech BBS | White Papers | Purchase


Sample Code to Edit a Massive Number of Files

Keywords: 	  massiveedit.wbt

Question:

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

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:

;debug(1)
thedir="D:\WorldWideSchool\LibraryMasters\zzz\"
themask="*.html"
outmask="*.edit"

infind="Recipes:Desserts I : P"
outReplace="Recipes:Desserts I ~ P"



AddExtender("wsrch32i.dll")
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