DIRWALK
DIRWALK dir [, wildcards] [USE f(x)]
Walk through the specified directory dir
and its
subdirectories. The user defined function f(x)
takes
x
as a parameter. x
contains information about
the current file. f(x)
must return zero to stop the
process. wildcards
can be used to filter the files.
Example 1: Print all files
FUNC PRNF(x)
? xTRUE
PRNF=END
DIRWALK "." USE PRNF(x)
Example 2: Create a list of all files
FUNC ListFiles(x)
FileList << xreturn true
END
DIRWALK "." USE ListFiles(x)
for n in FileList
print n.path, n.name
next
Example 3: Search for a certain file using wildcards
FUNC ListFiles(x)
FileList << xreturn true
END
DIRWALK ".", "scratch.bas" USE ListFiles(x)
for n in FileList
print n.path
next
Example 4: Search a certain file using user defined function
func SearchFile(x)
if(x.name == "scratch.bas")
path = x.pathreturn false ' file found, stop dirwalk
endif
return true ' file not found yet, continue dirwalk
end
DIRWALK "." USE SearchFile(x)
print path
Example 5: Using wilcards
FUNC ListFiles(x)
FileList << xreturn true
END
DIRWALK ".", "*.bas" USE ListFiles(x)
for n in FileList
print n.path, n.name
next
Example 6: File list utility
' Note: this demo is also a useful utility (version 2)
' -------------------- Start of Demo search ---------------------
' "." = Start search in current directory:
"."
dir = ' Exclude some binary files to speed up search (it's not case sensitive):
"jpeg jpg png gif mp3 mp4 zip 7z exe dat"
exclude_ext = ' Search this "string":
"FUNC "
word = ' 0 = Not case sensitive search; 1 = Case sensitive search:
0
case_sensitive = ' Search it!
search_word dir, exclude_ext, word, case_sensitive
' ' Demo only for dir_list():
' a = dir_list(dir, exclude_ext)
' For i In a Do ? i
' ? " - Length of list: "; Len(a)
' -------------------- End of Demo search ---------------------
' Purpose: return sorted array of directory files-list (including files in
' sub-directories, but not directory-names).
' Details: "dir" is the top directory to start search from (default = ".");
' "exclude_ext" is a Not case sensitive string of file-extension(s)
' to exclude from list (default = ""), For example:
' "jpeg jpg zip z7 gif wav mp3" ' Space delimited extensions.
' Example:
' a = dir_list("", "") ' a is all files in current dir (and its sub-dirs).
' a = dir_list("..", "bak zip") ' a is files in upper dir, exclude .bak .zip
Func dir_list(dir, exclude_ext)
Local a, ext
' Verify "dir" ("." is current directory):
Trim(dir): If dir = "" Then dir = "." '
dir = ' Exclude extension in Any case (for Windows):
Enclose(Lcase(Squeeze(exclude_ext)), " ")
ext =
' Make the dir list (DIRWALK):
Func make_list(f)
Local e ' File Extension (without ".")
If Isfile(f) Then ' Don't add directory-names to list
Enclose(Lcase(Rightoflast(f, ".")), " ")
e = If Not (e In ext) Then a << f ' Exclude extension? or append file
Fi
True ' (True = Continue to walk)
make_list = End Func
Dirwalk dir Use make_list(x) ' (Note: parameter name must be 'x')
' Sort dir list and ignore case (SORT):
Func no_case(x, y)
Lcase(x)
x = Lcase(y)
y = Select Case 1
Case x = y: no_case = 0
Case x > y: no_case = +1
Case Else : no_case = -1
End Select
End Func
Sort a Use no_case(x, y)
dir_list = aEnd Func
' Purpose: search for a "word" in "dir" (and its sub-dirs), and print results.
' Details: for "dir" and "exclude_ext" details - See dir_list() function.
' "word" is the string to search for.
' 'cs' is case sensitive search - True or False.
Sub search_word(dir, exclude_ext, word, cs)
Local w, file, list, l, lines, p ' For Searching
Local c_page, c_line ' Counters
Local t_lines, t_files, old_t_lines ' Totals (for sum)
Local LINES_PER_PAGE, attr = 3
10 ' How many lines to print before pause?
LINES_PER_PAGE =
' Get the list of all files
list = dir_list(dir, exclude_ext) Iff(cs, word, Ucase(word)) ' Case sensitive search?
w = Cat(0); Chr(12); Cat(attr); ' Clear console attributes and screen
? ' Load and search in each file:
For file In list
Tload file, lines
0
c_line =
' Search in each line:
For l In lines
' Keep line # for [print the line]
c_line++ ' Look for the word in this line:
If cs Then
Instr(l, w)
p = Else
Instr(Ucase(l), w)
p = Fi
' If word found then print the line:
If p Then
Cat(2); file + " [" + c_line + "]"; Cat(-2); " "; Mid(l, p)
?
c_page++If c_page = LINES_PER_PAGE Then c_page = 0: Pause
' Keep total lines # for sum
t_lines++ Fi
Next l
If t_lines > old_t_lines Then
old_t_lines = t_linesCat(attr); ' Change color on new file
attr = -attr: ? ' Keep total files # for sum
t_files++ Fi
Next file
' Print sum:
If c_page <> 0 Then Pause
Cat(0) ' Clear all console attributes
? Color 15
"- Search in: "; Enclose(dir)
? "- Exclude file-extension(s): "; Enclose(Squeeze(exclude_ext))
? "- Search "; Iff(cs, "IS", "is NOT"); " case sensitive."
? "- Found "; Enclose(word); &
? " in "; t_lines; " lines, in "; t_files; " files "; &
"(out of "; Len(list); " files)."
?Color 7
"Done. Press Backspace key...";
? While Inkey <> Chr(8): Wend
End Sub