Results 1 to 2 of 2

Thread: TBSearch - searches files for strings

  1. #1
    Member dcromley's Avatar
    Join Date
    Apr 2009
    Location
    Wyoming, USA
    Age
    86
    Posts
    80
    Rep Power
    24

    TBSearch - searches files for strings

    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

    [code=thinbasic]
    ' 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
    [/code]

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736

    Re: TBSearch - searches files for strings

    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!
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Similar Threads

  1. The environmental impact of Google searches
    By ErosOlmi in forum Search engines
    Replies: 0
    Last Post: 12-01-2009, 23:27

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •