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 237291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:24:49+00:00 2026-05-11T20:24:49+00:00

I want to scan a directory tree and list all files and folders inside

  • 0

I want to scan a directory tree and list all files and folders inside each directory. I created a program that downloads images from a webcamera and saves them locally. This program creates a filetree based on the time the picture is downloaded. I now want to scan these folders and upload the images to a webserver but I´m not sure how I can scan the directories to find the images.
If anyone could post some sample code it would be very helpful.

edit: I´m running this on an embedded linux system and don´t want to use boost

  • 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-11T20:24:49+00:00Added an answer on May 11, 2026 at 8:24 pm

    See man ftw for a simple “file tree walk”. I also used fnmatch in this example.

    #include <ftw.h>
    #include <fnmatch.h>
    
    static const char *filters[] = {
        "*.jpg", "*.jpeg", "*.gif", "*.png"
    };
    
    static int callback(const char *fpath, const struct stat *sb, int typeflag) {
        /* if it's a file */
        if (typeflag == FTW_F) {
            int i;
            /* for each filter, */
            for (i = 0; i < sizeof(filters) / sizeof(filters[0]); i++) {
                /* if the filename matches the filter, */
                if (fnmatch(filters[i], fpath, FNM_CASEFOLD) == 0) {
                    /* do something */
                    printf("found image: %s\n", fpath);
                    break;
                }
            }
        }
    
        /* tell ftw to continue */
        return 0;
    }
    
    int main() {
        ftw(".", callback, 16);
    }
    

    (Not even compile-tested, but you get the idea.)

    This is much simpler than dealing with DIRENTs and recursive traversal yourself.


    For greater control over traversal, there’s also fts. In this example, dot-files (files and directories with names starting with “.”) are skipped, unless explicitly passed to the program as a starting point.

    #include <fts.h>
    #include <string.h>
    
    int main(int argc, char **argv) {
        char *dot[] = {".", 0};
        char **paths = argc > 1 ? argv + 1 : dot;
    
        FTS *tree = fts_open(paths, FTS_NOCHDIR, 0);
        if (!tree) {
            perror("fts_open");
            return 1;
        }
    
        FTSENT *node;
        while ((node = fts_read(tree))) {
            if (node->fts_level > 0 && node->fts_name[0] == '.')
                fts_set(tree, node, FTS_SKIP);
            else if (node->fts_info & FTS_F) {
                printf("got file named %s at depth %d, "
                    "accessible via %s from the current directory "
                    "or via %s from the original starting directory\n",
                    node->fts_name, node->fts_level,
                    node->fts_accpath, node->fts_path);
                /* if fts_open is not given FTS_NOCHDIR,
                 * fts may change the program's current working directory */
            }
        }
        if (errno) {
            perror("fts_read");
            return 1;
        }
    
        if (fts_close(tree)) {
            perror("fts_close");
            return 1;
        }
    
        return 0;
    }
    

    Again, it’s neither compile-tested nor run-tested, but I thought I’d mention it.

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

Sidebar

Related Questions

I want to recursively scan a directory and all its sub-directories for files with
I want not scan all the files in a directory and its sub-directory. And
Often I would want to scan an entire directory tree (a directory, and everything
In order to localize strings used within my javascript, I want scan all my
I want to create an application for Android that will be able to scan
I want to write an OSX (Snow Leopard) app that receives notifications when files
How do you scan a file with java that isn't in the directory the
I want to delete cache files in a directory, the directory can contain up
I have a folder named Comics and sub folders in that directory with the
i am looking to build a php function which will list all folders and

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.