LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-03-2005, 11:53 AM   #1
os2
Member
 
Registered: Dec 2003
Location: Canada
Distribution: openSUSE Tumbleweed
Posts: 209

Rep: Reputation: 30
scan folder to search file


hi

in a c program, we need to scan a directory and find all: hist*.htm file

are there any special c function to do the job

thanks
 
Old 03-03-2005, 12:02 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Scanning a directory is implementation-dependent. Which OS and compiler are you using?
 
Old 03-03-2005, 12:07 PM   #3
os2
Member
 
Registered: Dec 2003
Location: Canada
Distribution: openSUSE Tumbleweed
Posts: 209

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by itsme86
Scanning a directory is implementation-dependent. Which OS and compiler are you using?
we use linux and gcc (ppc)
 
Old 03-03-2005, 01:12 PM   #4
os2
Member
 
Registered: Dec 2003
Location: Canada
Distribution: openSUSE Tumbleweed
Posts: 209

Original Poster
Rep: Reputation: 30
the file i need to search are: hist_2005-02-24.htm..

hist_0000-00-00.htm

0 can be any number

i begining to write this code

Code:
int searchfile(char *history_dir, char *filename){
  DIR *pdir;
  pdir = opendir(history_dir);
  struct dirent *pent;
  if (!pdir){
    printf ("%s - Incapable d'utiliser opendir()", strerror (errno));
    return 1;
 }
 while ((pent=readdir(pdir))!=NULL){
   printf("%s", pent->d_name);
 }
  closedir(pdir);
  return 0;
}
 
Old 03-03-2005, 01:25 PM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You can do something like:
Code:
if(strncmp(pent->d_name, "hist", 4) == 0 && strcmp(pent->d_name+15, ".htm") == 0)
{
  // It's a match
}
 
Old 03-03-2005, 01:26 PM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
I have made this some time ago.
I don't know if that's what you need, but it scans down sub-directories.

Change the line: #define FILENAME_FILTER "*.txt" to the shell pattern as you need.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <fnmatch.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>

#define FILENAME_FILTER "*.txt"

int scandirectory(const char *dirname)
{
    DIR *dir;
    struct dirent *entry;
    char path[PATH_MAX];

    dir = opendir(dirname);
    if (dir == NULL) {
        perror("Error opendir()");
        return 0;
    }
    while ((entry = readdir(dir)) != NULL) {
        if (entry->d_type == DT_DIR) {
            if (strcmp(entry->d_name, ".")
                && strcmp(entry->d_name, "..")) {
                snprintf(path, (size_t) PATH_MAX, "%s/%s", dirname,
                         entry->d_name);
                scandirectory(path);
            }
        } else if (entry->d_type == DT_REG) {
            if (!fnmatch(FILENAME_FILTER, entry->d_name, 0)) {

                /*
                 * This is the place where a file matching the filter is
                 * found. Do file processing here, in this case, just print. 
                 */

                printf("%s/%s\n", dirname, entry->d_name);
            }
        }
    }
    closedir(dir);
    return 1;
}


int main(int argc, char *argv[])
{
    if (argc != 2) {
        fprintf(stderr, "Usage:\t%s <directory>\n", *argv);
        return 1;
    }
    return !scandirectory(argv[1]);
}

Last edited by Hko; 03-03-2005 at 01:35 PM.
 
Old 03-03-2005, 01:33 PM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
-- Accidental double post.
-- Please ignore/remove.
 
Old 03-03-2005, 01:37 PM   #8
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Huh, I never came across fnmatch() before. That's cool.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why Do I need to run updatedb each time I search for a file or folder? lennysokol Linux - Software 4 05-30-2005 10:49 PM
F-prot Anti-virus scan log, suspicous file question. webwolf70 Linux - Security 5 11-16-2004 09:15 AM
Find File broken, need search utility, where does WineX install, KDE file roller? Ohmn Mandriva 6 07-05-2004 10:34 PM
how can i default the max folder file size when it create inside a folder antony_csf Linux - Software 1 06-17-2004 02:26 AM
How to 'apt-cache search' & 'apt-file search' by distribution? davidas Debian 3 04-19-2004 01:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration