Solved

Displaying File Date and TimevStamp

Posted on 2002-06-24
6
428 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

744 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

12 Experts available now in Live!

Get 1:1 Help Now