Search tool for strings (pbide tool)

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 796
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

Search tool for strings (pbide tool)

Post by Zebuddi123 »

Hi to all Pb Ide Tool (how i use it) to search files for string/s (pattern match), shows which file/files have match`s. Left double click the file column to load the file into PB Ide --- if pb source etc, if txt file will load in default txt editor, search any file for any kind of string.

Fairly quick 12000 + .pb source files located on c:\ parsed & complete in 25s on my ssd.

Hope its of use to some, having received tons of code from the forum time to give some back :D

Compiler options
unicode
threadsafe

Pattern Matching txt, pdf, xml, pb, pbi etc leave blank to match all file types (*. added)

Code: Select all

; Zebuddi.; 4/7/2012

Enumeration 
	#Window_0
	#StatusBar_Window_0 
	#Button_dir
	#String_dir 
	#String_term 
	#Button_search 
	#ListIcon
	#Button_cancel
	#Text
	#String_mask
	#Frame3D
	#CheckBox_wholeword
	#CheckBox_sensitive
	#CheckBox_showdir
	#Window_ExportOptions
	#Window_ExportSearchString
	#Window_ExportAllWithPath
	#Window_ExportAsViewed
	#Window_DefaultFilename
	#Window_ChooseFilename
	#Window_ExportFileName
	#CheckBox_inccomments
EndEnumeration

Global title$= "Search Files/Dirs for Strings   X86                                                    "+FormatDate("%dd/%mm/%yyyy --- %hh:%ii", Date())
Global NewList FilesAndFolders.s(), tline.q, dir$, pattern$, level.l, f$, ccount, mask$
Global ListIcon_Tooltip, gotsum, st, filecount, tline, myfilesize, start_time, hits, ccount, timerthread, mythread, StatusBarDetails
Global NewList dlist$()
Global NewList ForExport$()
ProcedureDLL.s __FormatNumberFloat(Number.q) ;1000000===>1,000,000
	
	Buffer.s=Space(255)
	GetNumberFormat_(0, 0, Str(Number), 0, @Buffer, Len(Buffer)) ;API
	ProcedureReturn Buffer
EndProcedure

ProcedureDLL.s __CalcFileSizes(calcsize.i); calc size and return string from bytes
	If calcsize < 1024
		ProcedureReturn StrD(calcsize, 2)+" bytes"
	EndIf
	If calcsize >1024 And calcsize <1048576
		ProcedureReturn StrD(1024/calcsize, 2)+" Kilobytes"
	EndIf
	If calcsize >1048576 And calcsize<1073741824
		ProcedureReturn StrD(calcsize/1048576, 2)+" Megabytes"
	EndIf
	If calcsize >1073741824 And calcsize< 1099511627776
		ProcedureReturn StrD(1073741824/calcsize, 2)+" Gigabytes"
	EndIf
EndProcedure
Procedure.s bGetUserName()
	
	Username.s = Space(512) 
	nsize.l = 512 
	retour=GetUserName_(@Username, @nsize) 
	If retour=0 : Username="": EndIf ; If error return an Empty String
	ProcedureReturn Username
EndProcedure

Procedure.l SearchDirectory(dir$, pattern$,List  List.s(), level.l = 0) 
	Protected eName$ 
	NewList Dirs.s() 
	If (level = 0) 
		ClearList(List()) 
	EndIf 
	If Right(dir$, 1) <> "\"  
		dir$ + "\" 
	EndIf 
	If ExamineDirectory(0, dir$, "")
		While NextDirectoryEntry(0) 
			If DirectoryEntryType(0) = #PB_DirectoryEntry_Directory 
				eName$ = DirectoryEntryName(0)   
				If (eName$ <> ".") And (eName$ <> "..")   
					AddElement(Dirs())  
					Dirs() = eName$ + "\" 
				EndIf   
			EndIf 
		Wend  
		FinishDirectory(0)
		If ExamineDirectory(0, dir$, pattern$) 
			While NextDirectoryEntry(0)  
				eName$ = DirectoryEntryName(0) 
				If (eName$ <> ".") And (eName$ <> "..") 
					AddElement(List())  
					List() = dir$ + eName$    
					ccount+1
					Delay(1)
					SetWindowTitle(#Window_0,"Searching for Files   "+Str(ccount))
					If DirectoryEntryType(0) = #PB_DirectoryEntry_Directory  
						List() + "\"    
					EndIf   
				EndIf 
			Wend  
			FinishDirectory(0) 
		EndIf 
	EndIf
	If ListSize(Dirs())
		ForEach Dirs()  
			SearchDirectory(dir$ + Dirs(), pattern$, List(), level + 1) 
		Next
	EndIf 
	If (level = 0) 
		ForEach List()  
			List() = Mid(List(), Len(dir$) + 1, Len(List())) 
		Next 
		SortList(List(), 2) 
	EndIf
	If ListSize(List())>0
		ProcedureReturn 0
	Else
		ProcedureReturn -1
	EndIf
	totalfiles=ListSize(List())
EndProcedure

ProcedureDLL.s GGetUserName() ; Retrieves the user name wich launch the current thread
	Username.s = Space(512) 
	nsize.l = 512 
	retour=GetUserName_(@Username, @nsize) 
	If retour=0 : Username="": EndIf ; If error return an Empty String
	ProcedureReturn Username
EndProcedure

Procedure StatusBarTime(d)
	myfilesize=0
	start__time=ElapsedMilliseconds()
	Repeat
		StatusBarText(#StatusBar_Window_0,0,"Hit`s "+Str(hits)+" From "+Str(filecount)+" File`s")
		StatusBarText(#StatusBar_Window_0,1,"Number of Lines Read   "+__FormatNumberFloat(tline)+" ---  Total "+__CalcFileSizes(myfilesize)) 
		StatusBarText(#StatusBar_Window_0,2,"Time Taken"+Chr(10)+Str((ElapsedMilliseconds()-start_time)/1000)+" Sec`s")  
		Delay(250)
	ForEver
EndProcedure

Procedure ssearch(f)
	start_time=ElapsedMilliseconds()
	timerthread=CreateThread(@StatusBarTime(),1)
	StatusBarText(#StatusBar_Window_0,0,"Hit`s   ")
	StatusBarText(#StatusBar_Window_0,1,"Number of Lines Read   ") 
	StatusBarText(#StatusBar_Window_0,2,"Time Taken   ") 
	ClearGadgetItems(#ListIcon)
	a$="":t$="":hits=0:st=0:et=0:line=0:filecount=0
	dd$=GetGadgetText(#String_dir)
	ignorecomments.i=GetGadgetState(#CheckBox_sensitive)
	casesensitive=GetGadgetState(#CheckBox_sensitive)
	If Right(dd$,Len(dd$)-1)=":\"
		mresult=MessageRequester("Warning","You Have Elected To Search The Drive  "+UCase(dd$)+Chr(13)+Chr(13)+"This Could Take A long Time"+Chr(13)+"Depending On Your Computers SetUp"+Chr(13)+"Please Select Appropriate Selection To Continue",#PB_MessageRequester_YesNo|#MB_ICONWARNING)
		If mresult=#PB_MessageRequester_No
			ProcedureReturn 
		EndIf
	EndIf
	
	If GetGadgetText(#String_dir)>""    
		ClearGadgetItems(#ListIcon)   
		Delay(250)  
		ClearList(FilesAndFolders()) 
		If GetGadgetText(#String_term)>""   
			If GetGadgetText(#String_mask)=""
				mask$="*.*"
			Else
				mask$=GetGadgetText(#String_mask)
				If Left(mask$,2)<>"*."
					mask$=ReplaceString(mask$,"*","")
					mask$=ReplaceString(mask$,".","")
					mask$="*."+mask$
				EndIf
			EndIf
			gotsum=SearchDirectory(f$, mask$, FilesAndFolders(), 1)
			ResetList(dlist$())
			FirstElement(dlist$())
			;StartOfLinkedList(dlist$())
			ResetList(FilesAndFolders())
			FirstElement(FilesAndFolders())
			;StartOfLinkedList(FilesAndFolders())   
			st=ElapsedMilliseconds()    
			SetWindowTitle(#Window_0, "Working....") 
			t$=GetGadgetText(#String_term) 
			If GetGadgetState(#CheckBox_wholeword)=#PB_Checkbox_Checked
				t$=" "+t$+" "
				t$=LCase(t$)
			EndIf
			
			If gotsum >-1
				ForEach FilesAndFolders() 
					filecount+1      
					SetWindowTitle(#Window_0,"Working...  File Count ="+Str(filecount)+" "+GetFilePart(FilesAndFolders()))  
					line=0: a$=""
					If OpenFile(0,FilesAndFolders()) 
						myfilesize=myfilesize+FileSize(FilesAndFolders()) ; build up the amount of bytes read
						While Eof(0)=0    
							If casesensitive
								a$=ReadString(0,#PB_UTF8)
								dummy$=a$
							Else
								a$=ReadString(0,#PB_UTF8)
								dummy$=a$
								dummy$=LCase(dummy$)
							EndIf
							
							If ignorecomments
								If Left(dummy$,1)=";"
									a$=""
									dummy$=""
								Else
									If FindString(dummy$,";")
										dummy$=Mid(dummy$,1,(FindString(dummy$,";")-1))
									EndIf
								EndIf
							EndIf
							tline+1
							line+1    
							If FindString(dummy$,t$,1)   
								hits+1     
								AddElement(dlist$())
								dlist$()=FilesAndFolders()
								If GetGadgetState(#CheckBox_showdir)=1
									AddGadgetItem(#ListIcon, -1,"@ line "+Str(line)+Chr(10)+FilesAndFolders()+Chr(10)+dummy$)
									AddElement(ForExport$())
									ForExport$()="@ line "+Str(tline)+" "+FilesAndFolders()+" "+dummy$
								Else
									AddGadgetItem(#ListIcon, -1,"@ line "+Str(line)+Chr(10)+GetFilePart(FilesAndFolders())+Chr(10)+dummy$)
									AddElement(ForExport$())
									ForExport$()="@ line "+Str(line)+" "+FilesAndFolders()+" "+dummy$
								EndIf
								
							EndIf 
						Wend  
						CloseFile(0)       
					EndIf    
				Next  
				et=ElapsedMilliseconds()     
				SetWindowTitle(#Window_0,title$+"   ---   Search For Term "+t$+ "  Complete")   
			Else	
				MessageRequester("Mask or Extension Error","No Files Found with Extension of"+Chr(13)+Chr(13)+mask$)
			EndIf
			
		Else 
			MessageRequester("ERROR","Search string cannot be Empty!!!")  
		EndIf    
	Else 
		MessageRequester("ERROR","Directory string cannot be Empty!!!")  
	EndIf  
	ClearList(FilesAndFolders())
	killtimerthread= IsThread(timerthread)
	If killtimerthread
		KillThread(timerthread)
	EndIf
	
	killmythread=IsThread(mythread)
	If killmythread
		KillThread(mythread)
	EndIf
EndProcedure

Procedure GadgetBalloonToolTip(WindowNumber.l, GadgetNumber.l, Text.s)
	Protected Tooltip.l, Balloon.TOOLINFO
	Tooltip = CreateWindowEx_(0, "ToolTips_Class32", "", #WS_POPUP | #TTS_NOPREFIX | #TTS_BALLOON, 0, 0, 0, 0, WindowID(WindowNumber), 0, GetModuleHandle_(0), 0)
	SendMessage_(Tooltip, #TTM_SETTIPTEXTCOLOR, GetSysColor_(#COLOR_INFOTEXT), 0)
	SendMessage_(Tooltip, #TTM_SETTIPBKCOLOR, GetSysColor_(#COLOR_INFOBK), 0)
	SendMessage_(Tooltip, #TTM_SETMAXTIPWIDTH, 0, 180)
	Balloon\cbSize = SizeOf(TOOLINFO)
	Balloon\uFlags = #TTF_IDISHWND | #TTF_SUBCLASS
	If IsGadget(GadgetNumber)
		Balloon\hwnd = GadgetID(GadgetNumber)
		Balloon\uId = GadgetID(GadgetNumber)
	Else
		Balloon\hwnd = GadgetNumber
		Balloon\uId = GadgetNumber
	EndIf
	Balloon\lpszText = @Text
	SendMessage_(Tooltip, #TTM_ADDTOOL, 0, @Balloon)
	ProcedureReturn Tooltip
EndProcedure

Procedure ChangeGadgetBalloonToolTip(Tooltip.l, GadgetNumber.l, Text.s)
	Protected Balloon.TOOLINFO
	Balloon\cbSize = SizeOf(TOOLINFO)
	Balloon\uFlags = #TTF_IDISHWND | #TTF_SUBCLASS
	If IsGadget(GadgetNumber)
		Balloon\hwnd = GadgetID(GadgetNumber)
		Balloon\uId = GadgetID(GadgetNumber)
	Else
		Balloon\hwnd = GadgetNumber
		Balloon\uId = GadgetNumber
	EndIf
	Balloon\lpszText = @Text
	SendMessage_(Tooltip, #TTM_ADDTOOL, 0, @Balloon)
EndProcedure

Procedure FreeBalloonTooltip(Tooltip.l)
	DestroyWindow_(Tooltip.l)
EndProcedure 

Procedure OpenWindow_Window_0() 
	If OpenWindow(#Window_0, 488, 71, 1065, 513, title$, #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)  
		If CreateStatusBar(#StatusBar_Window_0, WindowID(#Window_0))  
			AddStatusBarField(200) 
			StatusBarText(#StatusBar_Window_0, 0, " ") 
			AddStatusBarField(465)
			StatusBarText(#StatusBar_Window_0, 1, " ")  
			AddStatusBarField(500)
			StatusBarText(#StatusBar_Window_0, 2, " ")  
		EndIf
		ButtonGadget(#Button_dir, 510, 5, 60, 25, "Directory")
		StringGadget(#String_dir, 5, 5, 500, 25, "")
		StringGadget(#String_term, 5, 30, 500, 25, "")
		ButtonGadget(#Button_search, 510, 30, 60, 25, "Search")
		ButtonGadget(#Button_cancel, 1000, 15, 60, 25, "Cancel")
		ListIconGadget(#ListIcon, 0, 145, 1065, 334, "Line", 100, #PB_ListIcon_AlwaysShowSelection|#PB_ListIcon_FullRowSelect|#PB_ListIcon_GridLines)
		
		ListIcon_Tooltip = GadgetBalloonToolTip(#Window_0, #ListIcon, "Left Double Click files section of the listview gadget to open the file in the PBIde")
		AddGadgetColumn(#ListIcon, 2, "String",738)
		AddGadgetColumn(#ListIcon, 1, "Files", 350) 
		TextGadget(#Text, 590, 25, 30, 15, "Mask")
		StringGadget(#String_mask, 625, 20, 65, 25, "pb")
		Frame3DGadget(#Frame3D, 580, 5, 415, 50, "Options")
		CheckBoxGadget(#CheckBox_wholeword, 700, 15, 80, 15, "Whole Word")
		CheckBoxGadget(#CheckBox_inccomments, 700, 35, 100, 15, "Ignore Comments")
		
		CheckBoxGadget(#CheckBox_sensitive, 810, 15, 170, 15, "Case Sensitive Word/Sentence")
		CheckBoxGadget(#CheckBox_showdir, 810, 35, 170, 15, "Show Directory Path in File View")
		Frame3DGadget(#Window_ExportOptions, 5, 65, 1055, 70, "Export Options")
		ButtonGadget(#Window_ExportSearchString, 15, 95, 115, 25, "Export Search String")
		ButtonGadget(#Window_ExportAllWithPath, 140, 95, 115, 25, "Export All With Path")
		ButtonGadget(#Window_ExportAsViewed, 265, 95, 115, 25, "Export As Viewed")
		CheckBoxGadget(#Window_DefaultFilename, 390, 95, 99, 25, "Default Filename")
		SetGadgetState(#Window_DefaultFilename,1)
		TextGadget(#Window_ExportFileName, 610, 95, 450, 25, "")
	EndIf
EndProcedure

OpenWindow_Window_0()

WindowBounds(#Window_0,1076,400,1076,1100)
SetGadgetColor(#ListIcon,#PB_Gadget_BackColor,$C6FDFC)
SetGadgetColor(#ListIcon,#PB_Gadget_LineColor,$949595)
SetGadgetColor(#ListIcon,#PB_Gadget_FrontColor,$000000)

Repeat
	Select WaitWindowEvent()
		Case #PB_Event_SizeWindow
			ResizeGadget(#ListIcon, #PB_Ignore, #PB_Ignore, WindowWidth(0) , (WindowHeight(0)-164) )
		Case #PB_Event_Gadget   
			Select EventGadget()
				Case #Button_dir 
					f$=PathRequester("","c:\")  
					SetGadgetText(#String_dir,f$) 
				Case #Button_search 
					SetWindowTitle(#Window_0,"Searching for Files   ")
					tline=0
					ccount=0
					mythread=CreateThread(@ssearch(), 0)
				Case #ListIcon
					ResetList(dlist$())
					FirstElement(dlist$())
					;StartOfLinkedList(dlist$())
					ev=EventType()
					If ev= #PB_EventType_LeftDoubleClick
						gs=GetGadgetState(#ListIcon)  
						If gs>=0
							If GetGadgetState(#CheckBox_showdir)=0
								ForEach dlist$()
									If ListIndex(dlist$())=gs
										sf$=dlist$()
									EndIf
								Next
								RunProgram( sf$,"",GetPathPart(f$) ) 
							Else
								sf$=GetGadgetItemText(#ListIcon,gs,1)
								RunProgram( sf$,"",GetPathPart(f$) ) 
							EndIf
						EndIf
					EndIf
				Case #Window_ExportSearchString
					
					If GetGadgetState(#Window_DefaultFilename)=#True
						exportfilename$ = "c:\users\"+bGetUserName()+"\desktop\Search Results For "+GetGadgetText(#String_term)+".txt"
					Else
						exportfilename$=OpenFileRequester("Select File to Store Search Results", "*.txt", "*.txt", 0)
					EndIf
					If OpenFile(0, exportfilename$)
						;StartOfLinkedList(ForExport$())
						ResetList(ForExport$())
						FirstElement(ForExport$())
						ForEach ForExport$()
							febs=FindString(ForExport$(),GetGadgetText(#String_term))
							fees=FindString(ForExport$()," ",febs+1)
							
							If Mid(ForExport$(),febs-1,1)=" "
								gotya$=Mid(ForExport$(),febs,(fees-febs))
								WriteStringN(o, gotya$)
							EndIf
						Next
						CloseFile(0)
					EndIf
					
				Case #Window_ExportAllWithPath
				Case #Window_ExportAsViewed
				Case #Button_cancel
					ClearList(FilesAndFolders())
					killtimerthread= IsThread(timerthread)
					If killtimerthread
						KillThread(timerthread)
					EndIf
					
					killmythread=IsThread(mythread)
					If killmythread
						KillThread(mythread)
					EndIf
					SetWindowTitle(#Window_0,"Search Cancelled By User "+GGetUserName())
			EndSelect
			
		Case #PB_Event_CloseWindow  
			Select EventWindow()   
				Case #Window_0    
					CloseWindow(#Window_0)  
					ClearList(FilesAndFolders())
					killtimerthread= IsThread(timerthread)
					If killtimerthread
						KillThread(timerthread)
					EndIf
					killmythread=IsThread(mythread)
					If killmythread
						KillThread(mythread)
					EndIf
					End
			EndSelect
	EndSelect
ForEver
Last edited by Zebuddi123 on Thu Apr 11, 2013 1:44 am, edited 2 times in total.
malleo, caput, bang. Ego, comprehendunt in tempore
User avatar
Bisonte
Addict
Addict
Posts: 1313
Joined: Tue Oct 09, 2007 2:15 am

Re: Search tool for strings (pbide tool)

Post by Bisonte »

hmm.... I think you are using some Userlibs...
There are some functions not in there... like FormatNumber() or CalcFileSize()

Please integrate these functions into the given source, so that compiling is possible ;)
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
mpz
Enthusiast
Enthusiast
Posts: 497
Joined: Sat Oct 11, 2008 9:07 pm
Location: Germany, Berlin > member German forum

Re: Search tool for strings (pbide tool)

Post by mpz »

Hi,

i found these commands and the code works. I hope the commands are correct....

Greetings Michael

Code: Select all

ProcedureDLL.s FormatNumber(Number) ;1000000===>1,000,000
   Buffer.s=Space(255)
   GetNumberFormat_(0, 0, Str(Number), 0, @Buffer, Len(Buffer)) ;API
   ProcedureReturn Buffer
EndProcedure


Procedure.s CalcFileSizes(calcsize.i); calc size and return string from bytes 
   If calcsize < 1024
      ProcedureReturn StrD(calcsize,2)+" bytes"
   EndIf
   If calcsize >1024 And calcsize <1048576
      ProcedureReturn StrD(1024/calcsize,2)+" Kilobytes"
   EndIf
   If calcsize >1048576 And calcsize<1073741824
      ProcedureReturn StrD(calcsize/1048576,2)+" Megabytes"
   EndIf
   If calcsize >1073741824 And calcsize< 1099511627776
      ProcedureReturn StrD(1073741824/calcsize,2)+" Gigabytes"
   EndIf
EndProcedure
Working on - MP3D Library - PB 5.73 version ready for download
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 796
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

Re: Search tool for strings (pbide tool)

Post by Zebuddi123 »

Hi mpz & Bisonte have updated the code and included the __CalcFileSizes() proc`s and amended 1 Formatnumber( to __FormatNumber(i had missed they are from my own userlibs, should work now. :oops:

Zebuddi. :D
malleo, caput, bang. Ego, comprehendunt in tempore
Post Reply