Retrieve Username

I am trying to detect the existence of file



function find_host_dbx(file_name)
  local host_dbx_found=io.open(file_name, "r") 
  if host_dbx_found==nil then
     host_dbx_found=file_name .. " ... Error - File Not Found"
   else
     host_dbx_found=file_name .. " ... File Found"
 end
  return host_dbx_found
end
 print(find_host_dbx("C:\\Users\\%username%\\AppData\\Local\\Dbox\\host.dbx"))

it seem I can’t do it like that.

can someone help me please

when I replace %username% by the computer user’s name it works

The environment variable substitution isn’t built into the file library. You should be able to use os.getenv() to search for the value, say by adapting the manual’s example here (from "%$(%w+)" to something like %%(%w+)%%", probably).

I think it should be something like this

local filePath = system.pathForFile( "your_file_here", system.DocumentsDirectory ) -- it might be in another location than system.DocumentsDirectory
local file = io.open( filePath, "r" )
if file then
  --file found
else
--file not found
end
io.close( file )

this link might help a bit

And if your files are located outside the reach of Solar2D code capabilities then you should use a plugin for that

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.