file.Write() to folder

I’m creating an addon that involves saving presets that can be loaded up from a filebrowser. Now whenever you use file.Write, it automatically writes the file to the “data” folder.


file.Write("preset_name",PresetVars)

This code will write this file inside of the data folder, which is bad, because my file browser will see all of the other configs from other addons in there, it would be very annoying.

Just wondering how I can write the file into a specific directory inside or outside of data. There has to be a way, because why could you create a dir, but not write to it.

Any help is appreciated :slight_smile:

[lua]function file.Write( filename, contents )

local f = file.Open( filename, "wb", "GAME" )
if ( !f ) then return end

f:Write( contents )
f:Close()

end[/lua]
Taken from the Garry’s mod Github. This is relative to garrysmod/. I’d also recommend you change the function name, otherwise every function will write to that directory.

So lets say I’m trying to write this file into data/presets

I run this code


file.Write("test_preset.txt",SettingsString,"DATA/presets")

Still nothing :confused:

You can’t write outside of the data folder, and

file.Write only has 2 arguments.

If you want to write into a directory within the data folder, first create it using

file.CreateDir, then write into it using

file.Write.

E.g.



file.CreateDir "mystuff"
file.Write("mystuff/test.txt", "Helloworld")


I am trying to write into a directory within the data folder, but yeah I’ve already tried that


file.CreateDir( "presets" )
file.Write("presets/preset.txt",SettingsString)

That code works fine for me, maybe file permissions?

Oh damn, for some reason my code stopped running when I tried your fix the first time, but it worked. You’ve saved me yet again bigdogmat :slight_smile: