36

Following my program shows contents of Documents directory and is displayed in a tableView . But the Documents directory contains some directories , some audio , some video , and some images etc .

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
fileType = @"" ;
NSString *subDirectoryPath = [docsDir stringByAppendingPathComponent:fileType];
NSLog(@"path : %@",subDirectoryPath);
files  = [[NSMutableArray alloc] initWithArray:[[NSFileManager defaultManager] contentsOfDirectoryAtPath:subDirectoryPath error:nil]];
NSLog(@"files ::::: %@ ",[files description]);

So , I want to check if file is directory then it directory image can be shown in the cell's imageView . Same for audio , video and image .

I searched in NSFileManager classReference , but dint get a solution .

How to do this ?

Bhavin
  • 26,755
  • 11
  • 52
  • 91
Subbu
  • 2,013
  • 4
  • 24
  • 39
  • See [my answer for Swift 4.2 and iOS 12](https://stackoverflow.com/a/54160565/1966109) that shows up to 4 different ways to check if a URL represents a file or a directory. – Imanou Petit Jan 12 '19 at 14:47

5 Answers5

56

Sample Code :

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *directory = [[NSFileManager defaultManager] directoryContentsAtPath: documentsDirectory];
BOOL isDirectory;
for (NSString *item in directory){
    BOOL fileExistsAtPath = [[NSFileManager defaultManager] fileExistsAtPath:item isDirectory:&isDirectory];
    if (fileExistsAtPath) {
       if (isDirectory)
       {
           //It's a Directory.
       }
    }
    if ([[item pathExtension] isEqualToString:@"png"]) {
       //This is Image File with .png Extension
    }
}

You can also use Uniform Type Identifiers as explained by Bavarious here.

Sample Code :

NSString *file = @"…"; // path to some file
CFStringRef fileExtension = (CFStringRef) [file pathExtension];
CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);

if (UTTypeConformsTo(fileUTI, kUTTypeImage)) NSLog(@"It's an image");
else if (UTTypeConformsTo(fileUTI, kUTTypeMovie)) NSLog(@"It's a movie");
else if (UTTypeConformsTo(fileUTI, kUTTypeText)) NSLog(@"It's text");

CFRelease(fileUTI);
Community
  • 1
  • 1
Bhavin
  • 26,755
  • 11
  • 52
  • 91
  • if there is no extension means that is folder – pratik bhiyani Jun 17 '13 at 11:07
  • what if audio file...vin...? because there is multiple extension audio file then what we can do..? – Nitin Gohel Jun 17 '13 at 11:22
  • @NitinGohel: Hey Bro, I think he has to compare all those Extensions, I guess !!! – Bhavin Jun 17 '13 at 11:24
  • yup may be but i just want to know can u plz explain for me it's for my understanding plz bro.. – Nitin Gohel Jun 17 '13 at 11:25
  • @NitinGohel: Check that link , I have added in my Answer. – Bhavin Jun 17 '13 at 11:30
  • @NitinGohel: Thx. Have you heard about it or used it ? – Bhavin Jun 17 '13 at 11:34
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31866/discussion-between-nitin-gohel-and-vin) – Nitin Gohel Jun 17 '13 at 11:35
  • That first piece of code is incorrect. The `if (fileExistsAtPath)` needs another test nested inside it: `if (isDirectory) { _here_ you know it's a folder }` – Basil Bourque Oct 17 '13 at 00:45
  • @BasilBourque: I just forgot that part at that time. Resolved now. Thanks for correcting me. – Bhavin Oct 17 '13 at 06:45
  • This doesn't work for me. "item" is just a string with the last path component. Instead use : `NSString * path = [documentsDirectory stringByAppendingPathComponent:item]; BOOL fileExistsAtPath = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory];` – olynoise Jul 15 '15 at 21:57
3

Look at the NSURL class. You can for example see if the url is a file url by saying isFileUrl.

Additionally you can get the last component of the url (usually the file name) by doing:

NSString *filename = [[url path] lastPathComponent];

These can help you do what you need, but more importantly look at the NSURL documentation.

Midhun MP
  • 90,682
  • 30
  • 147
  • 191
William Falcon
  • 9,426
  • 11
  • 59
  • 106
  • +1 for an string/path answer that doesn't rely on `fileExistsAtPath()` (which is blocked by the Sandbox). Answer would be improved by the expected value for `lastPathComponent`... does an empty string == a folder? – pkamb Jan 20 '17 at 21:54
3

Just in case of image file type

-(BOOL)isImageSource:(NSURL*)URL
{
    CGImageSourceRef source = CGImageSourceCreateWithURL((__bridge CFURLRef)URL, NULL);
    NSString *UTI = (__bridge NSString*)CGImageSourceGetType(source);
    CFRelease(source);

    CFArrayRef mySourceTypes = CGImageSourceCopyTypeIdentifiers();
    NSArray *array = (__bridge NSArray*)mySourceTypes;
    CFRelease(mySourceTypes);

    return [array containsObject:UTI];
}
pkamb
  • 26,648
  • 20
  • 124
  • 157
Hugo the Lee
  • 101
  • 1
  • 1
2

For any Swift lovers, coming to this question trying to check if a file is a directory here you go:

///
/// Check if NSURL is a directory
///
internal func fileIsDir(fileURL: NSURL) -> Bool {
    var isDir: ObjCBool = false;
    fileManager.fileExistsAtPath(fileURL.path!, isDirectory: &isDir)
    return Bool(isDir);
}

///
/// Check if a path is a directory
///
internal func fileIsDir(path: String) -> Bool {
    var isDir: ObjCBool = false;
    fileManager.fileExistsAtPath(path, isDirectory: &isDir)
    return Bool(isDir);
}

(Note that I tried doing return isDir as Bool but it didn't work. But the above Bool initiation seems to work.)

Evdzhan Mustafa
  • 3,126
  • 1
  • 20
  • 37
2

Updated Evdzhan's answer for Swift 4.0:

//
// Check if NSURL is a directory
//
func fileIsDir(fileURL: NSURL) -> Bool {
    var isDir: ObjCBool = false;
    FileManager.default.fileExists(atPath: fileURL.path!, isDirectory: &isDir)
    return isDir.boolValue
}

//
// Check if a path is a directory
//
func fileIsDir(path: String) -> Bool {
    var isDir: ObjCBool = false;
    FileManager.default.fileExists(atPath: path, isDirectory: &isDir)
    return isDir.boolValue
}
dougzilla
  • 116
  • 1
  • 4