How can i make sure a path exists in quirrel

87 Views Asked by At

I want to create files in custom subfolders in quirrel (squirrel fork). I have this example code

local function saveToFile(subfolder_name, file_name, content) {
  local path = $"folder/{subfolder_name}/{file_name}"
  local file = io.file(path, "wt+")
  file.writestring(content)
  file.close()
}

But it can't open a file if I don't create needed folders.

1

There are 1 best solutions below

0
On BEST ANSWER

Unfortunetly there is no such native method in squirrel standard library But as you can call system from squirrel you can create folders. See https://quirrel.io/doc/stdlib/stdsystemlib.html#system (quirrel is fork of squirrel with same improvements for safety and performance, but it is almost the same for original squirrel, the only question how you use standard native libraries in your own implementation, AFAIK usually they have native methods in just root table

local {system} = require("system")//this is to add 'system'
method to local scope
system("mkdir myDirName")

You can also add your own functon and bind it squirrel\quirrel or add it "io" standard library and submit a PR

Squirrel is first of all embedded language so its methods to create sideffects are supposed to be added by programmer