• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Distinguish between Directory and File
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Distinguish between Directory and File


  • Subject: Re: Distinguish between Directory and File
  • From: "Clint Shryock" <email@hidden>
  • Date: Thu, 4 Oct 2007 06:57:51 -0500

in Advanced Mac OS X Programming <
http://www.bignerdranch.com/products/core.shtml >
there is a quick file browser example.  the author goes through a directory
by getting an array of strings identifying the directories and files with
NSFilemanager directoryContentsAtPath:

NSArray *filenames = [[NSFileManager defaultManager]
directoryContentsAtPath:path]

then:
    int max, k;
    max = [filenames count];
    for (k=0; k < max; k++) {
        //    implementation stuff here...
    }

in the loop you could use NSFilemanager's fileAttributesAtPath:traverseLink:
like Ben suggested.  that method returns a dictionary of values which you
can check if fileType is equal to
<http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSFileTypeDirectory>
NSFileTypeDirectory

+Clint

On 10/3/07, Dani <email@hidden> wrote:
>
> isDirectory is always set to YES even if it's a file and not a
> directory. I don't know why. Here's the code:
>
> - (NSArray *)listFilesInFolder:(NSString *)apath
> {
>         NSArray *filesList;
>         NSMutableArray *onlyFiles;
>         NSString *path;
>         BOOL isDir;
>         int i;
>
>         NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager]
> enumeratorAtPath:apath];
>         filesList = [direnum allObjects];
>
>         [self llistarArray:filesList];
>
>         for (i=0;i<[filesList count];i++)
>         {
>                 path = [filesList objectAtIndex:i];
>                 [[NSFileManager defaultManager] fileExistsAtPath:path
> isDirectory:&isDir];
>
>                 if (isDir==false)
>                 {
>                         [onlyFiles addObject:[filesList objectAtIndex:i]];
>                 }
>         }
>
>         return filesList;
> }
>
>
> In theory in the onlyFiles NSMutableArray should be only files.
>
>
> El 03/10/2007, a las 22:47, Ben Stiglitz escribió:
>
> >> Hi! I've been trying to write some code to distinguish between a
> >> directory and a file. At first I tried with
> >> fileExistsAtPath:isDirectory method thinking that looking at the
> >> value of isDirectory will told me what I need. I was wrong.
> >
> > What part of the byref returned value was wrong?
> >
> > -[NSFileManager fileAttributesAtPath:traverseLink:] is another way
> > to get the information you're looking for; check out the docs for
> > the NSFileType key.
> >
> > -Ben
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Distinguish between Directory and File
      • From: Dani <email@hidden>
References: 
 >Distinguish between Directory and File (From: Dani <email@hidden>)
 >Re: Distinguish between Directory and File (From: Ben Stiglitz <email@hidden>)
 >Re: Distinguish between Directory and File (From: Dani <email@hidden>)

  • Prev by Date: Re: NSObjectInaccessibleException -- CoreData could not fulltill a fault
  • Next by Date: Binding (KVC/KVO) for C struct datatypes
  • Previous by thread: Re: Distinguish between Directory and File
  • Next by thread: Re: Distinguish between Directory and File
  • Index(es):
    • Date
    • Thread