简体   繁体   中英

How can i make sure a path exists in quirrel

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM