Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

Directory Date

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
LG_Goundalkar
Asked:
LG_Goundalkar
  • 15
  • 14
1 Solution
 
nietodCommented:
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
 
nietodCommented:
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
 
LG_GoundalkarAuthor Commented:
Hi nietod..
I am using Windows NT.
So you can consider OS as Windows XX :)
reply soon
Thanks.
L G Goundalkar.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
nietodCommented:
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
 
LG_GoundalkarAuthor Commented:
Hi nietod..
I am using Windows NT.
So you can consider OS as Windows XX :)
reply soon
Thanks.
L G Goundalkar.
0
 
nietodCommented:
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
 
LG_GoundalkarAuthor Commented:
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
 
LG_GoundalkarAuthor Commented:
Nietod Where are you..Please reply soon..
L G Goundalkar.
0
 
nietodCommented:
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
 
LG_GoundalkarAuthor Commented:
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
 
nietodCommented:
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
 
nietodCommented:
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
 
LG_GoundalkarAuthor Commented:
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
 
nietodCommented:
>> FileTimeToSystemTime(attri.ftLastWriteTime,&LasWrtTim);

should be

FileTimeToSystemTime(&attri.ftLastWriteTime,&LasWrtTim);
0
 
nietodCommented:
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
 
LG_GoundalkarAuthor Commented:
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
 
nietodCommented:
e-mail me your code.

nietod@journey.com
0
 
LG_GoundalkarAuthor Commented:
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
 
nietodCommented:
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
 
jkrCommented:
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
 
LG_GoundalkarAuthor Commented:
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
 
nietodCommented:
>> 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
 
LG_GoundalkarAuthor Commented:
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
 
LG_GoundalkarAuthor Commented:
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
 
nietodCommented:
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
 
LG_GoundalkarAuthor Commented:
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
 
nietodCommented:
You can use LocalTimeToLocalFileTime().
0
 
LG_GoundalkarAuthor Commented:
Hi nietod..
This function is not present..
where to find it.??
LGG
0
 
nietodCommented:
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
 
LG_GoundalkarAuthor Commented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 15
  • 14
Tackle projects and never again get stuck behind a technical roadblock.
Join Now