Home > DeveloperSection > Forums > Checking NSString path is a Directory or File in iOS.
Glen Martin
Glen Martin

Total Post:27

Points:191
Posted on    January-04-2016 7:19 AM

 iPhone iPhone  Ios  Objective C 
Ratings:


 1 Reply(s)
 332  View(s)
Rate this:

In our iPhone application I have a nsstring which can be a path of any location or file.

Now, I want to check that string(path) is locating a file or folder/director.

Is this possible in Cocoa to check the string path for file or directory, then please provide me the code or tell me the method.
Thank you.



Tarun Kumar

Total Post:210

Points:1614
Supported
Posted on    January-04-2016 10:30 PM

For checking existence of file or directory you can use NSFileManager:
[[NSFileManager defaultManager]fileExistsAtPath:path_name isDirectory:&directoryFlag];
Here we are providing complete example:

NSString *dirPath = @"/Album/";
BOOL check;
NSDirectoryEnumerator *enum = [[NSFileManager defaultManager] enumeratorAtPath:dirPath];
while((dirPath = [enum nextObject])){
   [[NSFileManager defaultManager] fileExistsAtPath:dirPath isDirectory:&check];
   if(check){
      NSLog(@"Directory Path: %@", dirPath);
   }
}

Modified On Jan-04-2016 10:31:17 PM

Don't want to miss updates? Please click the below button!

Follow MindStick