How to check if a file exists

Hello,

I’m trying to check if a specific file exists in this path: “/ storage / emulated / 0 / Download /”. Does anyone know how I can make this work?

 local nome = "/storage/emulated/0/Download/" .. "OC VISUAL " .. os.date("%Y%m%d") .. " H" .. composer.getVariable("ResultadoHORA") .. " M" .. composer.getVariable("ResultadoMINUTO") .. ".CSV" function fileExists(fileName, base) assert(fileName, "fileName is missing") local base = base or system.ResourceDirectory local filePath = system.pathForFile( fileName, base ) local exists = false if (filePath) then local fileHandle = io.open( filePath, "r" ) if (fileHandle) then exists = true io.close(fileHandle) end end return(exists) end function Verify(event) if fileExists(nome) then contadorArquivo = contadorArquivo + 1 nome = nome .. " (" .. contadorArquivo .. ")" .. ".csv" VerificarArquivo() print("excist") else ssk.files.util.writeFile( saida, nome) -- TABLET print("not found") end end Verify()

I think it is this way: https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/files/

but I am not able to develop this code =(

  1. I’d start with this (from https://github.com/roaminggamer/SSK2/blob/master/ssk2/extensions/io.lua)::slight_smile:

    function io.exists( fileName, base ) local base = base or system.DocumentsDirectory if( base ) then fileName = system.pathForFile( fileName, base ) end if not fileName then return false end fileName = io.repairPath( fileName ) local attr = lfs.attributes( fileName ) return (attr and (attr.mode == “file” or attr.mode == “directory”) ) end

  2. It looks like you want to check for the existence of files OUTSIDE the app sand-box.  In that case you are on your own regarding assembling the path to that file.  You may want to check the marketplace to see if there is a plugin to help with that.

If you have the base folder path and are SURE it exists…

you can use a hidden features of the SSK files utilities module as follows to get the contents of that folder:

local folderPath = "/storage/emulated/0/Download/" folderPath = ssk.files.util.repairPath( folderPath ) if( ssk.files.util.exists( folderPath ) ) then -- see source linked above for this function: getFilesInFolder() local filesInFolder = ssk.files.util.getFilesInFolder( folderPath ) table.dump( filesInFolder, "Found these files in " .. tostring( "/storage/emulated/0/Download/" ) ) end 

getFilesInFolder() is defined about line 334 in RGFiles_util.lua

Having a list of the files in the folder you can now easily check to see if the file you want is in the folder list.  i.e. it exists or not.

This gives you an easy way to see all the files in the folder and to be sure your naming convention matches what you’re comparing to. 

i.e. Your way is a little blind and you may be missing a space, have a capitalization or lower-case letter in the wrong place, etc.  So it will be very hit and miss.  Using SSK you can easily see what is in the folder and dump it to the console to speed up your dev work.

Well, I understood the line of reasoning, and yes, I’m absolutely sure that this path will never change … I understand that I will have a table with all the files it contains in the folder … But, I do not understand how I will search this table if it contains or not the file whose name is designated by the variable “nome” … =\

nome is the path.  The last string after the / is the file name.
 
If I read your code correctly, this is the file name:

local filename = "OC VISUAL " .. os.date("%Y%m%d") .. " H" .. composer.getVariable("ResultadoHORA") .. " M" .. composer.getVariable("ResultadoMINUTO") .. ".CSV"

So checking if the table contains the name and therefore checking if the file exists is as easy as…

local exists = false for k,v in pairs( filesInFolder ) do exists = exists or (v == filename) end print( filename, " exists? ", exists )

If that is too confusing you can write this too:

for k,v in pairs( filesInFolder ) do if(v == filename) then exists = true; end end

Oh … Many thanks man! Really the first “for” was very confusing for me hahah, but I managed to understand perfectly in the second … Thanks!

  1. I’d start with this (from https://github.com/roaminggamer/SSK2/blob/master/ssk2/extensions/io.lua)::slight_smile:

    function io.exists( fileName, base ) local base = base or system.DocumentsDirectory if( base ) then fileName = system.pathForFile( fileName, base ) end if not fileName then return false end fileName = io.repairPath( fileName ) local attr = lfs.attributes( fileName ) return (attr and (attr.mode == “file” or attr.mode == “directory”) ) end

  2. It looks like you want to check for the existence of files OUTSIDE the app sand-box.  In that case you are on your own regarding assembling the path to that file.  You may want to check the marketplace to see if there is a plugin to help with that.

If you have the base folder path and are SURE it exists…

you can use a hidden features of the SSK files utilities module as follows to get the contents of that folder:

local folderPath = "/storage/emulated/0/Download/" folderPath = ssk.files.util.repairPath( folderPath ) if( ssk.files.util.exists( folderPath ) ) then -- see source linked above for this function: getFilesInFolder() local filesInFolder = ssk.files.util.getFilesInFolder( folderPath ) table.dump( filesInFolder, "Found these files in " .. tostring( "/storage/emulated/0/Download/" ) ) end 

getFilesInFolder() is defined about line 334 in RGFiles_util.lua

Having a list of the files in the folder you can now easily check to see if the file you want is in the folder list.  i.e. it exists or not.

This gives you an easy way to see all the files in the folder and to be sure your naming convention matches what you’re comparing to. 

i.e. Your way is a little blind and you may be missing a space, have a capitalization or lower-case letter in the wrong place, etc.  So it will be very hit and miss.  Using SSK you can easily see what is in the folder and dump it to the console to speed up your dev work.

Well, I understood the line of reasoning, and yes, I’m absolutely sure that this path will never change … I understand that I will have a table with all the files it contains in the folder … But, I do not understand how I will search this table if it contains or not the file whose name is designated by the variable “nome” … =\

nome is the path.  The last string after the / is the file name.
 
If I read your code correctly, this is the file name:

local filename = "OC VISUAL " .. os.date("%Y%m%d") .. " H" .. composer.getVariable("ResultadoHORA") .. " M" .. composer.getVariable("ResultadoMINUTO") .. ".CSV"

So checking if the table contains the name and therefore checking if the file exists is as easy as…

local exists = false for k,v in pairs( filesInFolder ) do exists = exists or (v == filename) end print( filename, " exists? ", exists )

If that is too confusing you can write this too:

for k,v in pairs( filesInFolder ) do if(v == filename) then exists = true; end end

Oh … Many thanks man! Really the first “for” was very confusing for me hahah, but I managed to understand perfectly in the second … Thanks!