Manikandan
Manikandan

Reputation: 321

NSURL comparison: compare with different URL

I have searched for this but i didn't get the correct keyword to get the result. so i come with question. Here is my question.

How to give if condition for checking, Whether the given NSURL path is for the FOLDER (or) a text file.
For Example:

 if ([the given url is for FOlDERS])  
 {
    //i want to so something
 }
 else if([it contains .rtf files])
 {
    //i want to so something
 }
  else if([it contains .mov files])
  {
    //i want to so something
  }        

my url's are

Users/Desktop/dr/dg.rtf  
Users/Desktop/dr/dhoom.mov  
Users/Desktop/dr/SampleTest/

give me some suggestions.,

Upvotes: 1

Views: 555

Answers (3)

Shebuka
Shebuka

Reputation: 3248

How about this?

BOOL isDir;
NSURL *yourURL;

NSFileManager *fileManager = [[NSFileManager alloc] init];
BOOL result = [fileManager fileExistsAtPath:[yourURL path] isDirectory:&isDir];
if (result) {
    // ok yourURL is pointing to something real
    if (isDir) {
        // yourURL is a directory
    }
    else if ([[yourURL pathExtension] isEqualToString:@"mov"])
    {
        // yourURL is a .mov
    }
    else if ([[yourURL pathExtension] isEqualToString:@"rtf"])
    {
        // yourURL is a .rtf
    }
}
[fileManager release];

combination of Michal and werner answers with fixed syntax.

Upvotes: 1

Michal
Michal

Reputation: 971

NSURL *url;
//set url here

if([url isFileURL])
{
  if([url pathExtension] isEqualToString:@"mov"])
  {
    //do .mov stuff
  }
  else if([url pathExtension] isEqualToString:@"rtf"]
  {
    //do .rtf stuff
  }
}
else
{
   if([[url lastPathComponent] isEqualToString:@"SampleTest"])
   {
     //do your SampleTest folder stuff
   }
}

Upvotes: 2

werner
werner

Reputation: 859

Please take a look at the documentation for NSFileManager.

It contains a method that does exactly what you need :

- (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory

Upvotes: 0

Related Questions