Reputation: 763
I am using this code to get list of PDF files from documents directory. Now I want to find list of PDF files from Folder named "MyFiles" in Document Directory if that folder exists.
How can I do this??
func listFilesFromDocumentsFolder() -> [String]
{
var theError = NSErrorPointer()
let dirs = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String]
if dirs != nil {
let dir = dirs![0]
let fileList = NSFileManager.defaultManager().contentsOfDirectoryAtPath(dir, error: theError) as! [String]
var count = fileList.count
for var i = 0; i < count; i++
{
var filePath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
filePath = filePath.stringByAppendingPathComponent(fileList[i])
let properties = [NSURLLocalizedNameKey, NSURLCreationDateKey, NSURLContentModificationDateKey, NSURLLocalizedTypeDescriptionKey]
var attr = NSFileManager.defaultManager().attributesOfItemAtPath(filePath, error: NSErrorPointer())
}
return fileList.filter{ $0.pathExtension == "pdf" }.map{ $0.lastPathComponent } as [String]
}else{
let fileList = [""]
return fileList
}
}
Thanks in advance..
Upvotes: 3
Views: 10292
Reputation: 855
For Swift 4.2 - 5.0 using Extension on FileManager :
extension FileManager {
func urls(for DocumentsDirectory : String) -> [URL]? {
let dirs = urls(for: .documentDirectory, in: .userDomainMask)
//this will give you the path to MyFiles
let MyFilesPath = dirs[0].appendingPathComponent("/" + DocumentsDirectory)
let fileList = try? contentsOfDirectory(at: MyFilesPath, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
return fileList
}}
Upvotes: 1
Reputation: 434
For Swift 3
let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let myFilesPath = documentDirectoryPath.appending("/Inbox")
let files = FileManager.default.enumerator(atPath: myFilesPath)
while let file = files?.nextObject() {
print("\(myFilesPath)/\(file)")
}
Upvotes: 0
Reputation: 763
This is the working code..
func listFilesFromDocumentsFolder() -> [String]
{
var theError = NSErrorPointer()
let dirs = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String]
if dirs != nil {
let dir = dirs![0]//this path upto document directory
//this will give you the path to MyFiles
let MyFilesPath = dir.stringByAppendingPathComponent("/BioData")
if !NSFileManager.defaultManager().fileExistsAtPath(MyFilesPath) {
NSFileManager.defaultManager().createDirectoryAtPath(MyFilesPath, withIntermediateDirectories: false, attributes: nil, error: theError)
} else {
println("not creted or exist")
}
let fileList = NSFileManager.defaultManager().contentsOfDirectoryAtPath(MyFilesPath, error: theError) as! [String]
var count = fileList.count
for var i = 0; i < count; i++
{
var filePath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
filePath = filePath.stringByAppendingPathComponent(fileList[i])
let properties = [NSURLLocalizedNameKey, NSURLCreationDateKey, NSURLContentModificationDateKey, NSURLLocalizedTypeDescriptionKey]
var attr = NSFileManager.defaultManager().attributesOfItemAtPath(filePath, error: NSErrorPointer())
}
println("fileList: \(fileList)")
return fileList.filter{ $0.pathExtension == "pdf" }.map{ $0.lastPathComponent } as [String]
}else{
let fileList = [""]
return fileList
}
}
Upvotes: 1
Reputation: 937
I think this may be possible by implementing an extension to NSFileManager that implements the SequenceType protocol. But you could easily convert your code to using a while loop:
// path to documents directory
let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
let myFilesPath = documentDirectoryPath.stringByAppendingPathComponent("/MyFiles")
let files = filemanager.enumeratorAtPath(myFilesPath)
while let file = files?.nextObject() {
println(file)
}
Upvotes: 6
Reputation: 5436
Try Below Code:
func listFilesFromDocumentsFolder() -> [String]
{
var theError = NSErrorPointer()
let dirs = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String]
if dirs != nil {
let dir = dirs![0]//this path upto document directory
//this will give you the path to MyFiles
let MyFilesPath = documentDirectory.stringByAppendingPathComponent("/MyFiles")
let fileList = NSFileManager.defaultManager().contentsOfDirectoryAtPath(MyFilesPath, error: theError) as! [String]
var count = fileList.count
for var i = 0; i < count; i++
{
var filePath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
filePath = filePath.stringByAppendingPathComponent(fileList[i])
let properties = [NSURLLocalizedNameKey, NSURLCreationDateKey, NSURLContentModificationDateKey, NSURLLocalizedTypeDescriptionKey]
var attr = NSFileManager.defaultManager().attributesOfItemAtPath(filePath, error: NSErrorPointer())
}
return fileList.filter{ $0.pathExtension == "pdf" }.map{ $0.lastPathComponent } as [String]
}else{
let fileList = [""]
return fileList
}
}
Upvotes: 1