-1

I need to detect when an USB drive e.g. Office23, is mounted in OS X and also search for a file eg. jan.txt inside the drive and copy it to app bundle.

I am using following method to find if volume is mounted or not:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = error;

NSArray *keys = @[NSURLVolumeNameKey, NSURLVolumeIsRemovableKey, NSURLVolumeIsEjectableKey];
NSArray *volumeURL = [fileManager mountedVolumeURLsIncludingResourceValuesForKeys:keys options:NSVolumeEnumerationProduceFileReferenceURLs];

for (NSURL *url in volumeURL) {
    NSLog(@"URL %@", url);
}

Result: file:///.file/id=6571367.2/

How can I get the NSURL of the file inside Volume?

EDIT 1:

 NSArray *keys = [NSArray arrayWithObjects:NSURLVolumeNameKey, NSURLVolumeIsRemovableKey, nil];
    NSArray *urls = [[NSFileManager defaultManager] mountedVolumeURLsIncludingResourceValuesForKeys:keys options:0];
    NSURL *officeDriveURL;
    for (NSURL *url in urls) {
        NSError *error;
        NSNumber *isRemovable;
        NSString *volumeName;
        [url getResourceValue:&isRemovable forKey:NSURLVolumeIsRemovableKey error:&error];
        if ([isRemovable boolValue]) {
            [url getResourceValue:&volumeName forKey:NSURLVolumeNameKey error:&error];
            if ([[url absoluteString] containsString:@"Office"]) {
                officeDriveURL = url;
            }

        }
    }
    NSURL *fullURL = [officeDriveURL URLByAppendingPathComponent:@"documents/My File2.txt"];

    if ([[NSFileManager defaultManager]fileExistsAtPath:[fullURL absoluteString]]) {
        NSLog(@"YES FILE Exist %@", fullURL);
    }

Still, FileManager cannot find the url.

The fullURL is: file:///Volumes/Office/documents/My%File2.txt

When I check this through Finder or NSFileManager, this does not not work, but the path is correct.

Napier
  • 105
  • 8
  • Why are you passing `NSVolumeEnumerationProduceFileReferenceURLs`? The [documentation](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/#//apple_ref/swift/structdata/NSVolumeEnumerationOptions/c:@E@NSVolumeEnumerationOptions@NSVolumeEnumerationProduceFileReferenceURLs) says that it will produce file reference URLs rather than path-based URLs. Pass zero as argument for `options` and you should be good to go. – Cristik Jan 24 '16 at 19:14
  • @Cristik, I altered the method, and getting the Volume path but when I add the extension to it, NSFileManager cannot find it. – Napier Jan 24 '16 at 19:23
  • @Cristik, the path is correct, still unable to locate by it. – Napier Jan 24 '16 at 19:31
  • I mean, result is False for fileExistAtPath – Napier Jan 24 '16 at 19:39
  • Look at `NSWorkspaceDidMountNotification` of `NSWorkspace` – vadian Jan 24 '16 at 21:11

2 Answers2

0

I don't know why FileManager did not find the file using [NSURL absoluteString] method, When I removed file:// from file:///Volumes/Office/documents/My%File2.txt and also modified the %20, that got introduced (My%20File2,txt), it worked.

Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38
Napier
  • 105
  • 8
0

Use [fullURL path] instead of [fullURL absoluteString]

if ([[NSFileManager defaultManager]fileExistsAtPath:[fullURL path]]) {
    NSLog(@"YES FILE Exist %@", fullURL);
}
MartinV
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 26 '23 at 17:04