How to get file name from file path in swift

How to get the file name from the specified line of the file path?

For example, if I have a file path string like

file:///Users/DeveloperTeam/Library/Developer/CoreSimulator/Devices/F33222DF-D8F0-448B-A127-C5B03C64D0DC/data/Containers/Data/Application/5D0A6264-6007-4E69-A63B-D77868EA1807/tmp/trim.D152E6EA-D19D-4E3F-8110-6EACB2833CE3.MOV 

and I would like to receive a return result as

 trim.D152E6EA-D19D-4E3F-8110-6EACB2833CE3.MOV 

Thank you for your help.

+74
ios iphone swift
Aug 03 '15 at 6:10
source share
8 answers

Goal c

 NSString* theFileName = [string lastPathComponent] 

Swift

 let theFileName = (string as NSString).lastPathComponent 
+129
Aug 03 '15 at 6:12
source share

Swift 2:

 var file_name = NSURL(fileURLWithPath: path_to_file).lastPathComponent! 
+24
Jan 27 '16 at 9:29
source share

SWIFT 3.x or SWIFT 4: The shortest and cleanest way to do this below. In this example, the url variable is a url type so we can have a human-readable String result of the full file name with an extension of type My file name.txt and not like My%20file%20name.txt

 // Result like: My file name.txt let fileName = url.lastPathComponent 
+24
Nov 06 '16 at
source share

If you want to get the current file name, for example, for logging purposes, I use this.

Swift 4

 URL(fileURLWithPath: #file).lastPathComponent 
+22
Jan 04 '18 at 6:14
source share

Try this

 let filename: String = "your file name" let pathExtention = filename.pathExtension let pathPrefix = filename.stringByDeletingPathExtension 

Updated:

 extension String { var fileURL: URL { return URL(fileURLWithPath: self) } var pathExtension: String { return fileURL.pathExtension } var lastPathComponent: String { return fileURL.lastPathComponent } } 

Hope it helps.

+8
Aug 03 '15 at 6:14
source share

Below code works for me in Swift 4.X

  let filename = (self.pdfURL as NSString).lastPathComponent // pdfURL is your file url let fileExtention = (filename as NSString).pathExtension // get your file extension let pathPrefix = (filename as NSString).deletingPathExtension // File name without extension self.lblFileName.text = pathPrefix // Print name on Label 

I hope you succeed, good coding :)

+6
Nov 16 '18 at 10:38
source share

Creates a unique file name form URL, including the two previous folders

 func createFileNameFromURL (colorUrl: URL) -> String { var arrayFolders = colorUrl.pathComponents // -3 because last element from url is "file name" and 2 previous are folders on server let indx = arrayFolders.count - 3 var fileName = "" switch indx{ case 0...: fileName = arrayFolders[indx] + arrayFolders[indx+1] + arrayFolders[indx+2] case -1: fileName = arrayFolders[indx+1] + arrayFolders[indx+2] case -2: fileName = arrayFolders[indx+2] default: break } return fileName 

}

+1
Apr 26 '18 at 7:01
source share

To get the file name without its extension from the URL in Swift> = 4.2:

 let urlWithoutFileExtension: URL = originalFileUrl.deletingPathExtension() let fileNameWithoutExtension: String = urlWithoutFileExtension.lastPathComponent 
0
Jun 09 '19 at 8:24
source share



All Articles