checking in a folder with a filename exists and check the number of such files

Hi, I am a newbie to lua. I am saving files after analysis. I want to create a code to do following steps:

Step 1) search in a folder if there is a file with “filename = name*.dat” exists. (Here ‘*’ represents whatever the extension is. I use this in Matlab). Not sure how to do this in lua. I want to check for all files name_1.dat, name_2.dat…name_n.dat.

Step2) If any files with "name*.dat) exists, find the number of such files

Step3) then define variable n = number of such files

Step4) n=n+1

Step5) new_file_name = name_n.dat

How to make a Lua code for this?

Hey aneps,

you can try something like this:

local function searchFiles(filename, n, location) local path = system\_pathForFile(filename, location) for i=1, n do local file = io.open( path, "r" ) --open file to read if file then --file exists, do whatever you want (e.g. get its content) local contents = file:read( "\*a" ) io.close(file) --don't forget to close the opened file else --file does not exist, do whatever you want (e.g. create one and save its name in it) local file = io\_open(path, "w") file:write("name\_"..i) io.close(file) --don't forget to close the opened file end end end local myFilename = "name\_" local myN = 5 local myLocation = system.DocumentsDirectory searchFiles(myFilename, myN, myLocation)

It searches for files up to a certain value of n, if a file does not exist it creates one.

You can also look at the lfs.* API’s.  It basically lets you get a directory listing.

Rob

Thank you verymuch… I have one more question. if file exist, I want to know how many files with that filename exist. For example, if I have in the directory: filename_A.dat, filename_B.dat, filename_C.dat, I have 3 files with the same filename+some extension. How I can count there are 3 filename+some extension existing in the folder? Why I am looking for this is when I save a data with some filename, I dont want it to be overwritten instead create new files numbering successively.

Moreover, in this code, as far as I understood ‘n’ in the for loop is the number of files. I don’t know how many such files existing. The idea is first to look for how many files with that filename+somextension existing in the folder!

Hey aneps,

you can try something like this:

local function searchFiles(filename, n, location) local path = system\_pathForFile(filename, location) for i=1, n do local file = io.open( path, "r" ) --open file to read if file then --file exists, do whatever you want (e.g. get its content) local contents = file:read( "\*a" ) io.close(file) --don't forget to close the opened file else --file does not exist, do whatever you want (e.g. create one and save its name in it) local file = io\_open(path, "w") file:write("name\_"..i) io.close(file) --don't forget to close the opened file end end end local myFilename = "name\_" local myN = 5 local myLocation = system.DocumentsDirectory searchFiles(myFilename, myN, myLocation)

It searches for files up to a certain value of n, if a file does not exist it creates one.

You can also look at the lfs.* API’s.  It basically lets you get a directory listing.

Rob

Thank you verymuch… I have one more question. if file exist, I want to know how many files with that filename exist. For example, if I have in the directory: filename_A.dat, filename_B.dat, filename_C.dat, I have 3 files with the same filename+some extension. How I can count there are 3 filename+some extension existing in the folder? Why I am looking for this is when I save a data with some filename, I dont want it to be overwritten instead create new files numbering successively.

Moreover, in this code, as far as I understood ‘n’ in the for loop is the number of files. I don’t know how many such files existing. The idea is first to look for how many files with that filename+somextension existing in the folder!