Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a folder exists in Cocoa & Objective-C?

How to check if a folder (directory) exists in Cocoa using Objective-C?

like image 950
lajos Avatar asked Sep 19 '08 03:09

lajos


2 Answers

Use NSFileManager's fileExistsAtPath:isDirectory: method. See Apple's docs here.

like image 64
Matt Dillard Avatar answered Sep 27 '22 23:09

Matt Dillard


Some good advice from Apple in the NSFileManager.h regarding checking the file system:

"It's far better to attempt an operation (like loading a file or creating a directory) and handle the error gracefully than it is to try to figure out ahead of time whether the operation will succeed. Attempting to predicate behavior based on the current state of the filesystem or a particular file on the filesystem is encouraging odd behavior in the face of filesystem race conditions."

like image 36
Ryan Bavetta Avatar answered Sep 27 '22 21:09

Ryan Bavetta