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];
}
Related
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
Suppose, I have added one folder name "Images" in my project.How can I get the path to that folder? My main intention is to get the number of pictures in "Images" folder.
You should work a bit more on your question: it assumes a lot and requires the reader to guess.
I have added one folder name "Images" in my project
So I guess this means you added it as a folder reference
and I want to get it's path
And I guess that you want to do that at run time from your application, not at build-time from Xcode.
If so, you could do something like:
NSURL *containingURL = [[NSBundle mainBundle] resourceURL];
NSURL *imageURL = [containingURL URLByAppendingPathComponent:#"Images" isDirectory:YES];
NSFileManager *localFileManager = [[NSFileManager alloc] init];
NSArray *content = [localFileManager contentsOfDirectoryAtURL:imageURL includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsSubdirectoryDescendants error:NULL];
[localFileManager release];
NSUInteger imageCount = [content count];
This code does not assume that all images are of the same kind.
[[[NSBundle mainBundle] pathsForResourcesOfType:#"jpg" inDirectory:#"Images"] count];
This returns the number of jpg images from the Images folder. This is the case if you added the images to your application bundle.
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 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];
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.)