list certain files inside directory

C

cerr

Hi There,

I would like to list all files in a defined direcory that match a
certain pattern.
I would like to get the paths from every file that matches "/usr/share/
NovaxTSP/log_record*". I understand that I need to be using opendir()
but haven't quite figured out how i can do that.
Any suggestion, hints or examples are appreciated!

Thanks!
Ron
 
A

Antoninus Twink

I would like to list all files in a defined direcory that match a
certain pattern.
I would like to get the paths from every file that matches "/usr/share/
NovaxTSP/log_record*". I understand that I need to be using opendir()
but haven't quite figured out how i can do that.

Why not use glob(3) if you want to do globbing?

#include <stdio.h>
#include <glob.h>

#define PATH "/usr/share/NovaxTSP/log_record*"

int main(void)
{
glob_t buf;

if(glob(PATH, 0, NULL, &buf) == 0) {
char **s = buf.gl_pathv;
size_t i = buf.gl_pathc;
while(i--)
puts(*s++);
globfree(&buf);
}
return 0;
}
 
I

Ian Collins

cerr said:
Hi There,

I would like to list all files in a defined direcory that match a
certain pattern.
I would like to get the paths from every file that matches "/usr/share/
NovaxTSP/log_record*". I understand that I need to be using opendir()
but haven't quite figured out how i can do that.
Any suggestion, hints or examples are appreciated!

There isn't a standard C way. Try asking in comp.unix.programmer, or
use the shell.
 
C

cerr

Hi There,

I would like to list all files in a defined direcory that match a
certain pattern.
I would like to get the paths from every file that matches "/usr/share/
NovaxTSP/log_record*". I understand that I need to be using opendir()
but haven't quite figured out how i can do that.
Any suggestion, hints or examples are appreciated!

Thanks!
Ron

Uh, I think i just figured it out:

struct dirent *dp;
// enter existing path to directory below
const char *dir_path="/usr/share/NovaxTSP/";
DIR *dir = opendir(dir_path);
while((dp=readdir(dir))!=NULL){
if(strstr(dp->d_name, "log_record")!=NULL)
printf("filename: %s\n",dp->d_name);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top