I am new to corona and want to add a functionality in my app that on pressing back button, all data stored by the user get deleted from the documents directory. In short I want to know is there a way to empty documents directory?
Yes, you would use the LFS (Lua File System) module for that. See:
http://www.coronalabs.com/blog/2012/05/08/luafilesystem-lfs-tutorial/
Use this to delete all the files in /documents directory
local lfs = require "lfs";
local doc_dir = system.DocumentsDirectory;
local doc_path = system.pathForFile("", doc_dir);
local resultOK, errorMsg;
for file in lfs.dir(doc_path) do
local theFile = system.pathForFile(file, doc_dir);
if (lfs.attributes(theFile, "mode") ~= "directory") then
resultOK, errorMsg = os.remove(theFile);
if (resultOK) then
print(file.." removed");
else
print("Error removing file: "..file..":"..errorMsg);
end
end
end
Related
I typed up my code not expecting it to work first try, and of course it didn't. I kept tweaking it for hours on end, but I kept getting the same result until I made as simple as possible.
local file = io.open("File_Name", "r")
io.output(file)
local test = io.read('*all')
io.close(file)
print(test)
After getting (no return) from this, I've decided to take a break and let someone else answer my question.
The problem with your code is that you're trying to read from whatever is defined as your input file. You only opened a file, but you didn't tell Lua to use it as the input file, so io.read won't read from the opened file, yet.
local file = io.open(filename, "r")
local test = file:read("a")
io.close(file)
print(test)
Alternatively:
local file = io.open(filename, "r")
io.input(file)
local test = io.read("a")
io.close(file)
print(test)
or
local file = io.open(filename, "r")
local test = io.input(file):read("a")
io.close(file)
print(test)
Of course you should check wether opening the file succeeded befor using the file handle.
Depending on your Lua version the read format is either *a or a. I cannot remember if both is ok in all versions. At least that's what the manual says.
I'm doing a project in corona simulator that involves reading some data in from a csv file ("car.csv") that is in the project directory, I have this code that is supposed to read the first line in, but when I run it it gives me the error "Attempt to index local 'file' (a nil value)". Any idea how I can fix this?
local function init()
local path = system.pathForFile( "car.csv", system.DocumentsDirectory );
local file = io.open(path, "r");
line = file:read();
print(line);
end
For some reason it won't read it in to 'file'.
Edit: Ok if I use the full path rather than the relative file path it works. But I need to use the relative one and I don't know why it doesn't work.
When using io.open, you should always make sure that it actually succeeded before trying to read the file, or you will get an ugly "attemp to index nil" error.
If you want your program to still crash, just do
local file = assert(io.open(path, 'r'))
And it will give you a more helpful error message if the file can't be found. Alternatively, you could also manualle save the return values of io.open and check for errors:
local file, err, code = assert(io.open(path))
if not file then
print("Error opening file", path, err)
-- Do something to handle the error
end
I am trying to copy only latest file from a directory using lua file.
Latest file means : depends on modified time/created time.
How can i do this?
Referring to this question: How can I get last modified timestamp in Lua
You might be able to leverage the io.popen function to execute a shell command to get the name of the file. It seems like there's no builtin function that exposes filesystem metadata or stats. Something like this might work:
local name_handle = io.popen("ls -t1 | head -n 1")
local filename = name_handle:read()
I'm not familiar with Lua, but perhaps this helps. I imagine that once you have the name of the newest file you can use the other IO functions to do the copying.
local function get_last_file_name(directory)
local command = 'dir /a-d /o-d /tw /b "'..directory..'" 2>nul:'
-- /tw for last modified file
-- /tc for last created file
local pipe = io.popen(command)
local file_name = pipe:read()
pipe:close()
return file_name
end
local directory = [[C:\path\to\your\directory\]]
local file_name = get_last_file_name(directory)
if file_name then
print(file_name)
-- read the last file
local file = io.open(directory..file_name)
local content = file:read"*a"
file:close()
-- print content of the last file
print(content)
else
print"Directory is empty"
end
How can I put the file created by io.open somewhere besides the folder the lua file is in.
local test = io.open("Test.html", "w")
test:write("Hello world.")
test:close()
Yoy simply supply a full path (or relative path) and not just a filename.
local test = io.open("c:\temp\Test.html", "w")
In my app I have a structure similar to the following:
app (baseDirectory)
|
+-> config
|
+-> Levels
When using corona I am trying to autoload all files in the Levels directory. Below is how I am currently doing it. Note: This does work on Windows just not mac.
local dirPath = system.pathForFile('config/Levels')
for file in lfs.dir(dirPath) do
if (file ~= '.' and file ~= '..') then
-- require the file
end
end
Now if I use the following it works on Mac but not the 'config/Levels'.
local dirPath = system.pathForFile('config')
I'm not sure if this is a bug or if I am doing something wrong. I would assume since it works on Windows but not on Mac that it would be a bug.
So in conclusion how can I get the following to work with the above directory structure
local dirPath = system.pathForFile('config/Levels')
After much debugging I have figured out the solution for Mac. Below is my function for getting files in a directory. Works for both Windows and Mac.
Must have a file named main.lua in the baseDirectory.
-- path is relative to the baseDirectory
function getFiles(path)
local filenames = { }
local baseDir = system.pathForFile('main.lua'):gsub("main.lua", "")
for file in lfs.dir(baseDir .. path) do
if (file ~= '.' and file ~= '..') then
table.insert(filenames, file)
end
end
if (#filenames == 0) then return false end
return filenames
end
The problem is system.pathForFile('directory/subdirectory') does not work on Mac as it is strictly looking for a file. As long as it can find some file in the baseDirectory you can append on the relative path to retrieve all files in the intended directory.
One solution is to change "Levels" in "levels".
Maybe it is case sensitive.
Second solution is to not use "levels" directory inside config folder.
Here is how I suggest to organize your files:
app (baseDirectory)
|
+-> • configLevels
• configPlayer
• configOther