• 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: Automatic file numbering ideas...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Automatic file numbering ideas...


  • Subject: Re: Automatic file numbering ideas...
  • From: Nathan Kinsinger <email@hidden>
  • Date: Sun, 23 Nov 2008 15:02:41 -0700

On Nov 23, 2008, at 1:42 PM, Adam R. Maxwell wrote:
On Nov 23, 2008, at 11:56 AM, Kevin Gessner wrote:

Rather than hitting the file system every time you want to check file existence, you could cache it from -[NSFileManager directoryContentsAtPath:]. You could run in to a race condition (if another process creates a file between when you check the list and when you write your file), but that's true of your iterative solution as well.

This is a good idea, but you do need to be careful. For instance, the following program reports this:


froude:tmp amaxwell$ cc -o nametest -framework Foundation nametest.m
froude:tmp amaxwell$ ./nametest
2008-11-23 12:32:40.408 nametest[2186:10b] created Test File
2008-11-23 12:32:40.411 nametest[2186:10b] contentsOfDirectoryAtPath contains Test File
2008-11-23 12:32:40.412 nametest[2186:10b] fileExistsAtPath: says that Test File exists
2008-11-23 12:32:40.412 nametest[2186:10b] created Test file
2008-11-23 12:32:40.413 nametest[2186:10b] contentsOfDirectoryAtPath does not contain Test file
2008-11-23 12:32:40.413 nametest[2186:10b] fileExistsAtPath: says that Test file exists
froude:tmp amaxwell$ ll Test\ File
-rw-r--r-- 1 amaxwell wheel 0 Nov 23 12:32 Test File


"Test File" exists, but checking contentsOfDirectoryAtPath: and fileExistsAtPath: for the existence of "Test file" gives contradictory results.

In some respects, it would be nice if the array returned by NSFileManager used case-insensitive equality callbacks when called on a case-insensitive filesystem. Cocoa has a number of weird edge cases like this (e.g. instances of NSURL for files may not compare equal for the same file: URL, depending on how they were created).

--
Adam


#import <Foundation/Foundation.h>

int main (int argc, char const *argv[])
{
   NSAutoreleasePool *pool = [NSAutoreleasePool new];

   NSFileManager *fm = [NSFileManager defaultManager];

NSString *fname = @"Test File";
if ([fm createFileAtPath:fname contents:[NSData data] attributes:nil])
NSLog(@"created %@", fname);


NSArray *content = [fm contentsOfDirectoryAtPath:[fm currentDirectoryPath] error:NULL];
if ([content containsObject:fname])
NSLog(@"contentsOfDirectoryAtPath contains %@", fname);
else
NSLog(@"contentsOfDirectoryAtPath does not contain %@", fname);


   if ([fm fileExistsAtPath:fname])
       NSLog(@"fileExistsAtPath: says that %@ exists", fname);
   else
       NSLog(@"fileExistsAtPath: says that %@ does not exist", fname);

fname = @"Test file";
if ([fm createFileAtPath:fname contents:[NSData data] attributes:nil])
NSLog(@"created %@", fname);




content = [fm contentsOfDirectoryAtPath:[fm currentDirectoryPath] error:NULL];


   if ([content containsObject:fname])
       NSLog(@"contentsOfDirectoryAtPath contains %@", fname);
   else
       NSLog(@"contentsOfDirectoryAtPath does not contain %@", fname);

   if ([fm fileExistsAtPath:fname])
       NSLog(@"fileExistsAtPath: says that %@ exists", fname);
   else
       NSLog(@"fileExistsAtPath: says that %@ does not exist", fname);

   [pool release];
   return 0;
}


It didn't find the file because you forgot to update the content array after creating the new file.

--Nathan



_______________________________________________

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: Automatic file numbering ideas...
      • From: "Adam R. Maxwell" <email@hidden>
References: 
 >Automatic file numbering ideas... (From: Jean-Nicolas Jolivet <email@hidden>)
 >Re: Automatic file numbering ideas... (From: Jean-Nicolas Jolivet <email@hidden>)
 >Re: Automatic file numbering ideas... (From: Andrew Merenbach <email@hidden>)
 >Re: Automatic file numbering ideas... (From: Jean-Nicolas Jolivet <email@hidden>)
 >Re: Automatic file numbering ideas... (From: Kevin Gessner <email@hidden>)
 >Re: Automatic file numbering ideas... (From: "Adam R. Maxwell" <email@hidden>)

  • Prev by Date: NSXMLElement -stringValue and whitepsace
  • Next by Date: Re: Automatic file numbering ideas...
  • Previous by thread: Re: Automatic file numbering ideas...
  • Next by thread: Re: Automatic file numbering ideas...
  • Index(es):
    • Date
    • Thread