NO

Funções Para Tratamento De Arquivos

Por Nostradamus, 16 de out. de 2007 em Tutoriais de Scripting

5
2.5k
NostradamusN
16 de outubro de 2007 às 20:52

Desenvolvi algumas funções para tratamento de arquivos fácilmente, não é como a do Colex, que por sinal, é mais completa, mas, já ajuda.

 

function File:Exists(filename)
 local file = io.open(filename, "r")
 if (file == nil) then 
return false 
 else
file:close()
return true  
 end	
end

function File:Create(filename, content)
if (content == nil) then return false end
local file = io.open(filename, "w")
if (file == nil) then return false end
file:write(content)
file:flush()
file:close()


return true
end

function File:Save(filename, content)
 if (content == nil) then return false end
 local file = io.open(filename, "w+b")
 if (file == nil) then return false end
 file:write(content)
 file:flush()
 file:close()
 return true
end

function File:Load(filename)
 local file = io.open(filename, "r")
 if (file == nil) then return nil end  
 local load = file:read("*all")
 file:close()
 return (load)
end

function File:LoadAsTable(filename)
 local file = io.open(filename, "r")
 if (file == nil) then return nil end
 local load = {}
 while true do
  local line = file:read("*l")  
  if (line == nil) then break end
  table.insert(load, line)
 end
 file:close()
 return load, table.getn(load)
end

function File:GetExtension(filename)
local fileExt = nil
for w in string.gfind(filename, "(%.%a*)") do
	   fileExt = string.lower(string.sub(w, 2, 10))
end 
return fileExt
end

function File:GetName(filename)
local rFileName = nil
for w in string.gfind(filename,"(%w*%.%w*)") do 
	   rFileName = string.lower(string.sub(w, 1, string.find(w, "%.")-1))
end 
return rFileName
end

 

Creio que o nome das funções se auto-explicam, mas se tiverem dúvidas, é só postarem.

colexC
16 de outubro de 2007 às 20:57

Ficou muito bom e organizado

gostei principalmente da LoadAsTable

 

Abraços,

Colex

6 meses depois...
milenium666M
01 de abril de 2008 às 16:25

ond q eu coloco isso ?

desculpa perguntar isso .. =P

eh q eu tava tentando achar ond coloca x.x

3 meses depois...
xdtibia16X
10 de julho de 2008 às 14:30

é VBScript?

Vou Ver.

5 meses depois...
EventideE
11 de dezembro de 2008 às 12:06

VB? lawl

onde coloca? coloca no global.lua

muito bom, vai ajudar muito essas funções :-P

Editado Dezembro 11 por Eventide