Re: Insuring a path exists
Re: Insuring a path exists
- Subject: Re: Insuring a path exists
- From: Ondra Cada <email@hidden>
- Date: Wed, 9 Jun 2004 13:39:18 +0200
Mike,
On 9.6.2004, at 4:54, Mike O'Connor wrote:
>
NSString* thePath=@"/Library/Application
>
Support/MyApp/MySubfolder/file.typ"
>
>
I want to insure that folders MyApp and MySubfolder exist, so that
>
when I do a writeToFile: using that path, it will succeed.
>
>
Is there a call I can make to insure the path exists, creating the
>
folders if needed, or do I have to write some code going through the
>
components of the path and doing it myself? TIA!
Alas it is not possible to do this right by just one simple call, since
you must also set up properly the access rights and ownership of the
individual components.
As for creating more subfolders *with default ownership/access rights*
at once, for some reason it is not supported directly by Cocoa, but it
is pretty simple to add a category -- feel free to use mine, if you
want to:
@implementation NSFileManager (OCSAttributedStringHTMLExtension)
static void oops(NSString *filename) {
// logs the error, raises an exception: customize for your needs
}
-(NSString*)ocsCreateDirectoryNamed:(NSString*)foldername
basePath:(NSString*)base {
if (foldername) base=[base
stringByAppendingPathComponent:foldername];
BOOL dir;
if ([self fileExistsAtPath:base isDirectory:&dir])
if (dir) return base; // already exists
else oops(base); // plain file, not a folder
// does not exist: first ensure the parent exists
[self ocsCreateDirectoryNamed:nil basePath:[base
stringByDeletingLastPathComponent]];
if (![self createDirectoryAtPath:base attributes:nil]) oops(base);
return base;
}
@end
---
Ondra Hada
OCSoftware: email@hidden
http://www.ocs.cz
private email@hidden
http://www.ocs.cz/oc
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.