View Full Version : Scanning in file names in a Directory

Feb 1, 2012, 04:22 PM
Should I use the scanf or fgets to read in the file names of my PDF docs I have sitting in a directory in the phone? I want to display the name of the items in a tableview. I was unsure how to read in the file names that I would add to an NSArray.


Feb 1, 2012, 05:27 PM
Why think so low-level? NSFileManager (http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html) is your friend.

Feb 1, 2012, 05:36 PM
Look through the NSFileManager docs. You can read in the names of all the files in a folder in one method call.

Feb 3, 2012, 03:38 PM
Sorry, it was a dumb question. I was at the airport waiting on a 4 hour layover to Sweden. I had internet on my phone but not on my computer. I had no idea that the doc's needed the internet to work!

Suddenly I had to rely on my memory and not the internet and doc's to help me with the answer. it is so slow using my iphone with data roaming to search anything. After 30 minutes I was able to reason it out. When I did I knem my scanf and fgets was the wrong thing to ask for.

This code worked fine. Although I thought it was strange that I could not use the contentsOfDirectoryAtPath: with a mutableArray, it kept giving me a error so I used this code and then copied it to a mutableArray

NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/PDF"];
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSArray *tempArray = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:nil];
itemsInDirectoryArray = [NSMutableArray arrayWithArray:tempArray];

[self.tableView reloadData];

By the way, can I download the docs to my computer or does it allways need to relly on an internet connection?

Feb 3, 2012, 03:50 PM
By the way, can I download the docs to my computer or does it allways need to relly on an internet connection?

You can download them. What does your Documentation preferences (under Downloads) in Xcode show?

Feb 4, 2012, 01:29 AM
Here is the window

Feb 4, 2012, 02:27 AM
Wirelessly posted (Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3)

If you do want viewable documentation on your iPhone or iPad, I've found that Apple has made several of them available as iBooks. It seems the rest are available as PDF files which iBooks does quite well at displaying. It's not a replacement for a computer, mind you, but it works great in a pinch.