Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

*** Search DIR for file name ***********

Status
Not open for further replies.

DaniDak

Technical User
Mar 13, 2002
44
0
0
US
Hi

How can I do the following using C in UNIX.

I need to search a directory for a file and open that file.
The directory will contain log files and some other file, that have a file name of:
somename_DTS.log, DTS being date time stamp (20040429104607) when the file was created.
the only thing that is different between the log file names is the DTS.
What I need to do is to get the name of the newest file in the directory.
Is there anything in UNIX that I could use in my C program to find the newest file
and get its name. Or do I have to load all the files into an array than sort it and get the newest file name that way.

files that match
Thank you
 
You don't have to load in all the files: only the filenames and their details. Look up the man page on opendir, readdir and closedir.

Alternatively, since you have the names in such a convenient form, popen ("ls -1 | sort", "r") and start reading. It is more inefficient but will give you the names in some sort of order.
 
Here's an example illustrating what xwb suggested. It uses opendir,readdir,closedir and stat to get the info you need.

Code:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>

int main(void) {

  DIR *dir;
  struct dirent *ent;
  struct stat statbuf;
  time_t new_t=0;
  char new_n[100] = "";

  dir = opendir(".");

  while ((ent = readdir(dir)) != NULL) {
    if (strstr(ent->d_name,"_DTS")) { 
      stat(ent->d_name, &statbuf);
      if (statbuf.st_mtime > new_t) {
        strcpy(new_n,ent->d_name);
        new_t = statbuf.st_mtime;
      }
    }
  }

  printf("newest file is %s\n",new_n);
  closedir(dir);

  return 0;
}

Good luck.
 
Thanks for your information.
I really do appreciated.
 
Can somebody help me out if i need to read the last 10 oldest files from a directory without taking into array. The above gives the newest or the oldest.

Thanks for your help
 
If you are going to use xwb's method using popen, then this command probably will work:

ls -1rt mydir|head -10
 
Is there a way to get this done using STAT and dirent structures. because i dont want to make a systemcall or popen call to ls -lrt.

 
Well basically, you take the itsgsd's answer and add this

Code:
time_t then;
time_t now = time(NULL);
struct tm tm_now = *localtime( &now );
tm_now.tm_mday -= 10;     /* 10 days ago */
then = mktime( &tm_now ); /* now we have a time_t for 10 days ago */

Then inside the loop, we simply have
Code:
if ( statbuf.st_mtime > then )
/* file is newer than 10 days old */

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top