REFERENCE
http://www.techotopia.com/index.php/Working_with_Directories_on_iPhone_OS
Creating an NSFileManager Instance Object
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
Identifying the Current Working Directory :
As previously mentioned, when an application first loads its current working directory is the application’s root directory, represented by a / character. The current working directory may be identified at any time through a call to the currentDirectoryPath method of the file manager object. For example, the following code fragment identifies the current working directory:
NSFileManager *filemgr; NSString *currentPath;
filemgr =[NSFileManager defaultManager];
currentPath = [filemgr currentDirectoryPath];
[filemgr release];
Identifying the Documents Directory
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
/Users//Library/Application Support/iPhone Simulator/User/Application//Documents
Identifying the Temporary Directory:
In addition to the Documents directory, iPhone applications are also provided with a tmpNSTemporaryDirectory C function as follows: directory for the storage of temporary files. The path to the current application’s temporary directory may be ascertained with a call to the
NSString *tmpDir = NSTemporaryDirectory();
Changing Directory:
Having identified the path to the application’s document or temporary directory the chances are good that you will need to make that directory the current working directory. The current working directory of a running iPhone application can be changed with a call to the changeCurrentDirectoryPath method of an NSFileManager class instance. The destination directory path is passed as an argument to the instance method in the form of an NSString object. Note that this method returns a boolean YES or NO result to indicate if the requested directory change was successful or not. A failure result typically indicates that the specified directory does not exist, or that the application lacks the appropriate access permissions:
NSFileManager *filemgr;
NSArray *dirPaths;
NSString *docsDir;
filemgr =[NSFileManager defaultManager];
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
if ([filemgr changeCurrentDirectoryPath: docsDir] == NO)
{
// Directory does not exist – take appropriate action
}
Creating a New Directory
A new directory is created using the createDirectoryAtPath instance method of the NSFileManager class, once again passing through the pathname of the new directory as an argument and returning a boolean success or failure result. This method also takes an additional argument in the form a set of attributes for the new directory. Specifying nil will use the default attributes.
The following code fragment identifies the Documents directory and creates a new sub-directory named data in that directory:
NSFileManager *filemgr;
NSArray *dirPaths;
NSString *docsDir;
NSString *newDir;
filemgr =[NSFileManager defaultManager];
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
newDir = [docsDir stringByAppendingPathComponent:@"data"];
if ([filemgr createDirectoryAtPath:newDir attributes:nil] == NO)
{ // Failed to create directory }
[filemgr release];
Deleting a Directory
An existing directory may be removed from the file system using the removeItemAtPath method, passing though the path of the directory to be removed as an argument. For example, to remove the data directory created in the preceding example we might write the following code:
NSFileManager *filemgr;
NSArray *dirPaths;
NSString *docsDir; NSString *newDir;
filemgr =[NSFileManager defaultManager];
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
newDir = [docsDir stringByAppendingPathComponent:@"data"];
if ([filemgr removeItemAtPath: newDir error: nil] == NO)
{
// Directory removal failed.
}
[filemgr release];
Listing the Contents of a Directory
A listing of the files contained within a specified directory can be obtained using the directoryContentsAtpath method. This method takes the directory pathname as an argument and returns an NSArray object containing the names of the files and sub-directories in that directory. The following example obtains a listing of the contents of the root directory (/) and displays each item in the Console window application (located in the /Applications/Utilities folder of your Mac OS X system). Note that since there is no Console for the iPhone device, the output will only be visible when the application is running in the simulator environment:
NSFileManager *filemgr;
NSArray *filelist;
int count;
int i;
filemgr =[NSFileManager defaultManager];
filelist = [filemgr directoryContentsAtPath:@"/"];
count = [filelist count];
for (i = 0; i < count; i++)
NSLog(@"%@", [filelist objectAtIndex: i]);
[filemgr release];
Getting the Attributes of a File or Directory
The attributes of a file or directory can be obtained using the attributesOfItemAtPath method. This takes as arguments the path of the directory and an optional NSError object into which information about any errors will be placed (may be specified as NULL if this information is not required). The results are returned in the form of an NSDictionary dictionary object. The keys for this dictionary are as follows:
NSFileType
NSFileTypeDirectory
NSFileTypeRegular
NSFileTypeSymbolicLink
NSFileTypeSocket
NSFileTypeCharacterSpecial
NSFileTypeBlockSpecial
NSFileTypeUnknown
NSFileSize
NSFileModificationDate
NSFileReferenceCount
NSFileDeviceIdentifier
NSFileOwnerAccountName
NSFileGroupOwnerAccountName
NSFilePosixPermissions
NSFileSystemNumber
NSFileSystemFileNumber
NSFileExtensionHidden
NSFileHFSCreatorCode
NSFileHFSTypeCode
NSFileImmutable
NSFileAppendOnly
NSFileCreationDate
NSFileOwnerAccountID
NSFileGroupOwnerAccountID
For example, we can extract the file type and POSIX permissions for the /Applications directory using the following code excerpt:
NSFileManager *filemgr;
NSDictionary *attribs;
filemgr = [NSFileManager defaultManager];
attribs = [filemgr attributesOfItemAtPath: @"/Applications" error: NULL];
NSLog (@"File type %@", [attribs objectForKey: NSFileType]);
NSLog (@"POSIX Permissions %@", [attribs objectForKey: NSFilePosixPermissions]);
[filemgr release];
hi, i tried to get the working directory in the simulator by:
ReplyDelete[[NSBundle mainBundle] bundlePath];
and also by:
[[NSFileManager defaultManager] currentDirectoryPath];
and i got in both cases : Invalid Summary
does those functions should work also in the simulator or there is a different function for this?
Thanks,
Yoav