Search Selected Folder for Files Containing Specific Text
Oct 19, 2023 23:32:25 GMT
cundo likes this
Post by xxgeek on Oct 19, 2023 23:32:25 GMT
The following code allows the user to search for specified text in files, in a chosen folder.
Does not search sub folders.
1. Search for - specify some text using keyword(s)
2. in Files of Type - Specify a file type eg: txt bas html csv cmd ps1 - etc
If 'Files of Type textbox' is left empty, the search returns files with no extension (if any exist)
If a * is used in 'Files of Type textbox' the search returns ALL files containing the search word(s)
3. Select Folder
A folder can be selected by using the button, or manually entered in the 'Folder Selected" textbox
Once the folder is selected a list of the files in the selected folder containing the text provided will be presented. If an item in the list is clicked, the full text of the file appears in the text editor.
If no folder is selected, and no path exists in the 'Selected Folder' texbox when 'Search' is clicked - no search will be conducted. It just waits for another attempt.
Please report any issues found.
Enjoy!
Does not search sub folders.
1. Search for - specify some text using keyword(s)
2. in Files of Type - Specify a file type eg: txt bas html csv cmd ps1 - etc
If 'Files of Type textbox' is left empty, the search returns files with no extension (if any exist)
If a * is used in 'Files of Type textbox' the search returns ALL files containing the search word(s)
3. Select Folder
A folder can be selected by using the button, or manually entered in the 'Folder Selected" textbox
Once the folder is selected a list of the files in the selected folder containing the text provided will be presented. If an item in the list is clicked, the full text of the file appears in the text editor.
If no folder is selected, and no path exists in the 'Selected Folder' texbox when 'Search' is clicked - no search will be conducted. It just waits for another attempt.
Please report any issues found.
Enjoy!
'Search files in a selected folder for specific text
'by xxgeek Oct 2023
global FolderDialog$, searchFor$
dim listing$(10)
dim info$(10,10)
nomainwin
WindowWidth = 800:WindowHeight = 600
UpperLeftX= int((DisplayWidth-WindowWidth)/2)
UpperLeftY= int((DisplayHeight-WindowHeight)/2)
BackgroundColor$ = "lightgray"
ForegroundColor$ = "black"
listbox #search.list, listing$(), [itemSelected], 5, 55, 285, 440
texteditor #search.fileText, 290, 55, 490, 475
statictext #search.for, "Search For", 35, 10, 70, 20
textbox #search.searchFor, 110, 5, 285, 25
statictext #search.filetype, " in Files of Type", 400, 10, 100, 20
textbox #search.type, 505, 5, 60, 25
button #search.folder, "Select Folder", [folder], ul, 575, 5, 100, 25
button #search.search, "Search", [search], ul, 685, 5, 90, 25
textbox #search.searchin, 110, 30, 670, 25
statictext #search.selected, "Selected Folder", 5, 35, 105, 20
statictext #search.found, "", 5, 500, 280, 50
open "Search" for window as #search
#search "trapclose [quit]"
#search "font arial 10 bold"
#search.list "singleclickselect"
#search.fileText, "!autoresize"
#search.searchFor "!setfocus"
wait
[itemSelected]
#search.list "selection? filePath$"
open FolderDialog$;"\";filePath$ for input as #1
#search.fileText, "!contents #1";
close #1
#search.fileText "!origin 0 0"
wait
[folder]
#search.searchin ""
#search.searchFor "!contents? searchFor$"
caption$ = " Select a Folder to Search - for Files Containing";chr$(13);" ' ";searchFor$;" '"
call browser caption$
#search.searchin FolderDialog$
[search]
#search.searchin "!contents? FolderDialog$"
if FolderDialog$ = "" then wait
cursor hourglass
#search.fileText, "!cls"
found = 0
redim info$(10,10)
#search.searchFor "!contents? searchFor$"
#search.type "!contents? fileType$"
files FolderDialog$, "*.";fileType$, info$()
numFiles = val(info$(0,0))
redim listing$(numFiles+1)
for i = 1 to numFiles
fileToOpen$ = info$(i, 0)
open FolderDialog$;"\";fileToOpen$ for input as #2
contents$ = input$(#2, lof(#2))
close #2
if instr(lower$(contents$), lower$(searchFor$)) then
found = found +1
listing$(found) = fileToOpen$
end if
next i
#search.list "reload"
if fileType$ = "" then fileType$ = "'no .ext'"
#search.found "Number of .";fileType$;" Files Found Containing ";chr$(13);" '";searchFor$;"' = ";found
cursor normal
wait
[quit]
close #search
end
'sub to make folder dialog window
sub browser caption$
dim info$(0, 0)
dim folderInfo$(0, 0)
WindowWidth = 700
WindowHeight = 500
UpperLeftX=INT((DisplayWidth-WindowWidth)/2)
UpperLeftY=INT((DisplayHeight-WindowHeight)/2)
gosub [FolderDlgGetDrives]
statictext #folderdlg.selection, "Selection >> ", 40, 505, 95, 15
statictext #folderdlg.caption, caption$, 150, 20, 525, 45
listbox #folderdlg.filelist, fileList$(, [fileSelected], 350, 70, 330, 290
listbox #folderdlg.list, FolderList$(, [FolderDlgSelect], 10, 70, 330, 290
button #folderdlg.default, "OK", [FolderDlgOk], UL, 220, 410, 75, 25
button #folderdlg.back, "< < <", [FolderDlgBack], UL, 10, 10, 60, 30
button #folderdlg.C, "Cancel", [FolderDlgCancel], UL, 395, 410, 75, 25
button #folderdlg.plusfont, "+", [plusFont], UL, 75, 10, 30, 30
button #folderdlg.minusfont, "-", [minusFont], UL, 110, 10, 30, 30
textbox #folderdlg.text, 10, 370, 670, 25
BackgroundColor$ = "lightgray"
open "Just Basic v2.0 File Browser" for dialog_modal as #folderdlg
#folderdlg, "trapclose [FolderDlgCancel]"
#folderdlg.text, "Selected (Drive \ Folder \ File) Path Appears Here"
#folderdlg, "font Arial 12 bold"
#folderdlg.filelist, "singleclickselect"
#folderdlg.list, "singleclickselect"
fontsize = 12
wait
[minusFont]
fontsize = fontsize - 1
#folderdlg.list, "font arial ";fontsize;" bold"
#folderdlg.filelist, "font arial ";fontsize;" bold"
wait
[plusFont]
fontsize = fontsize + 1
#folderdlg.list, "font arial ";fontsize;" bold"
#folderdlg.filelist, "font arial ";fontsize;" bold"
wait
[FolderDlgSelect]
#folderdlg.list, "selection? temp$"
if temp$ <> "" then
level = level+1
folder$ = folder$; temp$; "\"
#folderdlg.text, folder$
gosub [FolderDlgGetDir]
#folderdlg.list, "reload"
#folderdlg.list, "select 0"
#folderdlg.default "!setfocus"
end if
wait
[FolderDlgBack]
if level > 0 then
level = level-1
if level = 0 then
folder$ = ""
gosub [FolderDlgGetDrives]
else
i = len(folder$)-1
while mid$(folder$, i, 1) <> "\" and mid$(folder$, i, 1) <> ""
i = i-1
wend
folder$ = left$(folder$, i)
gosub [FolderDlgGetDir]
end if
#folderdlg.text, folder$
fileList$(0) = " F I L E S"
#folderdlg.list, "reload"
#folderdlg.filelist, "reload"
end if
wait
[FolderDlgGetDrives]
c = 1
while word$(Drives$, c) <> ""
c = c+1
wend
redim FolderList$(c)
FolderList$(0) = " D R I V E S"
for i = 1 to c
FolderList$(i) = word$(Drives$, i)
next i
redim fileList$(0)
return
[FolderDlgGetDir]
files folder$, info$(
s = val(info$(0,0))
tt = val(info$(0,1))
redim FolderList$(tt)
FolderList$(0) = " F O L D E R S"
for i = 1 to tt
FolderList$(i) = info$(i+s, 1)
next i
[filesBack]
files folder$, "*.*", folderInfo$()
numFiles = val(folderInfo$(0, 0))
redim fileList$(numFiles)
for x = 1 to numFiles
filename$ = folderInfo$(x, 0)
fileList$(x) = filename$
next x
fileList$(0) = " F I L E S"
sort fileList$(), 0 , numFiles
#folderdlg.filelist, "reload"
return
[FolderDlgOk]
#folderdlg.text, "!contents? FolderDialog$"
If right$(FolderDialog$,1) = "\" then
if right$(FolderDialog$, 2) = ":\" then [goAround]
FolderDialog$ = left$(FolderDialog$, len(FolderDialog$) - 1)
else
[fileSelected]
notice chr$(13);"The Selection is Not a Drive or a Folder";chr$(13);chr$(13);"Select a Drive or a Folder and Try Again" : wait
[goAround]
end if
[FolderDlgCancel]
close #folderdlg
end sub