Mihai C.
Mihai C.

Reputation: 15

Xcode OSX - How to read the content of a directory from usb

I need to make a mac app which will run from an usb device.The app is a product presentation app. I need to check if some directory exist in order to define the product category I can display. If the directory for a category doesn't exit that category will not be displayed. Then i have to check all the jpg files from each category directory and store them in order to show them when the user select one.

My problem is that I have no idea how to access the files from the usb device. I thought I could use for listing the content like this:

NSFileManager *fileManager = [[NSFIleManager alloc] init];
NSString *usbRootPath = [fileManager currentDirectoryPath];
// Reading the root content
NSArray *fileList = [fileManager contentAtDirectoryAtPath:usbRootPath error:NULL];
// Printing the content
for(int counter=0; counter < [fileList count]; counter++)
      NSLog(@"%@", [fileList objectAtIndex:counter]);

The usbRootPath (after I build the app and copy it on usb) is "/" and the content list contains all the directories/files from system drive.

How can I read/verify a file /directory/content of a directory located on usb drive (my app being copy there in root).

Any help will be appreciated! Thanks in advance!

Upvotes: 0

Views: 849

Answers (2)

user149341
user149341

Reputation:

NSString *usbRootPath = [fileManager currentDirectoryPath];

This code retrieves the current working directory of your process. This is not the same as the directory your application is located in; in most cases, it will be the system root directory (as you've discovered). The path to a USB device should be of the form /Volumes/devicename.

To find the path to your application, use:

NSString *applicationPath = [[NSBundle mainBundle] bundlePath];

Then take the parent directory (if appropriate) and work from there.

Upvotes: 3

john elemans
john elemans

Reputation: 2676

All mounted volumes can be found in /Volumes. If you make sure that your USB drives have a specified name, you can look in /Volumes for "myUSBDriveName" and then go down from there.

Upvotes: 0

Related Questions