0

I am reading and then modifying some data in a file that is located in the Windows program files folder. Because of this the file does not have write permissions. So I receive an error when trying to write to it.

When manually giving write permissions to the folder containing the file, the script will work.

Is it possible to modify a folders permissions within a Lua script, or write to a file regardless of it's permissions? Or can this only be done manually from the File Explorer?

This is my current section of code that is re-writing the inputFile:

k=0
updatefile = io.open(inputFile,"w")

for k=1,i do     
    updatefile:write(modfilecontent[k] .. '\n')    --This is where I'm blocked by permissions
end

updatefile:close()

Also if there is a solution possible, and I load this script onto another persons computer that does not have admin privileges would the solution still work?


EDIT: Piglet's solution will work using the os.execute method to change privileges. As long as the software, that the plug-in is being used in, is ran as admin the first time the permissions will be set. From then on you no longer need to run as admin.

Below is the code that I needed to add to the beginning of the program, minus the brackets of course

os.execute('icacls "C:\\Program Files\\[software name]\\[folder name]" /grant Everyone:(OI)(CI)F /T')
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
xelarim
  • 1
  • 1

1 Answers1

0

You could use os.execute to run the appropriate Windows command.

See How to grant permission to users for a directory using command line in Windows?

Alternatively use a library that supports stuff like this. Maybe checkout

https://keplerproject.github.io/luafilesystem/manual.html

Also if there is a solution possible, and I load this script onto another persons computer that does not have admin privileges would the solution still work?

As that person wouldn't be able to run Lua as administrator that is very unlikely. That would completly defy the purpose of different user levels.

Piglet
  • 27,501
  • 3
  • 20
  • 43