new to Corona SDK, and i'm trying to figure out a way to load and save a file (which stores game data) on the simulator. (i dont want to have to debug on real device and take 15 seconds just to see a variable change each time).
i followed the tutorial on here: http://www.coronalabs.com/blog/2011/08/03/tutorial-exploring-json-usage-in-corona/ and couldn't find anything on stackoverflow that already addresses this issue.
right now i have the following code for reading and storing files:
local readJSONFile = function( filename, base )
-- set default base dir if none specified
if not base then base = system.ResourceDirectory; end
-- create a file path for corona i/o
local path = system.pathForFile( filename, base )
-- will hold contents of file
local contents
-- io.open opens a file at path. returns nil if no file found
local file = io.open( path, "r" )
if file then
-- read all contents of file into a string
contents = file:read( "*a" )
io.close( file ) -- close the file after using it
end
return contents
end
local writeToFile = function( filename, content )
-- set default base dir if none specified
if not base then base = system.ResourceDirectory; end
-- create a file path for corona i/o
local path = system.pathForFile( filename, base )
-- io.open opens a file at path. returns nil if no file found
local file = io.open( path, "w" )
if file then
-- write all contents of file into a string
file:write( content )
io.close( file ) -- close the file after using it
end
end
it seems to work because i'll read my JSON file, save it with different data, load it, and that seems to persist. HOWEVER, as soon as i close my IDE, the changes are gone. Furthermore, my actual file on my system (mac book pro) is NOT changing.
if i do:
local json = require "json"
local wordsData = json.decode( readJSONFile( "trivia.txt" ) )
wordsData.someKey = "something different"
writeToFile("trivia.txt", json.encode( wordsData ) ) -- this only works temporarily
i'm reading my trivia.txt file which is in the same directory as my main.lua and attempt to change and load something. However, the above code will NOT make the actual change to the trivia.txt on my mac book pro.
what's the proper way to do this?? i need to store game settings and game data (this is a trivia app, i need to store up to 50 words and what answer the user picked). I need to store the data in such a way that when i close my IDE, it'll remember what i wrote to file.
my guess is that when i load my trivia.txt, it's actually looking at my mac book pro for that file, every time i load up my IDE. but then when i run it on my simulator the first time, it creates a new trivia.txt in some temporary folder (which i have no idea where this is). and then it will start reading from there if i re-run the same code. right?
any help would be much appreciated!!! upvotes for more detailed answers, since i'm new to Corona SDK
I suggest you to use system.DocumentsDirectory for the path. First you can read from resource directory and then you can store it in DocumentsDirectory. After that you can always look for DocumentsDirectory. This will solve your problem. Here some functions for you to be able to check if file exist or not. You can modify paths of course
function saveTable(t, filename)
local path = system.pathForFile( filename, system.DocumentsDirectory)
local file = io.open(path, "w")
if file then
local contents = json.encode(t)
file:write( contents )
io.close( file )
return true
else
return false
end
end
function loadTable(filename)
local path = system.pathForFile( filename, system.DocumentsDirectory)
local myTable = {}
local file = io.open( path, "r" )
local contents = ""
if file then
-- read all contents of file into a string
local contents = file:read( "*a" )
myTable = json.decode(contents);
io.close( file )
return myTable
end
return nil
end
Related
I am trying to archive a file. included in the image is the settings of my system file task. I'm trying to send a file whose name is stored in a variable within the loop, and send that file into the requisite folder.
The file is supposed to land in F:\DATA\ARCHIVE\WELLBORE\ using the filename it was given. However, it keeps landing in F:\DATA\ARCHIVE with the NAME WELLBORE, which is not what I am looking for.
What do I have missing?
Thanks
Problem
The issue is when you select Move File so you have to select the source full file path and the destination full file path, so the destination variable should contains the destination full path (with file name) and if the variable contains a folder path, the folder name will be considered as a filename and it will throw an exception.
Solution
Create a new variable #[User::DestinationFile] and assign the following expression to it:
#[User::ArchiveFolder] + RIGHT( #[User::WellBoreFile] , FINDSTRING(REVERSE( #[User::WellBoreFile] ) , "\\", 1) - 1)
This expression will add the filename to the destination path. And use this new variable as a Destination
References
SSIS EXPRESSION TO GET FILE NAME FROM FULL PATH
SSIS Expression to get filename from FilePath
Microsoft Docs article*
Good evening,
I am currently working on a programm that takes information from a file into a Database, for testing purposes I used to open Testfiles in the classical way via IO:
function reader (file, delimeter)
local f = io.open(file)
for line in f:lines() do
lines[count] = splitty(line, delimeter)
count = count + 1;
end
end
(this part also containes the first part of a splitter)
But in the actual environment, the database programm imediatly moves the file in another directory with a name change to, for example this:
$30$15$2016$09$26$13$27$24$444Z$.Pal.INV.csv
Now I know the directory but I can't really predict the name, so I wanted to know if there might be a way to open files without knowing their name.
(and delete them after reading them)
I had ideas to use a modified link:
local inputFile = "D:\\Directory\\(*all)"
but it failed.
Other aviable information:
The system is until now only planned on Windows PCs.
The directory will always only contain the one file that is to ready, no other files.
You can use the lfs.dir iterator from LuaFileSystem to iterate through the contents of the directory. A small example:
local lfs = require("lfs")
local path = "D:\\Directory\\" -- Your directory path goes here.
for filename in lfs.dir(path) do
print(filename) -- Work with filename, i will just print it
end
If you keep a record of the files you will be able to know which one is the new one. If it is only one file, then it will be easier, you can just check the extension with a string function. From what i remember the iterator includes .. and .. lfs documentation can be found here.
-- directory name and file name should consist of ASCII-7-bit characters only
local dir = [[C:\Temp\New Folder]]
local file = io.popen('dir /b/s/a-d "'..dir..'" 2>nul:'):read"*a":match"%C+"
if not file then
error"No files in this directory"
end
-- print the file name of first file in the directory
print(file) --> C:\Temp\New Folder\New Text Document.txt
I want to save my files within a folder but the issue which raising is that my files are storing outside of the folder which is annoying. I am sharing what I have done so far.
-- get raw path to app's Temporary directory
local doc_path = system.pathForFile( "", system.DocumentsDirectory )
-- change current working directory
local success = lfs.chdir( doc_path ) -- returns true on success
local new_folder_path
if success then
lfs.mkdir( "MyNewFolder" )
new_folder_path = lfs.currentdir() .. "/MyNewFolder"
end
local filePath = system.pathForFile( dataFileName , new_folder_path )
r = media.newRecording(filePath)
--print("new recording has been started with a name"..dataFileName)
r:startRecording()
but my recorded file is out of this newly created folder
can someone help me here?
I searched out and finally got the answer that the problem.This is how to create a new recording in specified folder
r = media.newRecording( new_folder_path.."/"..dataFileName )
This line would automatically create a file in this folder as mentioned in the question.
system.pathForFile() only creates a file automatically when the second argument is a base directory like system.DocumentsDirectory (as it is clearly mentioned in the documentation that the second argument is constant and it should be only any base directory).
so if you want to create a new file r want to find the path of the file you would append folder name before the file name like my folder/my file
Hope this would help
here's what i've been trying to do with lua.
i have a ton of pictures i transfer back and forth between 2+ computers. I always end up having multiple copies of pictures. I would like my lua code to get all the file names from the folder and allow me to input a search filter (a file name) that the program will compare to the list of file names and let me know if it's already in the folder or not. I've been racking my brain for the past few days to get this to work but no luck, all i've got so far is the text box made and i can get lua to return data about the files in the folder with this:
local f = io.popen("dir \"C:\\mydir\\\"")
if f then
print(f:read("*a"))
else
print("failed to read")
end
Standard Lua has no mechanism for doing what you want. It's a scripting language designed for embedded systems, unlike Python which has a massive, feature-rich runtime library.
If you want to do this, you should consider using the Lua FileSystem module. It can get directory lists and that sort of thing.
How to get an array of filenames from some folder:
local function get_files(path, prepend_path_to_filenames)
if path:sub(-1) ~= '\\' then
path = path..'\\'
end
local pipe = io.popen('dir /b/a-d "'..path..'*.*" 2> nul')
local output = pipe:read'*a'
pipe:close()
-- If your file names contain national characters
-- output = convert_OEM_to_ANSI(output)
local files = {}
for filename in output:gmatch'[^\r\n]+' do
if prepend_path_to_filenames then
filename = path..filename
end
table.insert(files, filename)
end
return files
end
local array_of_files = get_files('C:\\mydir', false)
for _, fn in ipairs(array_of_files) do
print(fn)
end
Empty array will be returned in case of path not exists (no distinction between empty folder and non-existing folder).
The following code shows the problem I am experiencing:
// Assume working directory is empty.
File foo = new File("asdf");
foo.createNewFile(); // returns true, creates file "asdf" in working directory.
File bar = new File("asdf");
bar.mkdir(); // returns false
When I try to make a directory with the same name as a file that already exists, the "mkdir()" function returns false.
A similar problem occurs when the operations are performed in the opposite order; when the directory is made first, the "createNewFile()" function returns false.
I understand that when the second "File" object is initialised it 'finds' the file created on the previous line therefore "bar.exists() && bar.isFile()" is true.
Please could someone detail how I can create a file with the same name as an existing folder and vice-versa.
Thanks,
Harri
It's impossible, since your operating system (file system) does not permit it. Not a Java issue as such.
You can't create a file and a folder with the same name and in the same folder. The OS would not allow you to do that since the name is the id for that file/folder object.
Assume it was possible and we'd have something like this:
foo (folder)
|- bar (folder)
|- bar (file)
How would you decide which one to open when you get a command "open foo/bar"?
If you can't decide with just that information, then how should the OS decide for you?
It will not be possible as it's your operating system that doesn't allow it. You can always try and if it fails rename the folder (or file):
File bar = new File("asdf");
if(!bar.mkdir()) {
// rename your folder or file
bar.mkdir();
}
// Assume working directory is empty.
File foo = new File("asdf.txt");
foo.createNewFile(); // returns true, creates file "asdf" in working directory.
File bar = new File("asdf");
bar.mkdir(); // returns false
The above code should work for you. Whenever you create file, give some extension so file and directory can be differentiated.