Code: Select all
; count all lines in several files
EnableExplicit
#gadget1=0
Structure prgdata
filename.s
totallines.l
blanklines.l
remlines.l
EndStructure
Global Dim prg.prgdata(30) ; totals in prg(0)\
Global totalprgs
Define w,h,cnt,filename$
Procedure ReadaFile(filecnt)
Define lne.s
If ReadFile(0,prg(filecnt)\filename)
While Not Eof(0)
lne=ReadString(0)
prg(filecnt)\totallines+1
If Len(Trim(lne))=0 : prg(filecnt)\blanklines+1 : EndIf
If Left(Trim(lne),1)=";" : prg(filecnt)\remlines+1 : EndIf
Wend
Else
MessageRequester("Reading file...notfound",prg(filecnt)\filename+Str(filecnt))
EndIf
prg(filecnt)\totallines-4 ; take out pb inserted lines
prg(filecnt)\remlines-4 ; take out pb inserted lines
AddGadgetItem(#gadget1,-1,Str(filecnt)+" File name: "+GetFilePart(prg(filecnt)\filename))
AddGadgetItem(#gadget1,-1," Total number of lines in this file: "+Str(prg(filecnt)\totallines))
AddGadgetItem(#gadget1,-1," Total number of blanklines in this file: "+Str(prg(filecnt)\blanklines))
AddGadgetItem(#gadget1,-1," Total number of comment lines in this file: "+Str(prg(filecnt)\remlines))
EndProcedure
w=640
h=400
OpenWindow(1,0,0,w,h,"Lines in program(s)",#PB_Window_SystemMenu | #PB_Window_WindowCentered)
filename$=OpenFileRequester("File(s) to count...","MyFile.pb","",0,#PB_Requester_MultiSelection)
If filename$
ListViewGadget(#gadget1,0,0,w,h) ; for showing data
totalprgs=1
prg(totalprgs)\filename=filename$ ;: Debug prg()
While filename$
totalprgs+1
filename$ = NextSelectedFileName()
prg(totalprgs)\filename=filename$ ;: Debug prg()
Wend
totalprgs-1
;read lines in each file
For cnt=1 To totalprgs
ReadaFile(cnt)
prg(0)\totallines+prg(cnt)\totallines
prg(0)\blanklines+prg(cnt)\blanklines
prg(0)\remlines+prg(cnt)\remlines
AddGadgetItem(#gadget1,-1,"")
Next
AddGadgetItem(#gadget1,-1,"Total lines in all files: "+Str(prg(0)\totallines))
AddGadgetItem(#gadget1,-1,"Total blank lines in all files: "+Str(prg(0)\blanklines))
AddGadgetItem(#gadget1,-1,"Total rem lines in all files: "+Str(prg(0)\remlines))
EndIf
Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow
End