Swift - How to Get the File Path Inside a Folder

Swift - How do I get the file path inside a folder

First make sure when you drag your folder audioFiles to your project to check copy items if needed and select create folder references. Make sure it shows a blue folder if your project.

Sample Image

Sample Image

Also NSBundle method pathForResource has an initialiser that you can specify in which directory your files are located:

let audioFileName = "audioName" 

if let audioFilePath = Bundle.main.path(forResource: audioFileName, ofType: "mp3", inDirectory: "audioFiles") {

If you would like to get that file URL you can use NSBundle method URLForResource(withExtension:, subdirectory:)

if let audioFileURL = Bundle.main.url(forResource: audioFileName, withExtension: "mp3", subdirectory: "audioFiles") {

Swift 3 : How to get path of file saved in Documents folder

First, separate name and extension:

Bundle.main.path(forResource: "Owl", ofType: "jpg")

Second, separate (mentally) your bundle and the Documents folder. They are two completely different things. If this file is the Documents folder, it absolutely is not in your main bundle! You probably want something like this:

let fm = FileManager.default
let docsurl = try! fm.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let myurl = docsurl.appendingPathComponent("Owl.jpg")

Third, if Owl is an image asset in the asset catalog, then say

let im = UIImage(named:"Owl") // or whatever its name is

Get the name of file(s) within last directory and the full directory path using Swift

As element is an URL, if you're interested in the full path name rather than the last component, just go for:

    var nextObject = element.absoluteURL  // instead of .lastPathComponent

or just

    var nextObject = element.path  // or even relativePath 

how to get all file full paths with swift in a folder

Suppose fm is our file manager and url is our directory URL.

let dir = fm.enumerator(at:url, includingPropertiesForKeys: nil)! 
for case let f as URL in dir {
print(f) // or its absolute path or whatever

Here's a test:

let fm = FileManager.default
let docs = fm.urls(for: .documentDirectory, in: .userDomainMask)[0]
let f1 = docs.appendingPathComponent("f1.txt")
try? "howdy".write(to: f1, atomically: true, encoding: .utf8)
let subdir = docs.appendingPathComponent("subdir")
try? fm.createDirectory(at: subdir, withIntermediateDirectories: true, attributes: nil)
let f2 = subdir.appendingPathComponent("f2.txt")
try? "hello".write(to: f2, atomically: true, encoding: .utf8)

let dir = fm.enumerator(at:docs, includingPropertiesForKeys: nil)!
for case let f as URL in dir where f.pathExtension == "txt" {
print(f) // or its absolute path or whatever



I believe that matches your requirements.

Get path of file from a SubGroup in project with Swift 5

The group in which you keep a resource in Xcode is not necessarily related to its final location in the bundle. Your file is probably still in the top level Resources directory.

If you want to be completely sure where the build puts your file, look for the build log in the Report Navigator, locate the "Copy " line and expand it using the button at the right hand edge. That will tell you exactly where it is.

A copy resource line from the build log

In the above, I have an Excel spreadsheet in the group testData. You can see that the build has put it in the top level Resources directory.

Getting list of files in documents folder

This solution works with Swift 4 (Xcode 9.2) and also with Swift 5 (Xcode 10.2.1+):

let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
do {
let fileURLs = try fileManager.contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil)
// process files
} catch {
print("Error while enumerating files \(documentsURL.path): \(error.localizedDescription)")

Here's a reusable FileManager extension that also lets you skip or include hidden files in the results:

import Foundation

extension FileManager {
func urls(for directory: FileManager.SearchPathDirectory, skipsHiddenFiles: Bool = true ) -> [URL]? {
let documentsURL = urls(for: directory, in: .userDomainMask)[0]
let fileURLs = try? contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil, options: skipsHiddenFiles ? .skipsHiddenFiles : [] )
return fileURLs

// Usage
print(FileManager.default.urls(for: .documentDirectory) ?? "none")

Accessing inner folders in Assets folder

This crash because you try force unwrap optional value.

UIImage(named: "logos/ca/payments/x.png") is optional value. Incase image doesn't exist in your project, this will be nil.

And we don't require put path when get image, just only image name and extension file like UIImage(named: "x.png")

Also cell.paymentImageView.image can accept nil image. So this line just only cell.paymentImageView.image = UIImage(named: "x.png") will be okey.

Hope this help!

Related Topics

Leave a reply