Distinguish between Directory and File

  • 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.
    Then after some reading about UNIX I tried to do it using Shell
    Scripting but with no success.
    So, what I want to know if there's any way to do that and if not, how
    can I get the file permissions with objective-c, so then I could
    distinguish between file and directories thanks to the first
    character in the permissions string: "d"rwxrwxrwx

    Thanks
  • > 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
  • 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
  • On Oct 3, 2007, at 4:00 PM, Dani 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.
    >

    The problem is that enumeratorAtPath returns an enumerator that is a
    "rooted" in the given path.  Asking for all objects (which, if you
    have an enumerator already and are then going to iterate over it is
    wasteful - you should just use the enumerator iteration methods btw)
    will return relative paths.  Asking "existsAtPath" is actually going
    to return "NO" since that expects full paths - and as a result, isDir
    is never changed from the garbage value it had to start with (which
    happened to be 0).

    So instead, something like this:

    NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager]
    enumeratorAtPath:apath];
    NSString *subpath;
    while ((subpath = [direnum nextObject]) != nil) {
    NSString *fullPath = [apath stringByAppendingPathComponent: subpath];
    if ([[NSFileManager defaultManager] fileExistsAtPath: fullPath
    isDirectory: &isDir] == YES && isDir == YES) {
      [onlyFiles addObject: fullPath]; // or subpath if you only want
    relative sub-paths
    }
    }

    Glenn Andreas                      <gandreas...>
      <http://www.gandreas.com/> wicked fun!
    quadrium2 | build, mutate, evolve, animate  | images, textures,
    fractals, art
  • You'll also want to actually allocate that onlyFiles array...

    - d

    On Oct 3, 2007, at 5:07 PM, glenn andreas wrote:

    >
    > On Oct 3, 2007, at 4:00 PM, Dani 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.
    >>
    >
    >
    > The problem is that enumeratorAtPath returns an enumerator that is
    > a "rooted" in the given path.  Asking for all objects (which, if
    > you have an enumerator already and are then going to iterate over
    > it is wasteful - you should just use the enumerator iteration
    > methods btw) will return relative paths.  Asking "existsAtPath" is
    > actually going to return "NO" since that expects full paths - and
    > as a result, isDir is never changed from the garbage value it had
    > to start with (which happened to be 0).
    >
    > So instead, something like this:
    >
    > NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager]
    > enumeratorAtPath:apath];
    > NSString *subpath;
    > while ((subpath = [direnum nextObject]) != nil) {
    > NSString *fullPath = [apath stringByAppendingPathComponent: subpath];
    > if ([[NSFileManager defaultManager] fileExistsAtPath: fullPath
    > isDirectory: &isDir] == YES && isDir == YES) {
    > [onlyFiles addObject: fullPath]; // or subpath if you only want
    > relative sub-paths
    > }
    > }
    >
    >
    >
    >
    > Glenn Andreas                      <gandreas...>
    > <http://www.gandreas.com/> wicked fun!
    > quadrium2 | build, mutate, evolve, animate  | images, textures,
    > fractals, art
  • Dani wrote:

    > 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.

    In what way? That routine has never failed me. The only point of
    confusion I can imagine is packages that _are_ directories but
    _look_like_ files. For that, you can do this:

      Boolean theDirectoryFlag = false;
      Boolean thePackageFlag = false;
      LSItemInfoRecord theItemInfo = {};
      if(LSCopyItemInfoForURL(theFileURL, kLSRequestBasicFlagsOnly,
    &theItemInfo) == noErr)
      {
        theDirectoryFlag = (theItemInfo.flags & kLSItemInfoIsContainer) !
    = 0;
        thePackageFlag = (theItemInfo.flags & kLSItemInfoIsPackage) != 0;
      }

    Or if you want to stick with Cocoa (that being what this list is
    about and all...):

    NSWorkspace>>isFilePackageAtPath:
  • 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/NSFileT
    ypeDirectory
    >
    NSFileTypeDirectory

    +Clint

    On 10/3/07, Dani <daniel.amselem...> 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

    >
  • Thanks to everybody. The problem was that I was calling the
    fileExistsAtPath with an incomplete path.

    El 04/10/2007, a las 13:57, Clint Shryock escribió:

    > 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 NSFileTypeDirectory
    >
    > +Clint
    >
    > On 10/3/07, Dani <daniel.amselem...> 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

    >
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today