Counting number of files in Directory with reference counting
Asked Answered
C

5

9

I have a folder of images that were imported with Create Folder Reference method, since I want to call images with URLForResoure for transfer to Watch. Before I transfer Images I would like to count them in folder. I was able to get folderURL path with this code:

let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]

    let folderURL = documentsURL.URLByAppendingPathComponent("Folder/SubFolder", isDirectory: true)

but can not get access to files inside this folder.

I want to access files and count them either with same prefix or inside subfolder.

Please help.

Clawson answered 6/10, 2015 at 21:43 Comment(1)
Files bundled with your app are not in the Documents folder.Maniple
C
-3

I solved my problem using this line of code:

let URL = NSBundle.mainBundle().URLsForResourcesWithExtension("jpg", subdirectory: "Folder/SubFolder")
let count = (excerciseImagesForAnimationURL?.count)!
Clawson answered 8/10, 2015 at 20:35 Comment(0)
C
16

Swift3

let fileManager = FileManager.default
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let dirContents = try? fileManager.contentsOfDirectory(atPath: documentsPath)
let count = dirContents?.count
if count == 0{
   disableTabBarHistory()// or whatever...
}
Courtesan answered 10/1, 2017 at 15:43 Comment(0)
T
3

here's a count of directory contents:

   let fileManager = NSFileManager.defaultManager()
   let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
   var dirContents = try? fileManager.contentsOfDirectoryAtPath(documentsPath)
   let count = dirContents?.count
Treadle answered 7/10, 2015 at 2:10 Comment(1)
Since I imported files with create folder references and I need to call them using URL addresses because I need it for Watch transfer, I can not call Path functions on it.Clawson
S
1

Swift 5.5:

let fileManager = FileManager.default
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let content = try? fileManager.contentsOfDirectory(atPath: path)
let count = content?.count
Stroh answered 3/8, 2022 at 7:44 Comment(0)
T
0

it's easy my friend

 let fileManager =            NSFileManager.defaultManager()
 var error : NSError?
 if let files =       fileManager.contentsOfDirectoryAtPath(folde  rURL, error: &error) {
    let count = files.count
   // ...
   } else {
    println("Could not get contents of     directory: \(error?.localizedDescription)")
    }
Tredecillion answered 6/10, 2015 at 22:48 Comment(1)
I tried that but get an error saying "Cannot convert value of type 'NSURL' to expected argument of type 'String.Clawson
C
-3

I solved my problem using this line of code:

let URL = NSBundle.mainBundle().URLsForResourcesWithExtension("jpg", subdirectory: "Folder/SubFolder")
let count = (excerciseImagesForAnimationURL?.count)!
Clawson answered 8/10, 2015 at 20:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.