PDA

View Full Version : TBSearch - searches files for strings



dcromley
23-05-2009, 18:25
Hi,
This isn't rocket science or anything, but I sure find it useful to search the SampleScripts and my own scripts. I could've made an options dialog instead of hard coding everything, but I usually want something that's different that isn't in the options anyhow. [Go ahead, tell me there's already a better version out there :)]

This is set up to search "c:\thinBasic" (and subdirectories) for "texture".
It outputs to a file (change to your output filename):
1) The filename
2) The routine name (if any)
3) The line number and line



' TBSearch - searches for a string - gives filename and routine name
Uses "Console", "File", "UI"
Const sSearch as string = "texture" ' lower case
global fhOutput as long, Directory0 As String

' Dialog_BrowseForFolder(hWnd, msg, defalt, show/hide
' Directory0 = Dialog_BrowseForFolder(0, "Please select a directory", "C:\", %False)
Directory0 = "c:\thinBasic" ' use dialog if you want
fhOutput = File_Open("con:", "output") ' probably a file instead of con:
if fhOutput = 0 then msgbox 0, "Err Open fhOutput": stop
SearchDir(Directory0)
if File_Close(fhOutput) <> 0 then msgbox 0, "Err Close fhOutput": stop
Printl "End": beep
waitkey

Sub SearchDir(sPath as string)
local fhInput as long, i, nFiles, nParse, LineCount as integer
local aFiles(), aParse(2), s1, sRec, sSlash, sSubName as string
local swFileName, swSubName as integer
Printl "Searching", sPath
sSlash = iif$(Right$(sPath, 1) = "\", "", "\")
' first go thru subdirs
nFiles = Dir_ListArray(aFiles, sPath, "*.*", %File_SUBDIR)
for i = 1 to nFiles
SearchDir(sPath & sSlash & aFiles(i))
next i
' then go thru Files
nFiles = Dir_ListArray(aFiles, sPath, "*.tBasic", %File_NORMAL)
for i = 1 to nFiles
fhInput = File_Open(sPath & sSlash & aFiles(i), "input")
if fhInput = 0 then msgbox 0, "Err Open fhInput " & sPath & sSlash & aFiles(i): stop
LineCount = 0: swFileName = 1: swSubName = 0
do while not File_eof(fhInput)
incr LineCount
sRec = Lcase$(File_lineinput(fhInput))
nParse = parse(lCase$(sRec), aParse(), " ")
if nParse >= 1 then ' ck end of routine
if aParse(1) = "end" then swSubName = 0
end if
if instr(1, sRec, sSearch) > 0 then ' ck for hit
if swFileName = 1 then ' output filename
swFileName = 0
File_lineprint(fhOutput, $crlf & "----" & sPath & sSlash & aFiles(i))
end if
if swSubName = 1 then ' output line
swSubName = 0
File_lineprint(fhOutput, "--" & sSubName)
end if
File_lineprint(fhOutput, LineCount & ": " & sRec)
end if
if nParse >= 1 then ' ck begin routine
if aParse(1) = "sub" or aParse(1) = "function" then
swSubName = 1
sSubName = sRec
end if
end if
loop
if File_Close(fhInput) <> 0 then msgbox 0, "Err Close fhInput": stop
next i
End Sub

Petr Schreiber
23-05-2009, 19:01
Very nice!,

one little tip: it is safer to use APP_PATH, which returns path thinBasic is installed on, instead of "c:\thinbasic", I have it installed on other ( top secret ) place.

But worked well!