i want to know if there is a way to get the previous directory when working with NSFileManager...
This can be probably done by appending '..' to the current directory
([[NSFileManager defaultManager] currentDirectoryPath] ) , but it's not a good idea at all :(
Is there another effective way to do this ?
If what you're trying to do is just obtain the string representing the path of the parent directory then you could do this:
NSString* parentPath = [[[NSFileManager defaultManager] currentDirectoryPath] stringByDeletingLastPathComponent];
If you actually want to change to the parent directory, then just:
[[NSFileManager defaultManager] changeCurrentDirectoryPath:#".."];
Sure, just use the stringByDeletingLastPathComponent method in NSString. As per your question, you'd do something like:
NSFileManager* fileManager = [NSFileManager defaultManager];
NSString* currentDirectoryPath = [fileManager currentDirectoryPath];
NSString* previousDirectory = [currentDirectoryPath stringByDeletingLastPathComponent];
Related
I have one created one folder called "MYFOLDER" inside my folder in have one pdf file test.pdf.
I have one button named browse and the textfield. As soon as I click on the browse button I need to navigate through the folders and select the test.pdf file (Like GMAIL Attachment). After selecting particular file I need to set the file path in the text field.
How to do that?
Thanks in advance.
why not codes?
i appreciated NSFileManager.
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager directoryContentsAtPath:#"/MYFOLDER"];
NSArray *pdfList = [list filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"SELF ENDSWITH '.pdf'"]];
NSLog(#"%#", pdfList);
Use enumeratorAtPath:
Returns a directory enumerator object that can be used to perform a
deep enumeration of the directory at the specified path.
(NSDirectoryEnumerator *)enumeratorAtPath:(NSString *)path Parameters
path
The path of the directory to enumerate.
Return Value
An NSDirectoryEnumerator object that enumerates the contents of the
directory at path.
If path is a filename, the method returns an enumerator object that
enumerates no files—the first call to nextObject will return nil.
Maybe this will help you
[[NSBundle mainBundle] pathForResource:#"test" ofType:#"pdf";];
Then you need to list the files and folders in the folder, and keep on browsing.
NSError *error;
NSString *homeDirectory = NSHomeDirectory();
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:homeDirectory error:&error];
for(int i = 0; i < [dirContents count]; i++){
//Here you can browse dirContents
NSString *fileName = [dirContents objectAtIndex:i];
}
Let's say I have a file at the path /documents/recording.caf and I want to copy it into the folder /documents/folder. How would I do this? If I want to use the following code, it appears as though I have to include the file name and extension in the path, which I will not always know.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *newPath = [documentsDirectory stringByAppendingPathComponent:#"/temporary/recording.caf"];
if ([fileManager fileExistsAtPath:newPath] == NO) {
[fileManager copyItemAtPath:file toPath:newPath error:&error];
}
}
A tableview is updated with every file it finds in a directory and if the user taps a cell, I want to copy this file somewhere else.
Since the appended path component is also a string, you could use another create another string with the file name that you do not know
NSString *anotherString=[NSString stringWithFormat:#"/temporary/%#",<yourfilenameAsString>];
and just append that to newPath.
I would also suggest that you should not be constructing paths like #"/temporary/filename.extension". But rather, you construct it as using the path construction methods of NSString like
- (NSString *)stringByAppendingPathComponent:(NSString *)aString
- (NSString *)stringByAppendingPathExtension:(NSString *)ext
Creating a custom directory which has all the images. Designing it custom because, it will help me to get the images as an when I need at various places in the configuration.
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
[filemgr createDirectoryAtPath: #"/Users/home/lifemoveson/test" withIntermediateDirectories:YES attributes: nil error:NULL];
I have placed images under test folder and are of .png types.
Is there a way to retrieve the images like below.
/** UPDATED again **/
Currently this folder is under Application_Home/Resources/ImageTiles/ as per Photoscroller example.
How can we change it to /Users/home/lifemoveson/test/ImageTiles/ folder ?
- (UIImage *)tileForScale:(CGFloat)scale row:(int)row col:(int)col
{
// we use "imageWithContentsOfFile:" instead of "imageNamed:" here because we don't want UIImage to cache our tiles
NSString *tileName = [NSString stringWithFormat:#"%#_%d_%d_%d", imageName, (int)(scale * 1000), col, row];
// Currently this folder is under <Application_Home>/Resources/ImageTiles/ as per Photoscroller example.
// How can we change it to /Users/home/lifemoveson/test/ImageTiles/ folder ?
NSString *path = [[NSBundle mainBundle] pathForResource:tileName ofType:#"png"];
UIImage *image = [UIImage imageWithContentsOfFile:path];
return image;
}
Applications running on iOS are sandboxed; you can't simply create directories wherever you please. Your createDirectoryAtPath: call will fail. You should use one of the directories set aside for your application instead.
Once you obtain the path for one of those directories, getting the path for files within them is simply a case of using NSString's stringByAppendingPathComponent: method.
Makin a call to the functions such as
NSString* newDirPath = [self createDirectoryWithName:#"Test"];
if (newDirPath) {
[self saveFile:#"MasterDB.sqlite" atPath:newDirPath];
}
which are implemented as follows
-(NSString*)createDirectoryWithName:(NSString*)dirName{
NSArray* directoryArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask , YES);
NSString* directoryPath = [directoryArray objectAtIndex:0];
NSString* newPath = [directoryPath stringByAppendingString:[NSString stringWithFormat:#"/%#",dirName]];
NSFileManager *filemamager = [NSFileManager defaultManager];
BOOL flag = [filemamager createDirectoryAtPath:newPath withIntermediateDirectories:YES attributes: nil error:NULL];
return flag == YES ?newPath: nil;
}
-(BOOL)saveFile:(NSString*)fileName atPath:(NSString*)path{
BOOL success;
// Create a FileManager object, we will use this to check the status
// of the File and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the File has already been created in the users filesystem
NSString *filePath = [path stringByAppendingPathComponent:fileName];
success = [fileManager fileExistsAtPath:filePath];
// If the File already exists then return without doing anything
if(success) return YES;
// If not then proceed to copy the File from the application to the users filesystem
// Get the path to the database in the application package
NSString *pathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
// Copy the database from the package to the users filesystem
NSError *error = nil;
BOOL flag = [fileManager copyItemAtPath:pathFromApp toPath:filePath error:&error];
return flag;
}
might help you solve your problem. Creating a directory is achieved using first function and the second will let you save files from your application bundle to any of the previously created directory. I hope you can modify it save files located at places other than Application bundle to suit to your need.
I have a zip file that I unzip it in a directory 'extract' under the Documents directory (the dir and its content is properly created). Then I would to do some stuff on each item in the new dir, dipendently if it's a file or a dir. I use the contentsOfDirectoryAtPath method of the NSFileManager.
The problem is that the array extractedItems returns always null and I don't know what I'm wronging.
NSString *dirnameForUnzippedData = [[NSString alloc] initWithString:#"extract"];
NSString *dirpath = [documentsDirectory stringByAppendingPathComponent:dirnameForUnzippedData];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *extractedItems = [fileManager contentsOfDirectoryAtPath:dirnameForUnzippedData error:NULL];
for (NSString *item in extractedItems) { ... }
It seems you are looking at the wrong path. Your code doesn't use dirpath (or your documentsDirectory).
As a side note, don't forget to release dirnameForUnzippedData after you're done with it - or just do dirnameForUnzippedData = #"extract"; instead of the alloc/init.
I would like to store files in a specific directory inside the documents directory, so that I can iterate over all these files and show them to the user for selection. Is that possible?
Yups,You can do it like:
NSString *rootString = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
self.root = [rootString stringByAppendingPathComponent:#"DirectoryName"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:root isDirectory:YES] == NO)
{
[fileManager createDirectoryAtPath:root attributes:nil];
}
And For retrieving you can do this:
NSError *error;
NSArray *files = [fileManager contentsOfDirectoryAtPath:root error:&error];
Hope this helps,
Thanks,
Madhup
Yes.
Use -[NSFileManager createDirectoryAtPath:withIntermediateDirectories:attributes:error:].
(The standard POSIX mkdir(2) also works.)