Solved

Directory Date

Posted on 2001-07-12
30
257 Views
Last Modified: 2010-08-05
Hi all,
I would like to know, "How can i find the Last Modified Date of a Particular Directory using C++".
Please reply soon..

TIA
L G Goundalkar.
0
Comment
Question by:LG_Goundalkar
  • 15
  • 14
30 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 6276632
There is no way to do this using standard C/C++.  Standard C/C++ is OS indepedant.  And not all OSs even suppor tthe concept of directories.

However it can be done through OS depedant means.  What OS are you using?
0
 
LVL 22

Expert Comment

by:nietod
ID: 6276641
In windows, one way is to use FindFirstFile() to search for the directory name.  If found, it returns the dates associated with the directory.  (Date created, modified etc.)
0
 

Author Comment

by:LG_Goundalkar
ID: 6276644
Hi nietod..
I am using Windows NT.
So you can consider OS as Windows XX :)
reply soon
Thanks.
L G Goundalkar.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6276647
Ah here is probalby a better way (it depends on what you are doing)  You can use GetFileAttributesEx().   (Its what I had been looking for, but the name fooled me.)

Again, that is only for windows.
0
 

Author Comment

by:LG_Goundalkar
ID: 6276660
Hi nietod..
I am using Windows NT.
So you can consider OS as Windows XX :)
reply soon
Thanks.
L G Goundalkar.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6276664
Then try

const char *FileName = "C:\\"  // Root dir.
WIN32_FILE_ATTRIBUTE_DATA FilInf;

GetFileAttributesEx(FileName,GetFileExInfoStandard,&FilInf);

Then you will have the last modified date+time in FilInf.ftLastWriteTime.

There are functions to convert this time to other formats.  i don't knwo what format you need it in--or this might be fine.

0
 

Author Comment

by:LG_Goundalkar
ID: 6276790
I got up to this point where I am getting Runtime error of..
here is my Code..
#define _WIN32_WINNT 0x0400
#include "windows.h"
#include <iostream.h>
#include <stdio.h>
 
int main(int argc, char *argv[])
{
  WIN32_FIND_DATA FindFileData;
  WIN32_FILE_ATTRIBUTE_DATA attri;
  HANDLE hFind;
  GET_FILEEX_INFO_LEVELS  GetFileExInfoStandard;
 
  printf ("Target file is %s.\n", argv[1]);
 
  hFind = FindFirstFile(argv[1], &FindFileData);
 
  if (hFind == INVALID_HANDLE_VALUE) {
    printf ("Invalid File Handle. Get Last Error reports %d\n", GetLastError ());
  } else {
        printf ("The first file found is %s\n", FindFileData.cFileName);

     GetFileAttributesEx(argv[1],GetFileExInfoStandard,&attri);
      printf("%s",attri.ftLastWriteTime);
      FindClose(hFind);
  }  
  return (0);
}

Run time Memory violation error.

Please Reply Soon..

L G Goundalkar.
0
 

Author Comment

by:LG_Goundalkar
ID: 6276817
Nietod Where are you..Please reply soon..
L G Goundalkar.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6276832
Soon?  I thought I was!  :-)

One problem is that you are trying to print the file time as a string.  its not.  its a number.

continues
0
 

Author Comment

by:LG_Goundalkar
ID: 6276867
How to convert it to String? Is there any function available..please tell me..
I tried %d it is giving -858993460.

BTW i tried to increase the points, but the max for a question is 300 :(
Thanks a lot for your interest and help..
waiting for the final solution from you..

L G G
0
 
LVL 22

Expert Comment

by:nietod
ID: 6276888
nt main(int argc, char *argv[])
{
    if (argc < 2)
   {
       cout << "USAGE: " << argv[0] << " FieName" << endl;
       return -1;
   }
   cout << "Target file is: " << argv[1] << endl;

   WIN32_FILE_ATTRIBUTE_DATA attri;

   GetFileAttributesEx(argv[1],GetFileExInfoStandard,&attri);

   SYSTEMTIME LasWrtTim;

   FileTimeToSystemTime(attri.ftLastWriteTime,&LasWrtTim)

   cout << "Last Write Date: "   << LasWrtTim.wYear << '/'
                                                 << LasWrtTim.wMonth <<  '/'
                                                 << LasWrtTim.wDay << end;


   return (0);
}
0
 
LVL 22

Expert Comment

by:nietod
ID: 6276904
I hope you don't think I'm taking too long to provide this info?  I had to look up a function or two there and type that example.   I can't do it instantly.

Note that if you had used C++ i/O (stram object), instead of printf() you never woudl have gotten that crash.   That is why C++ I/O is superior.  

>> the max for a question is 300 :(
For extreme cases, you can ask additional "dummy" questions for an expert to award him/her more points.  But in this case, 300 points is probalby more than enough--not that I would turn down more, I wouldn't want to insult your generosity.  :-)
0
 

Author Comment

by:LG_Goundalkar
ID: 6276966
I tried it.. but got this error..
Here is my Code..

#define _WIN32_WINNT 0x0400
#include "windows.h"
#include <iostream.h>
#include <stdio.h>
#include <time.h>
int main(int argc, char *argv[])
{
  WIN32_FIND_DATA FindFileData;
  WIN32_FILE_ATTRIBUTE_DATA attri;
  HANDLE hFind;
  GET_FILEEX_INFO_LEVELS GetFileExInfoStandard;
 
  printf ("Target file is %s.\n", argv[1]);
 
  hFind = FindFirstFile(argv[1], &FindFileData);
 
  if (hFind == INVALID_HANDLE_VALUE) {
    printf ("Invalid File Handle. Get Last Error reports %d\n", GetLastError ());
  } else {
    printf ("The first file found is %s\n", FindFileData.cFileName);
 GetFileAttributesEx(argv[1],GetFileExInfoStandard,&attri);
 SYSTEMTIME LasWrtTim;
 FileTimeToSystemTime(attri.ftLastWriteTime,&LasWrtTim);
 cout<<"Last Write Date: "<< LasWrtTim.wYear << '/'<< LasWrtTim.wMonth <<  '/'<<LasWrtTim.wDay <<endl;
 FindClose(hFind);
  }
  return (0);
}


ERROR:
 cannot convert from const struct_FILETIME to const struct FILETIME*

Reply soon..

Btw How much extra points you want.. i will give you the chance to define my Generosity..:)
PS : i have 3440 points..:)
 
0
 
LVL 22

Expert Comment

by:nietod
ID: 6276978
>> FileTimeToSystemTime(attri.ftLastWriteTime,&LasWrtTim);

should be

FileTimeToSystemTime(&attri.ftLastWriteTime,&LasWrtTim);
0
 
LVL 22

Expert Comment

by:nietod
ID: 6276992
Note that FindFirstFile and GetFileAttributes are two different ways to get this information.  They are used for different purposes.  You probalby don't need to be using both.  

If you want to find the data/time for a specific file (no wildcards like "*" and "?", then you probalby should be using only GetFileAttributes(), like in my example code.   If you will be using wildcards, then you probalby want to use only FindFirstFile()  (But then you probably want to be using FindNextFile() too.)

>> 3440 points..:)
3439?

Just kidding.  300 is more than enough.   This is probalby a 100 pointer.  Maybe 150.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:LG_Goundalkar
ID: 6277024
could able to run the program..
but here is the out put..

Target file is c:\yogesh\test\test.cpp.
The first file found is test.cpp
Last Write Date:52428/52428/52428Press any key to continue

Expected output is 26/6/01 10:37PM

Whats wrong with it now..

LGG
0
 
LVL 22

Expert Comment

by:nietod
ID: 6277043
e-mail me your code.

nietod@journey.com
0
 

Author Comment

by:LG_Goundalkar
ID: 6277083
just sent the code..
BTW whats your good name ...:)
reply soon..
its started to rain here and i got to go to home soon..:)

L G G
0
 
LVL 22

Expert Comment

by:nietod
ID: 6277160
Remove the line

 GET_FILEEX_INFO_LEVELS GetFileExInfoStandard;

GetFileExInfoStandard is an enum defined in windows.h.   That is what you want to pass.  here you define a variable with that name, and then pass that uninitialized variable.
0
 
LVL 86

Expert Comment

by:jkr
ID: 6278080
The API to use is 'GetFileTime()' (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/hh/sysmgmt/time_5r51.asp)- see e.g.

http://support.microsoft.com/support/kb/articles/Q128/1/26.asp

for a fully featured sample. NOTE that this API also works for directories, even though just files are mentioned.
0
 

Author Comment

by:LG_Goundalkar
ID: 6278173
Hi nietod..
Thanks for the answer..
But still there is problem..
its giving Date correctly..
but the time is given for the previous file..:)
hope you are not sleeping by now..

waiting for your reply..

LGG
0
 
LVL 22

Expert Comment

by:nietod
ID: 6278204
>> The API to use is 'GetFileTime()'
That's another option.  But it requires that the file be open.   Asi isaid originally  "one way is to use FindFirstFile() "  There are lots of ways dependig one hwat you have to work with and what exactly you need to do.  But using both FindFirstFile and GetFileAttributesEx() is probably not a good idea.

>> but the time is given for the previous file..:)
What do you mean?  what previous file?

You are only doing this for one file (or directory) right?
0
 

Author Comment

by:LG_Goundalkar
ID: 6278253
Hi nietod..
i want date & time for file in a perticular directory.

current code has given the date properly but time stamp of previous file present in that directory.

how to get the proper output..??

L G G
0
 

Author Comment

by:LG_Goundalkar
ID: 6278284
Here is the Code :

#define _WIN32_WINNT 0x0400
#include "windows.h"
#include <iostream.h>
#include <stdio.h>
#include <time.h>
int main(int argc, char *argv[])
{
 WIN32_FILE_ATTRIBUTE_DATA attri;
 HANDLE hFind;
 cout<<"Target file is:" <<argv[1]<<endl;
 SYSTEMTIME LasWrtTim;
 
 GetFileAttributesEx(argv[1],GetFileExInfoStandard,&attri);
 FileTimeToSystemTime(&attri.ftLastWriteTime,&LasWrtTim);
 
 cout<<"Last Write Date:\n"<<LasWrtTim.wYear<<'/'<<LasWrtTim.wMonth<<'/'<<LasWrtTim.wDay<<" ";
 cout<<LasWrtTim.wHour<<":"<<LasWrtTim.wMinute;
 FindClose(hFind);
  return (0);
}

0
 
LVL 22

Expert Comment

by:nietod
ID: 6278311
I just tested it.  it works fine for me.  It seems almost impossible that it would work as you say.

FYI you don't need hFind and FindClose().
0
 

Author Comment

by:LG_Goundalkar
ID: 6278348
Hey Nietod i got it..
It adds up the gtm time setting.. i.e +5.30 for Bombay, calcutta..in India..
Its taking GTM..
Is there any way to get the GTM settings..??

LGG
0
 
LVL 22

Expert Comment

by:nietod
ID: 6278387
You can use LocalTimeToLocalFileTime().
0
 

Author Comment

by:LG_Goundalkar
ID: 6278419
Hi nietod..
This function is not present..
where to find it.??
LGG
0
 
LVL 22

Accepted Solution

by:
nietod earned 300 total points
ID: 6278525
I've got an extra "local"  It is FileTimetoLocalFileTime().

If you go to any of the time functions (likeFileTimetoSystemTime())  in the VC help, and then choose "time functions" at the bottom you will get a list of many functions that you can apply depending one what you have to start with and what you want to end up with.
0
 

Author Comment

by:LG_Goundalkar
ID: 6279754
Thanks a lot for your timely help.
Hope to be associated more with you to enjoy the IT world.

L G Goundalkar.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 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

20 Experts available now in Live!

Get 1:1 Help Now