Re: Problem with NSFileManager
Re: Problem with NSFileManager
- Subject: Re: Problem with NSFileManager
- From: Ken Thomases <email@hidden>
- Date: Wed, 2 Sep 2009 14:41:12 -0500
Moving this to Cocoa-Dev where it's appropriate...
On Sep 2, 2009, at 1:50 PM, K. Chen wrote:
I ran into a weird situation with NSFileMnager.
First my code want to see if a file already exists
if (![fileManager fileExistsAtPath:myConfigFile])
the result is NO, so my code goes into the if block
Then my code tries to copy the file over from the bundle; note the
the destination is under <app_home>/Documents.
if (![fileManager copyItemAtPath:configFilePath toPath:myConfigDir
error:&err])
Here I got the error message from [NSError localizedDescription]
saying: Operation could not be completed. File exists.
Well first FM told me the file doesn't exist and then when I tried
to copy, it said it already exists???
Well, you're checking myConfigFile but then copying to myConfigDir.
Based on the docs for the older -copyPath:toPath:handler: method, I
don't think -copyItemAtPath:toPath:error: implicitly creates a same-
named file in the destination directory. The destination is taken
literally. Since the directory does exist, that explains the error.
So, you need to copy to myConfigFile rather than myConfigDir.
You should also just abandon the check for the existence of
myConfigFile in advance. It doesn't help, since some other process
can create the file between when you check for it and when you try to
create it with the copy. Just attempt the copy and cope with a
failure due to a file existing at the destination.
Regards,
Ken
_______________________________________________
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