' 6) Can be run from the command line and explorer. i.e. It processes it's arguments, and now runs using a Sub Main() procedure.
' 7) Post-extraction works correctly now.
' 8) I've put some comments into the Grep functions, 'cos they were getting horribly complex.
' 9) Silent modes for command line running
' 10) Example files included.
' New functionality for Version 1.3 (bug fixes mainly).
' 1) If JustCountThem And DisplayFileNames = OncePerFile then don't have any output if matches = 0 for that file
' 2) More than 10 regular expressions! I thought 10 was easily enough, but no.
' 3) Improved modularity using compiler directives.
' 4) Removed disgusting common dialog and listview hacks (and replaced with ones almost as bad, sigh).
' 5) Numerous bugs in WorkOutGrepCommand() and PropagateUp() fixed.
' 6) Move to using Windows temp directory for default output files, and add cleardown routine.
' 7) Multiple concurrent searches (in different applications).
' 8) Automatic registration of script files for use with explorer.
' Further improvements that people have asked for....
' 1) Awk! (They don't ask for this directly, but practically, an Awk scripting language is what they need).
' - They can bog off unless they offer me a fat cheque or some seriously
' good parsing code or source code for a compiler (not fussy which language, excepting Prolog and LISP).
#Const VBHardCore = False ' If you have Bruce McKinley's VBCore library,
' setting this compiler directive to True makes for better looking dialog boxes
' available from www.mvps.org/vb/hardcore
Public Running As Boolean
Private Stopping As Boolean
Private DoNotCallWorkOutGrep As Boolean
Private Sub cboDispFileNames_Click()
If DoNotCallWorkOutGrep = False Then
WorkOutGrepCommand
End If
End Sub
Private Sub cboFilesToFind_Change()
WorkOutGrepCommand
End Sub
Private Sub cboFilesToFind_Click()
WorkOutGrepCommand
End Sub
Private Sub cboIncludeSeperator_Change()
WorkOutGrepCommand
End Sub
Private Sub cboIncludeSeperator_Click()
WorkOutGrepCommand
End Sub
Private Sub chkCase_Click()
WorkOutGrepCommand
End Sub
Private Sub chkExact_Click()
WorkOutGrepCommand
End Sub
Private Sub chkFindFiles_Click()
If chkFindFiles.Value = 0 Then
txtFolder.Enabled = False
cmdBrowseFolders.Enabled = False
chkRecurseSubFolders.Enabled = False
lblFilesToFind.Enabled = False
cboFilesToFind.Enabled = False
lblFolder.Enabled = False
lvwInputFiles.Enabled = True
cmdAddToListView.Enabled = True
cmdClearListView.Enabled = True
cmdRemoveFromListView.Enabled = True
cmdChangeListView.Enabled = True
cmdFindFiles.Enabled = True
lblFilesToAnalyse.Enabled = True
Else
txtFolder.Enabled = True
cmdBrowseFolders.Enabled = True
chkRecurseSubFolders.Enabled = True
lblFilesToFind.Enabled = True
cboFilesToFind.Enabled = True
lblFolder.Enabled = True
lvwInputFiles.Enabled = False
cmdAddToListView.Enabled = False
cmdClearListView.Enabled = False
cmdRemoveFromListView.Enabled = False
cmdChangeListView.Enabled = False
cmdFindFiles.Enabled = False
lblFilesToAnalyse.Enabled = False
End If
WorkOutGrepCommand
End Sub
Private Sub chkInvert_Click()
WorkOutGrepCommand
End Sub
Private Sub chkLineNumbers_Click()
WorkOutGrepCommand
End Sub
Private Sub chkOnlyACount_Click()
If chkOnlyACount.Value = 1 Then
chkLineNumbers.Enabled = False
Label3.Enabled = False
Label4.Enabled = False
txtPreviousLinesToOutput.Enabled = False
txtPostLinesToOutput.Enabled = False
Label5.Enabled = False
Else
chkLineNumbers.Enabled = True
Label3.Enabled = True
Label4.Enabled = True
txtPreviousLinesToOutput.Enabled = True
txtPostLinesToOutput.Enabled = True
Label5.Enabled = True
End If
WorkOutGrepCommand
End Sub
Private Sub chkOutputToFile_Click()
If chkOutputToFile.Value = 1 Then
txtOutputFile.Enabled = True
cmdBrowseOutputFile.Enabled = True
Else
txtOutputFile.Enabled = False
cmdBrowseOutputFile.Enabled = False
End If
End Sub
Private Sub chkRecurseSubFolders_Click()
WorkOutGrepCommand
End Sub
Private Sub cmdAddToListView_Click()
Dim i, j As Long
Dim ListItem As ListItem
Dim Path As String
Dim FileName As String
Dim ChkErr As Long
On Error GoTo ErrHandler:
cdb1.CancelError = True
cdb1.DefaultExt = ".txt"
cdb1.DialogTitle = "Add Files for Grep to search in..."
cdb1.Filter = "All Files (*.*)|*.*|ASCII Text Files (*.txt)|*.txt|All Text Files (*.txt,*.doc,*.rtf)|*.txt;*.doc;*.rtf|C Files (*.h,*.c)|*.h;*.c|Rich Text Files (*.rtf)|*.rtf|Visual Basic Files (*.bas,*.vbp,*.ctl,*.cls;*.frm,*.pag,*.res)|*.bas,*.vbp,*.ctl,*.cls,*.frm,*.pag,*.res"
cdb1.FilterIndex = 2
cdb1.FileName = ""
cdb1.Flags = cdlOFNAllowMultiselect Or cdlOFNExplorer Or cdlOFNFileMustExist Or cdlOFNHideReadOnly Or cdlOFNLongNames Or cdlOFNPathMustExist
cdb1.MaxFileSize = 32000
cdb1.ShowOpen
If Len(cdb1.FileName) > 1000 Then
Me.MousePointer = vbArrowHourglass
End If
i = InStr(cdb1.FileName, Chr(0))
If i > 0 Then
' multiselect. Whoever wrote the common dialog control decided that if you'd chosen multi-select
' that he'd return it in an odd way. Specifically, the first "bit" of the string is the path (up to a NULL chr(0) character)
' then the filenames are spaced using NULL characters. Yuk.
Path = Mid(cdb1.FileName, 1, i - 1)
j = InStr(i + 1, cdb1.FileName, Chr(0))
Do While j > 0
FileName = Path & "\" & Mid(cdb1.FileName, i + 1, j - i - 1)
On Error Resume Next
ChkErr = lvwInputFiles.ListItems(FileName).Text ' if error occurs here it is doesn't occur in the list? Shut up, it works, ok?
If Err Then
Err.Clear
Set ListItem = lvwInputFiles.ListItems.Add(, FileName, FileName)
cdb1.Filter = "All Files(*.*)|*.*|Text File(*.txt)|*.txt|Rich Text File (*.rtf)|*.rtf)"
cdb1.FilterIndex = 2
cdb1.Flags = cdlOFNExplorer Or cdlOFNLongNames Or cdlOFNHideReadOnly Or cdlOFNOverwritePrompt
cdb1.ShowSave
txtOutputFile.Text = cdb1.FileName
Exit Sub
ErrHandler:
Exit Sub
End Sub
Private Sub cmdChangeListView_Click()
Dim i, j As Long
Dim ListItem As ListItem
Dim Path As String
Dim FileName As String
On Error GoTo ErrHandler:
cdb1.CancelError = True
cdb1.DefaultExt = ".txt"
cdb1.DialogTitle = "Add Files for Grep to search in..."
cdb1.Filter = "All Files (*.*)|*.*|ASCII Text Files (*.txt)|*.txt|All Text Files (*.txt,*.doc,*.rtf)|*.txt;*.doc;*.rtf|C Files (*.h,*.c)|*.h;*.c|Rich Text Files (*.rtf)|*.rtf|Visual Basic Files (*.bas,*.vbp,*.ctl,*.cls;*.frm,*.pag,*.res)|*.bas,*.vbp,*.ctl,*.cls,*.frm,*.pag,*.res"
cdb1.FilterIndex = 2
cdb1.FileName = ""
cdb1.Flags = cdlOFNAllowMultiselect Or cdlOFNExplorer Or cdlOFNFileMustExist Or cdlOFNHideReadOnly Or cdlOFNLongNames Or cdlOFNPathMustExist
cdb1.MaxFileSize = 32000
cdb1.ShowOpen
If Len(cdb1.FileName) > 1000 Then
Me.MousePointer = vbArrowHourglass
End If
lvwInputFiles.ListItems.Clear
i = InStr(cdb1.FileName, Chr(0))
If i > 0 Then
' multiselect. Whoever wrote the common dialog control decided that if you'd chosen multi-select
' that he'd return it in an odd way. Specifically, the first "bit" of the string is the path (up to a NULL chr(0) character)
' then the filenames are spaced using NULL characters. Yuk.
Path = Mid(cdb1.FileName, 1, i - 1)
j = InStr(i + 1, cdb1.FileName, Chr(0))
Do While j > 0
FileName = Path & "\" & Mid(cdb1.FileName, i + 1, j - i - 1)
Set ListItem = lvwInputFiles.ListItems.Add(, FileName, FileName)
cdb1.FilterIndeh & Le use"Eoo stItemi)|*.1.Filter = "All Elsriv
im Path AAAAAAAAAAAAAAAAAAAAAAAAAAAA rvilea
im Path AAAAAAAAAAAnd
End Subme(Fame), "d.e McKinlar WorkOutGrepCommand2 AAAp t>AAnd
End Subme(Fame), L sear376
Directory
i + 1= True
txtP = InStr(i + 1, cdb1.Fnd Subm
i + 1= True
(Fame), L sear3e, cdb1.FileName)
, L sear376"Add Files for Grep ttttttttttleName)
ListIteFSDIRSkColor = &H00k()
DiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiieNamevate Sub pli It is si yPForWinGreps String
Di &H00k()
Diiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii-Ap t>AAniles|VisAs S
la"All Files rviles (2FSDIRS, t Clicf,rviles (2FSDIRS, t Clles (x sAs stItemi)|*.1.Filti!ub n%22) = Forma(ommand2eplorer Or cdlOsnd2eplorer , t Clles (x285
Left earc Style = W n%22) = minding diaemicolons betweep.
' 8) I've put some commeiItems(FileName).Text Dttribute VB_fa
'[!charli VB_fa
'[!charli VtItems.Add(, FileName
.Add(, FileName
.Add(, FileName
0SNistView
Caption = 1680
ndex Ersinghders
Caption = ltems(1) = FileLen(FileName)
Option Errt Ss (*.i) ListItect. Who VtItems.Add(, Ereiiiiiiii
End Sub
Private Seb
cdb1"2reiiiiiiii
End k.
ErrHandler:
tyle eight = 2er ,ame = Path & 1)
Set Lis eigh) Counting fachosen multi-select
' that he'd return it in an odd way. Specifically, the first "bit" of the string is the path (up to a NULL chr(0) character)
' then the filenames are spaced using NULL characters. Yuk.
Path = Mid(cdb1.FileNamegd for....
" of the string is the path (tItect. Who VtItems.Add(, c
If Len(2 FileName), " Sub\" om2 FileName),0 txtFolder.Enablens"
Height
Private Sub
't = lvwIncls,*.e)
S*.ctl,*.cls;*.frm,*NULL characterot "bit" of the string is the path (up to a NULL ce "Rxist Or cdlOF/Hist Or cdlOF/Histing is thbas,*.v "\" & Mid& cv "\" & Mid& c,o VtItems.Add(, Er*.v "\" & TabIndex & c,o VtItems.Add(, zutGrepCon(2 FileNyyyyyyyyyyyy1Vr*.vEr*.v "\" & TC7-850A-101B-AFC0-421(lView.Enabled = True
ListIteFSDIRSkColor = &H00kxLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLD0t>>>>Lv|* Sub cLLLLLLLLLLLLLLLLLLLLLLLLLLLLLD0tI)* Sub cLLLLLLLLLLLLLLLLLLLLLLLLLLLLLD0tIoLLLLLLLLLLLim Path As String
If Len(cd Forup to a NULL chr(ific*.*)|*.*|ASCII Text Fiiiiip to a NULL chr(0) characiiip to a NUUUUUUUUUUUUUUUUUUUUUUUf VBHaro a NUUUUUUUUUUUUUUUUUUUUUUUf VBHaro a NUUUUUUUUUUUUUUUUUUUUUUUt|Rich TLLLLLein/ameDme)
ListItem.Sub
ECan be runnnnnnnnEUUUUUUUUociiiiiiiiiiii
DLLLiiii
End kcLLLleNiiiiiiiii CoSnLLLSub
ErrHandler:
Path = Mid(cdb1.FNUUUii idtwi mChooseDir Height
Pr "Rxist Or cdles rviles (2FSDIA s (2 Xr)rviles f*.rtf|Viss=-)UUii idtwi r)))))))iid(c)))))))iid(c)))))))iid)UUii idtwi r)))))))iid(c)))))iiiiii&amec *.ctl, & Mid& "\" & Midi p
DyNiiiiiiiiiiiiiii
If Len(cd r)))))))iid(c)o)iiiiii&amec *.ctl, & Mid&))iid(c)))))))iid)z FileNan Error LLLLLLn
"ESBBBBBBBBB ' then theiii
tEa" epComaaaaaa,ad)z iSiSiSiSiSiSiSiSiSiSiSiciSiSiSiiiiiiiiiiiihdtIte iW rviles (2FSDIA aaa,ad)z iSiSiSiSiSiSiSiSiSiSiSiciSiSiSiiiiiiiiiiiihdtIte iW ry)))))i8"ll T txthen ListItem.SubItems(1UiSiSiSiSiSiciSiSems(1UiSiSiSiSiSiciSiSems(1SiSiSiSiciSiSems(1SiSiSLLLLLLLLLiciSiSems(1SiSiSiS0 Sems(1UiSiSiSi& Mid& ccccc()i8"ll T 375
End
SiSiSiSiciSiSitop )
thbas,*.v "\" Len(cdbcdb1.FileName, cdb1.Fillllll = "Add Files for Grepb1.FillllllrA aaa,ad)z iSiSiS Len(cdbcdb1.FileName, cdb1.Fillllll = "Add Fi mChooseDir P cdb1.Fi,Time( ' that he'd return it in an odd wayaFe'd returnfooseDileNiiiiiiiiiiiiiii