我正在使用此代码从文档目录中获取PDF文件列表。 现在,我想在文档目录中找到名为“MyFiles”的文件夹中的PDF文件列表,如果该文件夹存在的话。
我该怎么做?
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
}
}
提前致谢..
答案 0 :(得分:4)
我认为这可以通过实现实现SequenceType协议的NSFileManager的扩展来实现。但您可以轻松地将代码转换为使用while循环:
// 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)
}
答案 1 :(得分:1)
尝试以下代码:
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
}
}
答案 2 :(得分:1)
这是工作代码..
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
}
}
答案 3 :(得分:0)
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)")
}
答案 4 :(得分:0)
对于 Swift 4.2-5.0 ,使用 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
}}