Jump to content
×
×
  • Create New...

Quest Function - Manage File


Recommended Posts

M2 Download Center

Download Here ( Internal )

Hello guys, i made a simple function that allows you to easily manage files. This function let you create, read, write, rewrite or erase lines of text or the hole text.

  • Examples on how to use it:
Spoiler

 

Reading:



code: manage_file("/locale/test/test.txt", "read", "1", "")
what is doing: reading first line if the file test.txt
output: the first line of your file test.txt

 

Adding:

 



code: manage_file("/locale/test/test.txt", "add", "", "example text")
what is doing: it will add a line of file saying "example text"
output: nothing

 

Rewriting:

 



code: manage_file("/locale/test/test.txt", "rewrite", "2", "example text")
what is doing: it will rewrite line 2 of file to "example text"
output: nothing

 

Erasing:



code: manage_file("/locale/test/test.txt", "erase", "3", "")
what is doing: it will erase line 3 of file to "example text"
output: nothing
Ps: when you erase a line the code will automaticly move all lines up for example if i erase line 1, my line 2 will be my line 1 now.

 

 

 

 

  • Here is the function:
Spoiler

 



--This function was made by Frozen
--arguments: path of the file, type, line, text

function manage_file(filename, ctype, line, value)

    if type(value) ~= "string" then  return end

    local file = io.open(filename, "r")
    if not file then
        if sType == "create" then
            local file = io.open(filename, "w")
            file:write("")
            file:close()
        else return end
    end

    local sType = ctype
    local Nline = 0
    local bAllLines = line == "all"


    if not tonumber(line) and not bAllLines then return end

    Nline = tonumber(line)

    if sType == "add" then
        local file = io.open(filename, "a")
        file:write(value, "\n")


    elseif sType == "rewrite" or sType == "erase" or sType == "read" then
        local ltable = {}

        for i in file:lines() do table.insert(ltable, i) end

        if sType == "read" then
            if bAllLines then return ltable
            else return ltable[line] end
        end

        file = io.open(filename, "w+")

        if line == "-1" then Nline = table.getn(ltable) end --last line
        if ltable[Nline] == nil and not bAllLines then return end

        if sType == "rewrite" then
            if bAllLines then file:write(value, "\n")
            else
                ltable[Nline] = value
                for idx, v in ipairs(ltable) do file:write(v, "\n") end
            end

        elseif sType == "erase" then
            if bAllLines then file:write("")
            else
                for idx, v in ipairs(ltable) do
                    if idx ~= Nline then file:write(v, "\n") end
                end
            end
        end

    else return end

    file:close()

end

 

Aditional Information:

  1. You can put in line "all" , and the function will erase/add/rewrite all lines.
  2. You can put in line "-1" and it will count as the last line of the file.
  3. Types: "add", "create", "erase", "rewrite" and "read".

I hope it will be usefull :) ,

Kind Regards,

Frozen

  • Love 7
Link to comment
Share on other sites

4 minutes ago, ds_aim said:

wow fuck This will help me to make patchs for my customers.

Auto install updates. :wub:

Thanks. :)

No problem ;) Im glad i helped.

4 minutes ago, ZenkoKXO. said:

A friendly guy who really helped me with this function; thank you Frozen :)

Thanks, no problem :D you gave the idea .

Kind Regards

Link to comment
Share on other sites

i think its not fair, we see lots of guys in here with the devoleper medal and the only thing they can do is ask for help here in the forum.
And this guy make lots of helpfoul things and he is just a member.
What can some one do to have the devoleper medal/tag name?
Be freind of some one here?
Thanks for shering that Frozen.

  • Love 2
Link to comment
Share on other sites

1 hour ago, Faq said:

i think its not fair, we see lots of guys in here with the devoleper medal and the only thing they can do is ask for help here in the forum.
And this guy make lots of helpfoul things and he is just a member.
What can some one do to have the devoleper medal/tag name?
Be freind of some one here?
Thanks for shering that Frozen.

Thanks my friend, i appreciate it :) 

Link to comment
Share on other sites

  • 4 weeks later...
if not tonumber(line) and not bAllLines then  return end

I've seen a similar function made like 2/3 years ago (can even tell you that some var names are equal), but yours seems better. Nice one :)

Just a tip.

if type(tonumber(line)) ~= "number" and not bAllLines then  return end

The tonumber() function tries to convert the arg into a number, if it can't do that, it'll return a nil value, so you can do this instead:

if not tonumber(line) and not bAllLines then  return end
Link to comment
Share on other sites

On 4/2/2016 at 7:50 PM, 'PACI said:

I've seen a similar function made like 2/3 years ago (can even tell you that some var names are equal), but yours seems better. Nice one :)

Just a tip.

if type(tonumber(line)) ~= "number" and not bAllLines then  return end

The tonumber() function tries to convert the arg into a number, if it can't do that, it'll return a nil value, so you can do this instead:

if not tonumber(line) and not bAllLines then  return end

Yes there is actually a function made by pacificador a long time ago, it is good too, both do almost the same..

I just tried to make my own function and add some new features.

Thanks for the tip i will edit :D

Kind Regards

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.