Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Directory browsing

kkvelankkvelan Posts: 1Member
I want to search or get all the files present in a particular folder. I use findtfirst() and findnext() in Dos. How to do in Linux? I heared somewhere that these two functions are available in Linux too..






Comments

  • sumitxshahsumitxshah Posts: 20Member
    : I want to search or get all the files present in a particular folder. I use findtfirst() and findnext() in Dos. How to do in Linux? I heared somewhere that these two functions are available in Linux too..


    Hi there!,
    Yes, you heard it right. There are function which reads every file in the particular directory. First you have to OPEN the directory then READ that particular directory after that don't forget to CLOSE that directory. And the function name are opendir() readdir() closedir()
    Here is the RUNNING source code for reading each file from a current directory.
    ENJOY............

    #define DIR_2_SCAN "/home/Automation/buffer/"


    void read_dir(void)
    {
    /*local declaration of function. */
    void fun_filter(char filename[100]);
    DIR *dp;
    struct dirent *fnd_file;
    while(1)
    {
    dp = opendir(DIR_2_SCAN);
    if (dp == NULL)
    {
    puts ("*** Couldn't open the directory.");
    exit(0);
    }
    while((fnd_file=readdir(dp))>0)
    {
    if((strlen(fnd_file->d_name))>10)
    fun_filter(fnd_file->d_name);
    }
    (void)closedir(dp);
    sleep(5);
    }
    }
    ____________________________________________________________________
    A winner is not the one who never fails , but the one who never quits.
    Sumit Shah.

Sign In or Register to comment.