| Message |
The function below can be used to scan your disk from any given starting point, recursing into subdirectories. For each file found it calls a supplied function to do something with the file.
function scan_dir (path, f)
-- find all files in that directory
local t = assert (utils.readdir (path .. "\\*"))
for k, v in pairs (t) do
if not v.hidden and
not v.system and
k:sub (1, 1) ~= "." then
-- recurse to process file or subdirectory
if v.directory then
scan_dir (path .. "\\" .. k, f)
else
f (path .. "\\" .. k, v)
end -- if
end -- if
end -- for
end -- scan_dir
It excludes files starting with a "." (which stops it going into the current directory (.) or the owner (..). However if you wanted to process files starting with a dot, you could omit that test, and change it to not process a directory with those exact names.
It also excludes system and hidden files, you could remove those tests too. (See next post for how to do that).
An example of it in use is:
function scan_dir (path, f)
-- find all files in that directory
local t = assert (utils.readdir (path .. "\\*"))
for k, v in pairs (t) do
if not v.hidden and
not v.system and
k:sub (1, 1) ~= "." then
-- recurse to process file or subdirectory
if v.directory then
scan_dir (path .. "\\" .. k, f)
else
f (path .. "\\" .. k, v)
end -- if
end -- if
end -- for
end -- scan_dir
plugins = {}
-- this function is called for every found file
function load_file (name, stats)
if stats.size > 0 and
string.match (name:lower (), "%.xml$") then
table.insert (plugins, name)
end -- if
end -- load_file
scan_dir (GetInfo (60), load_file)
require "tprint"
tprint (plugins) -- display results
In this case I pass the function "load_file" to the "scan_dir" function. For every file found in the directory tree, load_file is called with the file name, and the file information (see utils.readdir for the exact things you get). You might test the file size (as I do above), and use a string.match to narrow the file names down to specific ones.
You could use this to quickly locate plugins, sounds, pictures, MUSHclient world files, or indeed anything you want. The starting location should *not* end with a slash (although GetInfo (60) puts one there, that seems to work anyway). Thus you could scan your entire C drive like this:
scan_dir ("C:", load_file)
I wrote this because I found I was doing lots of variants of directory scanners, with the test for the file names etc. inside the scanner, whereas this version splits the scanning, and processing, into more logical phases. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|