0

The problem here is that I cannot appending a file name using "stringByAppendingPathComponent" fma is a NSFileManager.

Working:

[fma changeCurrentDirectoryPath: [NSHomeDirectory() stringByAppendingPathComponent: @"Music/iTunes/iTunes Media/Music/ABC/DEF"]];
NSLog(@"Current Directory Path: %@", [fma currentDirectoryPath]); 

Output: Current Directory Path: /Users/plusa/Music/iTunes/iTunes Media/ABC/DEF

Not working:

[fma changeCurrentDirectoryPath: [NSHomeDirectory() stringByAppendingPathComponent: @"Music/iTunes/iTunes Media/Music/ABC/DEF/a.mp3"]];
NSLog(@"Current Directory Path: %@", [fma currentDirectoryPath]); // No change

Update

What I'm going to do is creating a simple fileManager.

main.m

#import <Foundation/Foundation.h>
#import "fileManager.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        NSLog(@"Welcome to File Manager.");

        fileManager *fma = [[fileManager alloc] init];

        NSLog(@"Current Directory Path: %@", [fma currentDirectoryPath]);
        [fma changeCurrentDirectoryPath: [NSHomeDirectory() stringByAppendingPathComponent: @"Music/iTunes/iTunes Media/Music"]];
        NSLog(@"Directory Path changed to iTunes Music source base");

        [fma readCurrentItem];

    }
    return 0;
}

fileManager.h

#import <Foundation/Foundation.h>

@interface fileManager : NSFileManager {
    NSArray *list;
}

-(int) readCurrentItem;

@end

fileManager.m

#import "fileManager.h"

@implementation fileManager

-(int) readCurrentItem
{
    int i = 1;

    NSLog(@"Current Directory Path: %@", [self currentDirectoryPath]);
    NSLog(@"Reading Directory Path...");

    if(NSFileTypeDirectory == [[self attributesOfItemAtPath: [self currentDirectoryPath] error: nil] objectForKey: @"NSFileType"]) {
        list = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self currentDirectoryPath] error: nil];
    } else {
        NSLog(@"File");
        return 0;
    }

    i = 1;
    for (NSString *item in list)
        NSLog(@"Item #%i: %@", i++, item);


    NSLog(@"The item to read:");
    scanf("%i", &i);

    if (i == 0) {
        NSLog(@"Shutdown.");
        return 0;
    }

    [self changeCurrentDirectoryPath: [[self currentDirectoryPath] stringByAppendingPathComponent: [list[(i - 1)] lastPathComponent]]];

    [self readCurrentItem];

}

@end
justin
  • 104,054
  • 14
  • 179
  • 226
PlusA
  • 545
  • 1
  • 6
  • 15

1 Answers1

1

Because your second example refers to a file, not a directory.

Presumably, the path is being composed correctly, but the NSFileManager is not changing the path to the file -- again, because it is not a directory.

justin
  • 104,054
  • 14
  • 179
  • 226
  • @PlusA I expanded the answer -- if that does not help, you will have to be more specific in your wording. – justin Nov 02 '12 at 09:03
  • So, I cannot handle a file using NSFileManager? Should use which method to instead? – PlusA Nov 02 '12 at 09:03
  • @PlusA you certainly can, but i think perhaps you are misunderstanding what the "current directory path" is used for (convenience for referring to paths *relative to* the specified directory). but how do you want to "handle" a file? – justin Nov 02 '12 at 09:06
  • Sorry for my english! I'm creating a simple fileManager for exploring a directory and let user choose which one do he want to read, and I using this for getting a path. Use NSData if it's a file, but even I cannot add the "pointer" to NSFileManager to pointing the path of the file. I expanded the question now. – PlusA Nov 02 '12 at 09:11
  • @PlusA you can use `fileExistsAtPath:isDirectory:` rather than attributes. – justin Nov 02 '12 at 09:22
  • @PlusA verify the current directory path is never nil – justin Nov 02 '12 at 09:23
  • @PlusA `[self changeCurrentDirectoryPath: [[self currentDirectoryPath] stringByAppendingPathComponent: [list[(i - 1)] lastPathComponent]]];` assumes some things about the directory structure. logically, your program should stop and do something with the file, rather than assigning the file as the current directory path, since the current directory path must not be a file. so, first test if the subpath is a file -- if it is, *then* `NSLog(@"File"); \n return 0;`. – justin Nov 02 '12 at 09:26