×
A new build of Corona SDK is now available to subscribers. Not a subscriber? Subscribe now.
CoronaSDK 2014.2277 | Released: 23 Apr 2014, 1:58am | What's New | Download Now

system.pathForFile()

Description:

Generates an absolute path using system-defined directories as the base (see System-defined directories below). A second, optional parameter baseDirectory specifies which base directory is used to construct the full path; its default value is system.ResourceDirectory.

If the base directory is system.ResourceDirectory and the generated path points to a non-existent file, "nil" is returned and a warning message is displayed in the Corona terminal.

Related Items:

system.ResourceDirectory -- returns constant to the App Bundle folder
system.DocumentsDirectory -- returns constant to /Documents folder
system.TemporaryDirectory -- returns constant to /tmp folder

Syntax:

1
system.pathForFile( filename [, baseDirectory] )

Example:

local path = system.pathForFile(  "data.txt", system.DocumentsDirectory )
local fhd = io.open( path )
 
-- Determine if file exists
if fhd then
   print( "File exists" )
   fhd.close()
else
    print( "File does not exist!" )
end

Parameters:

filename

File name string.

baseDirectory

Directory where the file is located. Assumes ResourceDirectory if this parameter is missing.

Returns:

String -- absolute path to file.

Remarks:

Note: On Android devices this API doesn't currently work on system.ResourceDirectory because there is no Resource folder. It's basically a zip file holding the resource files.

Supported on operating systems and platforms for build numbers shown:
  • Mac OS X:
    Corona SDK 1.0
  • Windows:
    Corona SDK 2.0
  • iOS:
    Corona SDK 1.0
  • Android:
    Corona SDK 2.0

Replies

ChrisSlee
User offline. Last seen 5 days 12 hours ago. Offline
Joined: 11 Feb 2011

Anyone know where this actually writes to on an android device? I've used astro to search for my files and can't find them.

paulallanharrington
User offline. Last seen 8 weeks 4 days ago. Offline
Joined: 14 Jan 2011

-The above example will produce a Runtime Error (see below):

Runtime error
/Users/paulh/Desktop/system.pathForFile/main.lua:7: bad argument #1 to 'close' (FILE* expected, got nil)
stack traceback:
[C]: ?
[C]: in function 'close'
/Users/paulh/Desktop/system.pathForFile/main.lua:7: in main chunk
Runtime error: /Users/paulh/Desktop/system.pathForFile/main.lua:7: bad argument #1 to 'close' (FILE* expected, got nil)
stack traceback:
[C]: ?
[C]: in function 'close'
/Users/paulh/Desktop/system.pathForFile/main.lua:7: in main chunk

-Solution

Replace fhd.close() with io.close( fhd ) (see code below)

1
2
3
4
5
6
7
8
9
10
11
local path = system.pathForFile(  "data.txt", system.DocumentsDirectory )
local fhd = io.open( path )
 
-- Determine if file exists
if fhd then
   print( "File exists" )
   --fhd.close() <- Produces a Runtime Error. Use io.close( fhd )
   io.close( fhd )
else
    print( "File does not exist!" )
end

Sincerely,

Paul Allan Harrington
http://www.pahgroup.com

paulallanharrington
User offline. Last seen 8 weeks 4 days ago. Offline
Joined: 14 Jan 2011

Graham Ranson over at Lime (http://justaddli.me/blog.php) has suggested that:

1
fhd.close( fhd )

should work as well and it does. Thanks Graham.

Best wishes,

Paul Allan Harrington
http://www.pahgroup.com

coderebelbase
User offline. Last seen 50 weeks 1 day ago. Offline
Joined: 1 Feb 2010

Shouldn't you check for path too? Because if I don't I get a Runtime error from the open function if the string given to system.pathForFile doesn't map to an actual file.


Runtime error
... bad argument #1 to 'open' (string expected, got nil)

1
2
3
4
5
6
7
8
9
10
11
12
local path = system.pathForFile( "data.txt", system.ResourceDirectory )
if path then -- file exists
        local file = io.open( path, "r" )
 
        if file then -- nil if no file found
                local contents = file:read( "*a" )
                print( "Contents of " .. path .. "\n" .. contents )
                io.close( file )
        end
else
        print( 'file does not exist' )
end

Tom
User offline. Last seen 14 hours 57 min ago. Offline
Staff
Joined: 13 Jul 2010

The file must exist when you specify the Resource Directory or a runtime warning will occur and path will be nil. Since this directory is read-only, your program should know what files are there.

You could check to make sure the value of path is not nil but that would mean you didn't include the file in your build.

Thanks,
Tom

J. A. Whye
User offline. Last seen 13 hours 36 min ago. Offline
Joined: 25 Sep 2010

While checking for nil might seem useless because "you should know whether the file is there" it comes in handy for me during development. I have a button routine that puts up a button and shows a depressed version when pressed...

...except when I'm developing and haven't done the depressed version yet, it will still work with just the one button...

...and even before that if there's NO button artwork yet it will display a text object that acts like a button.

Which means I can use the same button code from day 1 to the day I ship and never have to change anything except throw the button artwork in whenever I get it. :)

Just one example of how this can be used.

Jay

vitalyx
User offline. Last seen 44 weeks 2 days ago. Offline
Joined: 11 Apr 2011

Can we use custom base directory?

E.g. I tried this, but it didn't work:

1
2
3
4
local fileName = "file.xml"
local filePath = "/data/data/com.some.other.app/"
 
local path = system.pathForFile(fileName, filePath)

I was actually able to open file and write to it, all without errors. But no actual file was created in that directory.

EDIT: never mind, I check the path created by system.pathForFile and it was wrong: "/data/data/com.my.app/files/coronaResources/file.xml"
So I just called io.open(filePath + fileName, "w+"), but it gave an error. Probably, root permissions are needed.

Tom
User offline. Last seen 14 hours 57 min ago. Offline
Staff
Joined: 13 Jul 2010

Files are sandboxed and your are not allowed to access files like that. system.pathForFile is intended to be used with the Corona support file paths.

Tom
User offline. Last seen 14 hours 57 min ago. Offline
Staff
Joined: 13 Jul 2010

Please post your questions in the forums and not on the documentation pages. "All bug reports and support questions will be removed from this area."

Thanks.