Home Page

R. T. RUSSELL

BBC BASIC for Windows

File search



This program searches the disk (or a specified subfolder) for files matching a given name, which may include wildcards, and lists the files found. It incorporates a function FNfsearch which can be used separately to scan the disk for matching files and store the results in an array.

Download FSEARCH.BBC Run FSEARCH.EXE

     REM. File search in BBC BASIC for Windows, RTR 14-Nov-2004, 01-May-2007

     
INSTALL @lib$+"WINLIB2"
     INSTALL @lib$+"WINLIB5"

     MAXFILES = 10000
     DIM res$(MAXFILES,3)

     ON ERROR SYS "MessageBox", @hwnd%, REPORT$, 0, 48 : PROCcleanup : QUIT
     ON CLOSE PROC
cleanup : QUIT

     SYS
"InitCommonControls"
     hList% = FN_createwindow("SysListView32","",0,0,@vdu%!28,@vdu%!36,0,&8001,0) : REM LVS_REPORT | LVS_NOSORTHEADER

     
DIM file% 255, folder% 255
     DIM lc{mask%, fmt%, cx%, pszText%, cchTextMax%, iSubItem%}
     DIM lvitem{mask%, iItem%, iSubItem%, state%, stateMask%, pszText%, \
     \          cchTextMax%, iImage%, lParam%}
     DIM bi{hOwner%, pidlRoot%, pszDisplayName%, lpszTitle%, ulFlags%, lpfn%, \
     \      lParam%, iImage%}

     lc.mask% = 15 : REM LVCF_WIDTH | LVCF_SUBITEM | LVCF_FMT | LVCF_TEXT
     
lvitem.mask% = 9 : REM LVIF_TEXT | LVFI_STATE
     
bi.hOwner% = @hwnd%
     bi.pszDisplayName% = folder%

     DATA Name, 200, In Folder, 200, Size, 100, Modified, 100
     FOR C% = 0 TO 3
       READ head$, cx%
       head$ += CHR$0
       IF C% = 2 lc.fmt% = 1 ELSE lc.fmt% = 0 : REM right-justify size
       
lc.cx% = cx%
       lc.pszText% = !^head$
       lc.cchTextMax% = LENhead$
       lc.iSubItem% = C%
       SYS "SendMessage", hList%, &101B, C%, lc{} : REM LVM_INSERTCOLUMN
     
NEXT

     
dlg%=FN_newdialog("Find Files",50,50,274,87,8,512)
     PROC_pushbutton(dlg%,"Find Now",101,214,6,50,14,1)
     PROC_pushbutton(dlg%,"Stop",102,214,28,50,14,0)
     PROC_static(dlg%,"Named:",103,8,10,64,16,0)
     PROC_editbox(dlg%,"",104,38,7,167,13,0)
     PROC_static(dlg%,"Look in:",105,8,31,70,17,0)
     PROC_pushbutton(dlg%,"Browse...",106,154,28,50,14,0)
     PROC_editbox(dlg%,"C:",107,38,28,108,13,0)
     PROC_checkbox(dlg%,"Include subfolders",108,37,46,80,16,0)
     PROC_static(dlg%,"",109,8,71,256,8,0)

     PROC_showdialog(dlg%)
     SYS "CheckDlgButton", !dlg%, 108, 1

     GlobalStop% = TRUE
     
GlobalIndex% = 0
     GlobalPath$ = ""

     ON SYS PROCclick(@wparam% AND &FFFF) : RETURN
     ON MOVE PROC
move(@msg%,@wparam%,@lparam%) : RETURN
     ON TIME PROC
timer : RETURN

     REPEAT
       IF NOT
GlobalStop% THEN
         
GlobalIndex% = 0
         GlobalPath$ = ""
         PROCtimer
         SYS "SendMessage", hList%, &1009, 0, 0 : REM LVM_DELETEALLITEMS
         
SYS "GetDlgItemText", !dlg%, 104, file%, 255 TO L% : file%?L% = 13
         SYS "GetDlgItemText", !dlg%, 107, folder%, 255 TO L% : folder%?L% = 13
         SYS "IsDlgButtonChecked", !dlg%, 108 TO sub%
         found% = FNfsearch($folder%,$file%,res$(),sub%,0)
         GlobalStop% = TRUE
         SYS
"SetDlgItemText", !dlg%, 109, "Found "+STR$found%+" file(s)"
       ENDIF
     UNTIL INKEY
(2) = 0
     END

     
DEF PROCtimer
     PRIVATE I%, R%
     IF R% ENDPROC : REM. Prevent reentrant access
     
R% = TRUE
     LOCAL
C%, H%, text$
     IF I% > GlobalIndex% I% = 0
     WHILE I% < GlobalIndex%
       FOR C% = 0 TO 3
         text$ = res$(I%,C%)+CHR$0
         lvitem.iItem% = I%
         lvitem.iSubItem% = C%
         lvitem.pszText% = !^text$
         lvitem.cchTextMax% = LENtext$
         IF C% THEN
           SYS
"SendMessage", hList%, &102E, I%, lvitem{} : REM LVM_SETITEMTEXT
         
ELSE
           SYS
"SendMessage", hList%, &1007, I%, lvitem{} : REM LVM_INSERTITEM
         
ENDIF
       NEXT
C%
       I% += 1
     ENDWHILE
     IF NOT
GlobalStop% SYS "SetDlgItemText", !dlg%, 109, "Searching  "+GlobalPath$
     SYS "GetDlgItem", !dlg%, 101 TO H% : SYS "EnableWindow", H%, GlobalStop% AND 1
     SYS "GetDlgItem", !dlg%, 102 TO H% : SYS "EnableWindow", H%, (NOT GlobalStop%) AND 1
     R% = FALSE
     ENDPROC

     
DEF PROCclick(click%)
     LOCAL pidl%, malloc%
     CASE click% OF
       WHEN
101: GlobalStop% = FALSE
       WHEN
102: GlobalStop% = TRUE
       WHEN
106:
         SYS "SHBrowseForFolder", bi{} TO pidl%
         IF pidl% THEN
           SYS
"SHGetPathFromIDList", pidl%, folder%
           SYS "SHGetMalloc", ^malloc%
           SYS !(!malloc%+20), malloc%, pidl% : REM. IMalloc::Free
           
SYS "SetDlgItemText", !dlg%, 107, folder%
         ENDIF
     ENDCASE
     ENDPROC

     
DEF PROCmove(M%,W%,L%)
     IF M% = 5 SYS "MoveWindow", hList%, 0, 0, L% AND &FFFF, L% >> 16, 1
     ENDPROC

     
DEF PROCcleanup
     dlg% += 0 : IF dlg% PROC_closedialog(dlg%)
     hList% += 0: IF hList% PROC_closewindow(hList%)
     ENDPROC

     
REM. path$ = where to start searching ("C:" searches entire disk)
     REM. find$ = filename (possibly containing wildcards) to look for
     REM. result$() = 2-dimensional array in which to return results
     REM. sub% = TRUE to search subdirectories (subfolders)
     REM. index% = initial index into result$() array (normally 0)

     
DEF FNfsearch(path$,find$,result$(),sub%,index%)
     GlobalPath$ = path$
     GlobalStop% += 0
     LOCAL @%, dir%, buf%, l%, st%, sh%, res%, ?419
     *float 64
     @% = &1010
     DIM dir% LOCAL 317, buf% LOCAL 255, st% LOCAL 15
     IF RIGHT$(path$,1)<>"\" path$ += "\"
     IF find$ = "" find$ = "*"
     SYS "FindFirstFile", path$+find$, dir% TO sh%
     IF sh% <> -1 THEN
       REPEAT
         IF
(!dir% AND 16) = 0 THEN
           
result$(index%,0) = $$(dir%+44)
           result$(index%,1) = path$
           result$(index%,2) = STR$(2^32*dir%!28 + 2*(dir%!32>>>1) + (dir%!32AND1))
           SYS "FileTimeToSystemTime", dir%+20, st%
           SYS "GetDateFormat", 0, 0, st%, 0, buf%, 256 TO l%
           IF l% buf%?(l%-1) = 13 : result$(index%,3) = $buf%
           SYS "GetTimeFormat", 0, 10, st%, 0, buf%, 256 TO l%
           IF l% buf%?(l%-1) = 13 : result$(index%,3) += " "+$buf%
           index% += 1
           GlobalIndex% = index%
         ENDIF
         SYS
"FindNextFile", sh%, dir% TO res%
       UNTIL res% = 0 OR GlobalStop%
       SYS "FindClose", sh%
     ENDIF
     IF
sub% THEN
       SYS
"FindFirstFile", path$+"*", dir% TO sh%
       IF sh% <> -1 THEN
         REPEAT
           IF
!dir% AND 16 IF dir%?44 <> &2E THEN
             
index% = FNfsearch(path$+$$(dir%+44),find$,result$(),sub%,index%)
           ENDIF
           SYS
"FindNextFile", sh%, dir% TO res%
         UNTIL res% = 0 OR GlobalStop%
         SYS "FindClose", sh%
       ENDIF
     ENDIF
     
= index%


Home - Products - Contact us

Best viewed with Any Browser Valid HTML 3.2!
© Richard Russell 2007