Bueno, esta función es un FileFind que buscará todos aquellos archivos que coincidan con nuestra tabla de cadenas de búsqueda. Requiere la dll luafilesystem (lfs.dll):
Se utiliza así:
Siempre me gusta usar más las funciones que lua y sus módulos otorgan porque son mucho (pero muchísimo) más rápidas que las incorporadas en el AMS. También el uso del os.clock me gusta para determinar cuantos segundos y milisegundos tarda en realizar la búsqueda :D
--Globales
require"lfs";
function FileFind(sPath, tSearch, bSubFolders, bCaseSensitive, tToAdd, callback)
local tS = (type(tSearch=="table")) and tSearch or {""};
local bC = bCaseSensitive or false;
if #tS > 0 then
for x, y in pairs(tS) do
local a = (bC==true) and y or string.gsub(y, "(.)", function(s) return string.lower(s) end);
a = string.gsub(a, "(.)*(.)", function(s, sa) return "%b"..s..sa end);
a = string.gsub(a, "*(..)", function(s) return "%b"..s end);
a = string.gsub(a, "(..)(.)%.", function (s, ss) return (s=="%b") and s..ss.."." or s..ss.."%." end)
a = string.gsub(a, "^%.", "%%.")
tS[x] = string.gsub(a, "?", ".");
end
end
return FileFindB(sPath, tS, bSubFolders, bC, tToAdd, callback);
end
function FileFindB(sPath, tSearch, bSubFolders, bCaseSensitive, tToAdd, callback)
local tReturn = (type(tToAdd)=="table") and tToAdd or {};
local tDirs = {};
if lfs.chdir(sPath) then
for x in lfs.dir(sPath) do
if x ~= "." and x ~= ".." then
if lfs.attributes(x, "mode") == "file" then
for y, z in pairs(tSearch) do
local a = (bCaseSensitive==false) and x or string.gsub(x, "(.)", function(s) return string.lower(s) end);
if string.find(a, z) then
table.insert(tReturn, #tReturn+1, sPath.."\\"..x);
if callback ~= nil then
local b = callback(sPath.."\\"..x, #tReturn);
if not b then Application.ExitScript(); end
end
end
end
elseif lfs.attributes(x, "mode") == "directory" then
if bSubFolders then
table.insert(tDirs, #tDirs+1, sPath.."\\"..x);
end
end
end
end
if #tDirs > 0 and bSubFolders then
for x, y in pairs(tDirs) do
FileFindB(y, tSearch, true, bCaseSensitive, tReturn, callback);
end
end
return (#tReturn>0) and tReturn or nil;
else
return nil;
end
end
local t1 = os.clock()
local tFiles = FileFind("C:\\Windows", {".dat", "*.dll", "s*.exe"}, true, false, nil);
if tFiles then
Debug.ShowWindow(true);
Debug.Print("Files: "..#tFiles.."\r\nTime: "..(os.clock()-t1).."\r\n"..ptbl(tFiles));--ptbl es una función para imprimir tablas
else
Dialog.Message("", "NoFiles");
end