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

  • SEARCH
  • Home
  • 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 9097333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:05:02+00:00 2026-06-17T00:05:02+00:00

I have a task that should be simple, Given a path, search all children

  • 0

I have a task that should be simple,

  • Given a path, search all children (1 level deep) for a less folder.
  • If the folder is found add the full path as a key to an array
  • Set the value for the key to be the same path, but replace less with css
  • Inside the less directory recursively loop all child directories
  • Add the sub directories in the same manner as the original directories

So, given this structure

Note: All items below except for randomfile are directories

matthew@vaio:/var/www/constructor/public/bundles$ tree
.
├── first
│   └── less
│       ├── secondtester
│       └── tester
│           ├── anothersubtester
│           ├── randomfile
│           └── subtester
├── second
│   └── less
│       ├── secondtester
│       └── tester
│           ├── anothersubtester
│           ├── randomfile
│           └── subtester
└── third
    └── noless
        ├── secondtester
        └── tester
            ├── anothersubtester
            ├── randomfile
            └── subtester

18 directories, 3 files

I want to end up with this array (note I have truncated the path here just to make it easier to read)

Array
    (
    [/b/second/less] => /b/second/css
    [/b/second/less/secondtester] => /b/second/css/secondtester
    [/b/second/less/tester] => /b/second/css/tester
    [/b/second/less/tester/subtester] => /b/second/css/tester/subtester
    [/b/second/less/tester/anothersubtester] => /b/second/css/tester/anothersubtester
    [/b/first/less] => /b/first/css
    [/b/first/less/secondtester] => /b/first/css/secondtester
    [/b/first/less/tester] => /b/first/css/tester
    [/b/first/less/tester/subtester] => /b/first/css/tester/subtester
    [/b/first/less/tester/anothersubtester] => /b/first/css/tester/anothersubtester
)

Now I have the below code, but I don’t think this is optimized at all, e.g. I know there are the RecursiveIteratorIterators etc, but I cannot work out how to use them for this task, so have had to resort to a recursive function that does the lifting. Basically, I am wondering how I could write this to be optimized better:

$directories = array();
$bundlePath = realpath('/public/bundles');

function lessSearcher($lessPath, $cssPath){
    $directories = array($lessPath => $cssPath);

    $lessDirs = new DirectoryIterator($lessPath);
    foreach ($lessDirs as $lessDir) {
        //we only want the directories and not the .'s
        if ($lessDir->isDot() || !$lessDir->isDir()) continue;
        $lessCurrent = $lessPath . '/' . $lessDir->getFileName();
        $cssCurrent = $cssPath . '/' . $lessDir->getFileName();
        $directories[$lessCurrent] = $cssCurrent;
        $directories = array_merge($directories, lessSearcher($lessCurrent, $cssCurrent));
    }

    return $directories;
}

$bundles = new DirectoryIterator($bundlePath);
foreach ($bundles as $bundle) {
    //we only want the directories and not the .'s
    if($bundle->isDot() || !$bundle->isDir()) continue;
    //we only want the directories that have a less directory
    if(!realpath($bundlePath.'/'.$bundle->getFileName().'/less')) continue;

    $lessPath = realpath($bundlePath . '/' . $bundle->getFileName()) . '/less';
    $cssPath = realpath($bundlePath . '/' . $bundle->getFileName()) . '/css';

    $directories = array_merge($directories, lessSearcher($lessPath, $cssPath));
}
  • 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-06-17T00:05:03+00:00Added an answer on June 17, 2026 at 12:05 am

    I think the code is correctly optimized.
    I made a script that lists all the directories and sub-directories and then removes the ones that doesn’t have the ‘less’ directory and creates a new array for the ones that do have it.
    Then I tested both yours and mine with a loop of 1000 times. Your script took an average of 0.93s and my script took 1.27s. So in my opinion, your code is okay.

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

Sidebar

Related Questions

I have a task that takes a rather long time and should run in
i have the task to build an artifact of an application that should be
I have been given a task to create a Python file that will take
I have a task that runs periodically 10 second. I do some picturebox refreshing
I have a task that's driving me crazy because i have no clue where
Just stumbled upon propertyGrid and its awesome! However, i have one task that i
I have a periodic task that needs to execute once a minute (using delayed_job).
I have an MSBuild task that executes (among other things) a call to xcopy.
I have a scheduled task that is very IO intensive (deleting hundreds of thousands
I have a Fabric task that needs to access the settings of my Django

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.