Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 796023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:34:32+00:00 2026-05-14T22:34:32+00:00

I have a bunch of files, either NSString s or NSURL s (it doesn’t

  • 0

I have a bunch of files, either NSStrings or NSURLs (it doesn’t matter; they are, for the most part, interchangeable), and I need a way to find the common ancestor directory. Does anyone know how to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:34:32+00:00Added an answer on May 14, 2026 at 10:34 pm

    I could have sworn there was a pathByJoiningPathComponents method somewhere, or at least one like it, but I must be thinking of something else. This does the trick for paths, it may work for URLs too if you’re on 10.6 (I only tested it with paths):

    NSString *path1 = @"/path/to/file1.txt";
    NSString *path2 = @"/path/to/file/number2.txt";
    
    NSArray *path1Comps = [path1 pathComponents];
    NSArray *path2Comps = [path2 pathComponents];
    
    NSUInteger total = [path1Comps count];
    if ([path2Comps count] < total)
        total = [path2Comps count];  // get the smaller of the two
    
    NSUInteger i;
    for (i = 0; i < total; i++)
        if (![[path1Comps objectAtIndex:i] isEqualToString:[path2Comps objectAtIndex:i]])
            break;
    
    NSArray *commonComps = [path1Comps subarrayWithRange:NSMakeRange(0, i)];
    
    // join commonComps together to get the common path as a string
    

    I don’t think there is a “built-in” way to do it, unfortunately.

    If you have an array of file paths that you want to find the common ancestor, you could do something like this:

    NSArray *allPaths = [NSArray arrayWithObjects:@"/path/to/1.txt", @"/path/to/number/2.txt", @"/path/to/number/3/file.txt", nil];
    
    // put some checks here to make sure there are enough paths in the array.
    
    NSArray *currentCommonComps = [[allPaths objectAtIndex:0] pathComponents];
    
    for (NSUInteger i = 1; i < [allPaths count]; i++)
    {
        NSArray *thisPathComps = [[allPaths objectAtIndex:i] pathComponents];
        NSUInteger total = [currentCommonComps count];
        if ([thisPathComps count] < total)
            total = [thisPathComps count];
    
        NSUInteger j;
        for (j = 0; j < total; j++)
            if (![[currentCommonComps objectAtIndex:j] isEqualToString:[thisPathComps objectAtIndex:j]])
                break;
    
        if (j < [currentCommonComps count])
            currentCommonComps = [currentCommonComps subarrayWithRange:NSMakeRange(0, j)];
    
        if ([currentCommonComps count] == 0)
            break; // no point going on
    }
    
    // join currentCommonComps together
    

    You may want to explicitly allocate and release some of these objects if you want to keep the autorelease pool clean, especially if you have a large array of paths.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of files that need to be parsed, and they all
I have a bunch of files that I need to be able to transport
I have a bunch of files with coordinates in UTM form. For each coordinate
Say you have a bunch of files. Say you can store meta data to
I'd sooner not just permanently have a bunch of files checked out to me,
I have a whole bunch of files with filenames using our lovely Swedish letters
I have a bunch of perfmon files that have captured information over a period
I have a bunch (hundreds) of files that are supposed to have Unix line
I have a bunch of PDF files and my Perl program needs to do
I have a bunch of java files from which I want to remove the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.