- Offizieller Beitrag
Ich habe mal wieder ein paar Funktionen gebraucht, die standardmäßig in Lua nicht enthalten sind und deshalb eine kleine Bibliothek "FileEx" erstellt.
Vielleicht habt ihr ja Verwendung für. Wenn ihr bereits die shell.dll installiert habt (und diese auch im package.path registriert ist), greife ich darauf zu, anderenfalls über die io-Bibliothek mit CMD - da poppt dann kurz das CMD-Fenster auf.
Folgende Funktionen sind enthalten:
Code
fileexists - Prüfen Datei auf Existenz
getfilesrec - Rekursive Dateiauflistung
getfolderrec - Rekursive Ordnerauflistung
getscriptdir - Abfrage Skriptverzeichnis
list - Liste aller Funktionen des Moduls
requireprotected - Require mit Abfangen Fehlermeldung
syntax.functionname - Syntax der Funktion
Das Einbinden der Bibliothek erfolgt, wie üblich, mit fe = require "FileEx". Natürlich kann man statt fe einen anderen Modulnamen verwenden.
-- Auflisten der Funktionen
fe.list()
-- Syntax Anzeigen (z.B. für "getfilesrec")
fe.syntax.getfilesrec()
Code
-- TIME_STAMP 2016-11-11 19:45:30
do
----------------------------------------------------------------------------------------------------
-- requires a library by protected call
-- on error: returns nil
-- on success: returns the library table
----------------------------------------------------------------------------------------------------
local RequireProtected = function(_lib)
local loaded_lib
if pcall(function() loaded_lib = require(_lib) end) then
return loaded_lib
else
return nil
end
end
-- load the shell library, if exists
-- if not exists, shell has value: nil
local shell = RequireProtected('shell')
----------------------------------------------------------------------------------------------------
-- checks if any file exists
----------------------------------------------------------------------------------------------------
local FileExists = function(_path)
if shell then return shell.fileexists(_path) end
local fh = io.open(_path)
if not fh then return false end
fh:close()
return true
end
----------------------------------------------------------------------------------------------------
-- returns the script directory
----------------------------------------------------------------------------------------------------
local GetCurrentDir = function()
local sFullPathCurrent = debug.getinfo(1).short_src
local pos = sFullPathCurrent:find('\\[^\\]*$')
if pos == nil then pos = sFullPathCurrent:find('/[^/]*$') end
return sFullPathCurrent:sub(1, pos-1)
end
----------------------------------------------------------------------------------------------------
-- returns a table with all recursive files (full path name) from given root
-- gets by default all files, filter with parameter "_filetype", i.e. "lua" or "lua dll" or "lua dll any_other"
-- files can excluded by their attrib, i.e. "hs" or "rs"
-- possible: H-hidden, S-system, L-reparse points, R-read only, A-archive, I-not content indexed
----------------------------------------------------------------------------------------------------
local GetFilesRecursive = function(_rootpath, _filetype, _exclude_attrib)
local tFiles, popen, pfile, pos, ext = {}, io.popen
_rootpath = _rootpath or GetCurrentDir()
_filetype = _filetype or '*'
_exclude_attrib = _exclude_attrib or ''
_exclude_attrib = _exclude_attrib:gsub('(.)', '-%1')
-- write file types into table
local tFiletype = {}
if _filetype ~= '*' then
for v in string.gmatch(_filetype, "%S+") do
table.insert(tFiletype, v)
end
end
-- function to insert file pathes in output table by filtering
local function insert(_file)
if #tFiletype == 0 then
table.insert(tFiles, _file)
else
pos = _file:find('\.[^\.]+$')
ext = _file:sub(pos+1)
for i,v in ipairs(tFiletype) do
if v == ext then table.insert(tFiles, _file) end
end
end
end
-- get all files recursively from _rootpath and filter it
if shell then -- use shell library, if exists
_, result = shell.exec('CMD /c dir "'.._rootpath..'" /b /o:N /s /a-d'.._exclude_attrib, nil, true, true)
local tResult = {}
for v in result:gmatch("[^\n]+") do
table.insert(tResult, (v:gsub('\r', ''):gsub('\n', '')))
end
-- check if files found
if #tResult == 1 then if not shell.fileexists(tResult[1]) then return {} end end
for i,file in ipairs(tResult) do
insert(file)
end
else
local pfile = popen('dir "'.._rootpath..'" /b /o:N /s /a-d'.._exclude_attrib)
local firstfile = true
for file in pfile:lines() do
if firstfile then
if not FileExists(file) then break
else firstfile = false
end
end
insert(file)
end
pfile:close()
end
return tFiles
end
----------------------------------------------------------------------------------------------------
-- returns a table with all recursive sub folders (full path name) from given root
----------------------------------------------------------------------------------------------------
local GetFolderRecursive = function(_rootpath)
local tsub, popen = {}, io.popen
_rootpath = _rootpath or GetCurrentDir()
if shell then
_, result = shell.exec('CMD /c dir "'.._rootpath..'" /b /o:N /s /a:d', nil, true, true)
for v in result:gmatch("[^\n]+") do
table.insert(tsub, (v:gsub('\r', ''):gsub('\n', '')))
end
else
local pfile = popen('dir "'.._rootpath..'" /b /o:N /s /a:d')
for folder in pfile:lines() do
table.insert(tsub, folder)
end
pfile:close()
end
return tsub
end
----------------------------------------------------------------------------------------------------
local FileEx = {
-- Funktionen
fileexists = FileExists,
getfilesrec = GetFilesRecursive,
getfolderrec = GetFolderRecursive,
getscriptdir = GetCurrentDir,
requireprotected = RequireProtected,
list = function() print('fileexists - Prüfen Datei auf Existenz')
print('getscriptdir - Abfrage Skriptverzeichnis')
print('getfilesrec - Rekursive Dateiauflistung')
print('getfolderrec - Rekursive Ordnerauflistung')
print('list - Liste aller Funktionen des Moduls')
print('requireprotected - Require mit Abfangen Fehlermeldung')
print('syntax.functionname - Syntax der Funktion') end,
syntax = {
['fileexists'] = function() print('modul.fileexists(_path)')
print(' _path - Dateipfad der auf Existenz geprüft wird')
end,
['getfilesrec'] = function() print('modul.getfilesrec(_rootpath, _filetype, _exclude_attrib)')
print(' _rootpath - Startpfad (Default = ScriptDir)')
print(' _filetype - String Dateityp-Filter, z.B. "jpeg jpg png" (Default = "*")')
print(' _exclude_attrib - String Datei-Attribute für Ausschluss , z.B. "HS" od. "RS" (Default = "")')
print(' H-hidden, S-system, L-reparse points, R-read only, A-archive, I-not content indexed')
print('Return: Table mit den kpl. Dateipfaden')
end,
['getfolderrec'] = function() print('modul.getfolderrec(_rootpath)')
print(' _rootpath - Startpfad (Default = ScriptDir)')
print('Return: Table mit den kpl. Ordnerpfaden')
end,
['getscriptdir'] = function() print('modul.getscriptdir()')
print('Return: Das Scriptverzeichnis')
end,
['list'] = function() print('modul.list()')
print('Return: Liste aller Funktionen')
end,
['syntax'] = function() print('modul.syntax.Funktionsname()')
print('Return: Syntax der Funktion')
end,
['requireprotected'] = function() print('modul.requireprotected(_lib)')
print(' _lib - Name der zu ladenden Bibliothek')
print('Return: Erfolg - Table der Bibliothek, Fehler - nil')
end
}
}
----------------------------------------------------------------------------------------------------
return FileEx
end
Alles anzeigen