Get file path by file name from documents directory ios

32,282

Solution 1

You can search the documents directory like this:

NSString *searchFilename = @"hello.pdf"; // name of the PDF you are searching for

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager] enumeratorAtPath:documentsDirectory];

NSString *documentsSubpath;
while (documentsSubpath = [direnum nextObject])
{
  if (![documentsSubpath.lastPathComponent isEqual:searchFilename]) {
    continue;
  }

  NSLog(@"found %@", documentsSubpath);
}

EDIT:

You can also use NSPredicate. If there are many thousands of files in the documents directory, this might crash with an out of memory error.

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.lastPathComponent == %@", searchFilename];
NSArray *matchingPaths = [[[NSFileManager defaultManager] subpathsAtPath:documentsDirectory] filteredArrayUsingPredicate:predicate];

NSLog(@"%@", matchingPaths);

Solution 2

Swift 2.2 pretty simple:

let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first! as NSString
let plistPath = paths.stringByAppendingPathComponent("someFile.plist")

Solution 3

You'll have to walk the tree to find the file; there's no equivalent to -pathForResource:ofType that works in the ~/Documents directory.

Share:
32,282
ViruMax
Author by

ViruMax

ALL DAY: Write code for Qnap's iOS applications. EVENING: Workout NIGHT: Watch movies & cricket & sleep & study.

Updated on September 20, 2020

Comments

  • ViruMax
    ViruMax over 3 years

    In my application I download PDF files which gets stored in "Document" directory under different sub folders.

    Now I have file name for which I want to get its path in "Document" directory but problem is I don't know the exact sub folder under which that file is stored.

    So is there any method which will give me file path by file's name like there is one method which works for main bundle:

    (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension
    

    I don't want to iterate through each folder which is a tedious way.

    Thanks.

  • ViruMax
    ViruMax almost 10 years
    means will have to try for all possible paths
  • ViruMax
    ViruMax almost 10 years
    thanx, it seems that there is no alternative to search for all possible paths.
  • Abhi Beckert
    Abhi Beckert almost 10 years
    @ViruMax there are alternatives. For example you could grab every subpath (file manager has a supbathsAtPath: method) and then filter the returned array using a predicate (@"self.lastPathComponent == %@", searchFilename. But honestly it's better to just enumerate all the directories. It's not that much code.
  • Abhi Beckert
    Abhi Beckert almost 10 years
    See my updated answer for an example of NSPredicate.
  • ViruMax
    ViruMax about 5 years
    This answer doesn't address the problem given in the question, what if there are many subfolders under document directory, this logic won't work in that case.