Jump to content
  • 0

How to overwrite current file script?


estehmanis

Question

I know I can overwrite a file using something like:

file:write("overwritten");

If file is opened in "w" mode. But somehow I cannot overwrite current file script.

Is this kind of thing not allowed? or what?

 

Because I would like to update my script, by making request online. And I don't want to write into another file, instead as much as possible overwrite current file.

How to do this?

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

I had this issue before, and i made this solution:

- rename the current file by something else

- write in it

- then rename to the original name

local file_name = gg.getFile():match('[^/]+$')
local new_name = "banana.lua"

os.rename(file_name, new_name)
local script = io.open(new_name, "w+")
script:write("gg.alert('Hello i am the new auto updated script')")
script:close()
os.rename(new_name, file_name)

 

Explanation:

local file_name = gg.getFile():match('[^/]+$')

This statement return the current script name, (Credit to the regex maker)

 

local new_name = "banana.lua"

This statement we just save the name that will be used to rename temporally the script

 

local script = io.open(new_name, "w+")

This statement open the the current running file in write mode, erase all previous data (blank file), (for more info check lua documentation)

 

script:write("gg.alert('Hello i am the new auto updated script")

This statement write to the file, here you write the new script content

 

script:close()

Close the early opened file

 

os.rename(new_name, file_name)

Rename the file back to is original name

Link to comment
Share on other sites

1 hour ago, MAARS said:

I had this issue before, and i made this solution:

- rename the current file by something else

- write in it

- then rename to the original name

local file_name = gg.getFile():match('[^/]+$')
local new_name = "banana.lua"

os.rename(file_name, new_name)
local script = io.open(new_name, "w+")
script:write("gg.alert('Hello i am the new auto updated script')")
script:close()
os.rename(new_name, file_name)

 

Explanation:

local file_name = gg.getFile():match('[^/]+$')

This statement return the current script name, (Credit to the regex maker)

 

local new_name = "banana.lua"

This statement we just save the name that will be used to rename temporally the script

 

local script = io.open(new_name, "w+")

This statement open the the current running file in write mode, erase all previous data (blank file), (for more info check lua documentation)

 

script:write("gg.alert('Hello i am the new auto updated script")

This statement write to the file, here you write the new script content

 

script:close()

Close the early opened file

 

os.rename(new_name, file_name)

Rename the file back to is original name

It works, thanks.

I had tried this os.rename just before I posted this. But it didn't rename the file, then I learned, it turns out that is because the given argument is full path to file.

When I tried your code, it renames it.

Link to comment
Share on other sites

1 minute ago, estehmanis said:

It works, thanks.

I had tried this os.rename just before I posted this. But it didn't rename a file, then I learned, it turns out that is because the given argument is full path to file.

When I tried your code, it renames it.

os.rename only take file name,he will search in the full disk

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

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.