• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 455
  • Last Modified:

Displaying File Date and TimevStamp

I asked this question earlier, But I am not getting the correct results.
Tyring to run this code on an AIX /Unix system.
But I am not able to display the Date and time of the File correctly. What am I doing wrong.? Can anyone help me

 DIR *dirp;
    struct dirent *fileNames[2048];  
    char dtBuf[30];
    struct dirent *direntp;
    struct stat info;
   
    dirp = opendir( InboxDirectory );
    if( dirp != NULL ) {
     for(;;) {
       direntp = readdir( dirp);
     if( direntp == NULL ) break;
      if (directp->d_name[0] == '.')
        continue;    
     fileNames[fcnt] = (struct dirent *) malloc(sizeof(struct dirent));
     memset(fileNames[fcnt], '\0', sizeof(struct dirent));
     memset(&info, '\0', sizeof(struct stat));
     memcpy(fileNames[fcnt], direntp, sizeof(struct dirent));
     stat (fileNames[fcnt]->d_name, &info);
     strftime(dtBuf, 30, "%D, %H:%M:%S", localtime(&info.st_mtime));
     printf("File Date/Time Stamp: %s", dtBuf);    
     }
    closedir( dirp );
 }
0
Loysi
Asked:
Loysi
  • 4
  • 2
1 Solution
 
LoysiAuthor Commented:
couple of errors in the code. I just fixed. (Portion of the code I am using.)

DIR *dirp;
   struct dirent *fileNames[2048];  
   char dtBuf[30];
   struct dirent *direntp;
   struct stat info;
   
   dirp = opendir( InboxDirectory );
   if( dirp != NULL ) {
    for(;;) {
      direntp = readdir( dirp);
     if( direntp == NULL ) break;
      if (directp->d_name[0] == '.')
        continue;    
     fileNames[fcnt] = (struct dirent *) malloc(sizeof(struct dirent));
     memset(fileNames[fcnt], '\0', sizeof(struct dirent));
     memset(&info, '\0', sizeof(struct stat));
     memcpy(fileNames[fcnt], direntp, sizeof(struct dirent));
     stat (fileNames[fcnt]->d_name, &info);
     strftime(dtBuf, 30, "%D, %H:%M:%S", localtime(&info.st_mtime));
     printf("File Date/Time Stamp: %s\n", dtBuf);  
     fcnt++;
    }
   closedir( dirp );
}
0
 
woregulCommented:
I ran your snippet on AIX, and it produced good output, so I am not sure I understand your problem. See if the following code will resolve your issue. I modified the date format a little.


#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>

int main(int argc, char **argv)
{
  int fcnt = 0;
  DIR *dirp;
  struct dirent *fileNames[2048];
  char dtBuf[30];
  struct dirent *direntp;
  struct stat info;

  dirp = opendir( "." );

  if( dirp != NULL ) {
   for(;;) {
     direntp = readdir( dirp);
    if( direntp == NULL ) break;
     if (direntp->d_name[0] == '.')
       continue;

    fileNames[fcnt] = (struct dirent *) malloc(sizeof(struct dirent));
    memset(fileNames[fcnt], '\0', sizeof(struct dirent));
    memset(&info, '\0', sizeof(struct stat));
    memcpy(fileNames[fcnt], direntp, sizeof(struct dirent));
    stat (fileNames[fcnt]->d_name, &info);
    strftime(dtBuf, 30, "%m/%d/%Y %H:%M:%S", localtime(&info.st_mtime));
    printf("%s %s\n", fileNames[fcnt]->d_name, dtBuf);
    fcnt++;
   }
  closedir( dirp );
  }
}
0
 
LoysiAuthor Commented:
Thanks woregul, for your quick response.
I changed the date format and got better results but still not correct.
The date and time of my file using ls -l
displays jun 23 2002 19:25
My program displays  12/31/1969 19:00:00
I guessing that is CUT (Coordinated Universal time).
What can I do to get it give me the same info as ls does.?
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
LoysiAuthor Commented:
Message for Woregul, I retested the snippet and noticed it worked well for the root directory. recoded my program and added the path to the stat Function and it worked.
Got another question. for you. Can you tell me if there is an easy way to find files in a directory based on first in bases.? I'll double my points!
0
 
woregulCommented:
You could use scandir() to read the contents of a directory into an optionally sorted array of pointers to dirent structures. You would have to write your own compare function that compares 2 dirent structure pointers and see which one is newer. Once complete, you would have a sorted list by file date.

0
 
LoysiAuthor Commented:
Thanks Woregul, My very Thoughts, I was thinking in those very lines.
Thanks for your help. The solution was staring me in my face and I could not get it to work, I tried to use fstat and that gave me erratic results. I don't know why!  hence the question.
I doubled my points as promised.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

KuppingerCole Reviews AlgoSec in Executive Report

Leading analyst firm, KuppingerCole reviews AlgoSec's Security Policy Management Solution, and the security challenges faced by companies today in their Executive View report.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now