-
Notifications
You must be signed in to change notification settings - Fork 72
/
RegExFileSearch.ahk
219 lines (170 loc) · 7.13 KB
/
RegExFileSearch.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
RegExFileSearch(fnSearchTerm,fnSearchRootFolder := "")
{
; search files and folders using regular expressions
; MsgBox fnSearchRootFolder: %fnSearchRootFolder%`nfnSearchTerm: %fnSearchTerm%
; declare local, global, static variables
Global LocalWorkFolder, TempDir, RegExFileSearchResultsHTMLTemplate
Try
{
; set default return value
ReturnValue := 0 ; success
; validate parameters
If !fnSearchTerm
Throw, Exception("No search term was passed in")
; initialise variables
If !fnSearchRootFolder
fnSearchRootFolder := LocalWorkFolder
MatchingFolderNames := ""
MatchingFileNames := ""
MatchingFileContents := ""
BodyText := ""
MaxFilenameLength := 0
MaxFilenameLength2 := 0
MaxFoldernameLength2 := 0
MaxFilenameLength3 := 0
MaxFoldernameLength3 := 0
MaxLineLength3 := 0
RegExFileSearchResults = %TempDir%\RegExSearch_%A_Now%.html
; set default options if not present
; FirstOpenParen := InStr(fnSearchTerm, "(")
; FirstCloseParen := InStr(fnSearchTerm, ")")
; If (FirstCloseParen > FirstOpenParen) ; no options
; fnSearchTerm := "iS)" fnSearchTerm
; create arrays of matches
Loop, Files, %fnSearchRootFolder%\*.*, FDR
{
; folders
If A_LoopFileAttrib contains D
{
If RegExMatch(A_LoopFileName,fnSearchTerm)
{
MatchingFolderNames .= A_LoopFileName "|" A_LoopFileFullPath "`r`n"
MaxFilenameLength1 := StrLen(A_LoopFileName) > MaxFilenameLength1 ? StrLen(A_LoopFileName) : MaxFilenameLength1
}
}
; files
If A_LoopFileAttrib not contains D
{
If A_LoopFileExt in txt,sql,csv,ahk
{
; filenames
If RegExMatch(A_LoopFileName,fnSearchTerm)
{
MatchingFileNames .= A_LoopFileName "|" A_LoopFileFullPath "|" A_LoopFileDir "`r`n"
MaxFilenameLength2 := StrLen(A_LoopFileName ) > MaxFilenameLength2 ? StrLen(A_LoopFileName ) : MaxFilenameLength2
MaxFoldernameLength2 := StrLen(A_LoopFileFullPath) > MaxFoldernameLength2 ? StrLen(A_LoopFileFullPath) : MaxFoldernameLength2
}
; file contents
FileRead, ThisFileContents, %A_LoopFileFullPath%
If RegExMatch(ThisFileContents,fnSearchTerm)
{
Loop, Parse, ThisFileContents, `n, `r
{
ThisLineNum := A_Index
ThisLine := A_LoopField
StringReplace, ThisLine, ThisLine, %A_Tab%, %A_Space%, All
If RegExMatch(ThisLine,fnSearchTerm)
{
MatchingFileContents .= A_LoopFileName "|" SubStr("00000" ThisLineNum,-4) "|" A_LoopFileFullPath "|" ThisLine "|" StrLen(A_LoopFileName) "|" A_LoopFileDir "`r`n"
MaxFilenameLength3 := StrLen(A_LoopFileName ) > MaxFilenameLength3 ? StrLen(A_LoopFileName ) : MaxFilenameLength3
MaxFoldernameLength3 := StrLen(A_LoopFileFullPath) > MaxFoldernameLength3 ? StrLen(A_LoopFileFullPath) : MaxFoldernameLength3
MaxLineLength3 := StrLen(ThisLine ) > MaxLineLength3 ? StrLen(ThisLine ) : MaxLineLength3
}
}
}
}
}
}
; sort by filename
Sort, MatchingFolderNames
Sort, MatchingFileNames
Sort, MatchingFileContents
; format text
Loop, Parse, MatchingFolderNames, `n, `r
{
ThisArrayLine := A_LoopField
ThisFilename := ""
ThisFileDir := ""
ThisFilenameLength := 0
StringSplit, ThisElement, ThisArrayLine, |
ThisFilename := ThisElement1
ThisFileDir := ThisElement2
ThisFilenameLength := StrLen(ThisElement1)
MatchingFolderNamesHTML .= "<a href=""" ThisFilepath """ title=""" ThisFilepath """>" ThisFilename "</a>" StrReplicate(" ",MaxFilenameLength1-ThisFilenameLength) " " ThisFileDir "<br>`r`n"
}
Loop, Parse, MatchingFileNames, `n, `r
{
ThisArrayLine := A_LoopField
ThisFilename := ""
ThisFilepath := ""
ThisFileDir := ""
ThisFilenameLength := 0
StringSplit, ThisElement, ThisArrayLine, |
ThisFilename := ThisElement1
ThisFilepath := ThisElement2
ThisFileDir := ThisElement3
ThisFilenameLength := StrLen(ThisElement1)
MatchingFileNamesHTML .= "<a href=""" ThisFilepath """ title=""" ThisFilepath """>" ThisFilename "</a>" StrReplicate(" ",MaxFilenameLength2-ThisFilenameLength) " " ThisFileDir "<br>`r`n"
}
Loop, Parse, MatchingFileContents, `n, `r
{
ThisArrayLine := A_LoopField
ThisFilename := ""
ThisLineNum := ""
ThisFilepath := ""
ThisLine := ""
ThisFilenameLength := ""
ThisFileDir := ""
ThisLineLength := 0
StringSplit, ThisElement, ThisArrayLine, |
ThisFilename := ThisElement1
ThisLineNum := ThisElement2
ThisFilepath := ThisElement3
ThisLine := Trim(ThisElement4)
ThisFilenameLength := ThisElement5
ThisFileDir := ThisElement6
ThisLineLength := StrLen(ThisLine)
ThisLine := ReplaceHtmlDecodedChars(Trim(ThisElement4))
ThisLine := RegExReplace(ThisLine,fnSearchTerm,"<font color=""#9F000F""><b>$0</b></font>") ; highlight the search term in the line
MatchingFileContentsHTML .= "<a href=""" ThisFilepath """ title=""" ThisFilepath """>" ThisFilename "</a>" StrReplicate(" ",MaxFilenameLength3-ThisFilenameLength) " " ThisLineNum StrReplicate(" ",5-StrLen(ThisLineNum)) ": " ThisLine StrReplicate(" ",MaxLineLength3-ThisLineLength) " " ThisFileDir "<br>`r`n"
}
MatchingFileContentsHTML := StrReplace(MatchingFileContentsHTML," 0000"," ")
MatchingFileContentsHTML := StrReplace(MatchingFileContentsHTML," 000" ," ")
MatchingFileContentsHTML := StrReplace(MatchingFileContentsHTML," 00" ," ")
MatchingFileContentsHTML := StrReplace(MatchingFileContentsHTML," 0" ," ")
; enclose in html tags
PreOpenTag := "<pre><span class=""inner-pre"" style=""font-size: 11px"">"
PreCloseTag := "</span></pre>"
MatchingFolderNamesHTML := PreOpenTag MatchingFolderNamesHTML PreCloseTag
MatchingFileNamesHTML := PreOpenTag MatchingFileNamesHTML PreCloseTag
MatchingFileContentsHTML := PreOpenTag MatchingFileContentsHTML PreCloseTag
; get html template
FileRead, HTMLText, %RegExFileSearchResultsHTMLTemplate%
; write search term to html file
BodyText := "Search results for """ ReplaceHtmlDecodedChars(fnSearchTerm) """ in " ReplaceHtmlDecodedChars(fnSearchRootFolder) "<br><br>" ; html
; write results to html file
BodyText .= "<b>Matching folder names:</b>" "<br>" MatchingFolderNamesHTML "<br><br>"
BodyText .= "<b>Matching file names:</b>" "<br>" MatchingFileNamesHTML "<br><br>"
BodyText .= "<b>Matching file contents:</b>" "<br>" MatchingFileContentsHTML "<br><br>"
StringReplace, BodyText, BodyText, xBodyTextx, %BodyText%
FileAppend, %BodyText%, %RegExFileSearchResults%
; view the results
Run, %RegExFileSearchResults%
}
Catch, ThrownValue
{
ReturnValue := !ReturnValue
CatchHandler(A_ThisFunc,ThrownValue.Message,ThrownValue.What,ThrownValue.Extra,ThrownValue.File,ThrownValue.Line,0,0,0)
}
Finally
{
}
; return
Return ReturnValue
}
/* ; testing
SearchTerm := "report"
SearchRootFolder := A_MyDocuments "\_nbell\_SQLScripts"
ReturnValue := RegExFileSearch(SearchTerm,SearchRootFolder)
MsgBox, RegExFileSearch`n`nReturnValue: %ReturnValue%
*/