[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 447
  • 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
Turn Raw Data into a Real Career

There’s a growing demand for qualified analysts who can make sense of Big Data. With an MS in Data Analytics, you can become the data mining, management, mapping, and munging expert that today’s leading corporations desperately need.

 
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

Featured Post

Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

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