Solved

Displaying File Date and TimevStamp

Posted on 2002-06-24
6
430 Views
Last Modified: 2010-04-15
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
Comment
Question by:Loysi
  • 4
  • 2
6 Comments
 

Author Comment

by:Loysi
ID: 7103976
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
 

Expert Comment

by:woregul
ID: 7104235
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
 

Author Comment

by:Loysi
ID: 7104304
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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:Loysi
ID: 7104607
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
 

Accepted Solution

by:
woregul earned 100 total points
ID: 7104754
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
 

Author Comment

by:Loysi
ID: 7104859
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now