檔案,資料夾操作大全
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
//假設使用者文件下有如下檔案和資料夾[test1.txt,fold1/test2.txt]
let manager
= NSFileManager .defaultManager()
let urlForDocument
= manager. URLsForDirectory ( NSSearchPathDirectory . DocumentDirectory ,
inDomains: NSSearchPathDomainMask . UserDomainMask )
let url
= urlForDocument[0] as NSURL
//(1)對指定路徑執行淺搜尋,返回指定目錄路徑下的檔案、子目錄及符號連結的列表 let contentsOfPath
= try? manager.contentsOfDirectoryAtPath(url.path!)
//contentsOfPath:Optional([fold1,
test1.txt])
print ( "contentsOfPath:
\(contentsOfPath)" )
//(2)類似上面的,對指定路徑執行淺搜尋,返回指定目錄路徑下的檔案、子目錄及符號連結的列表
let contentsOfURL
= try? manager.contentsOfDirectoryAtURL(url, includingPropertiesForKeys: nil ,
options: NSDirectoryEnumerationOptions . SkipsHiddenFiles );
print ( "contentsOfURL:
\(contentsOfURL)" )
//(3)深度遍歷,會遞迴遍歷子資料夾(但不會遞迴符號連結)
let enumeratorAtPath
= manager.enumeratorAtPath(url.path!)
//enumeratorAtPath:Optional([fold1,
fold1/test2.txt, test1.txt])
print ( "enumeratorAtPath:
\(enumeratorAtPath?.allObjects)" )
//(4)類似上面的,深度遍歷,會遞迴遍歷子資料夾(但不會遞迴符號連結)
let enumeratorAtURL
= manager.enumeratorAtURL(url, includingPropertiesForKeys: nil
|