sa***********@g mail.com wrote:
Hi..
I am stuck with a problem where I need to extract some information from
a File in C++.e.g File size,date of creation, Location on the Disk etc.
I haven't been able to find any particular solution yet. Could anybody
amongst you help me in this regard. I'll be glad.
Regards;
Saleem
Try recls (
http://synesis.com.au/software/recls/). Its a recursive sile
sytem search library, cross-platform. You can set up a search and read
off the matches one at a time, using Iterator-like object, or using STL
container-like object.
To getting the details of a single file you write something like the
following:
#include <recls/cpp/filesearch.hpp>
int main(int argc, char **argv)
{
if(argc < 2)
{
return EXIT_FAILURE;
}
using namespace recls::cpp;
using namespace std;
FileEntry entry = FileSearch::Sta t(argv[1]);
It gives you full information on path and components, including
absolute, and relative to the search directory.
cout << "full path: " << entry.GetPath() << endl;
cout << "search-relative path: " << entry.GetSearch RelativePath()
<< endl;
cout << "location: " << entry.GetDirect oryPath() << endl;
#ifdef RECLS_PLATFORM_ API_WIN32
cout << "drive: " << entry.GetDrive( ) << endl;
#endif
cout << "directory: " << entry.GetDirect ory() << endl;
cout << "file name+extension: " << entry.GetFile() << endl;
cout << "file name: " << entry.GetFileNa me() << endl;
/// and so on
One problem is that the size type is platform specific. So:
if(!entry.IsDir ectory())
{
#ifdef RECLS_PLATFORM_ API_WIN32
// recls_filesize_ t is ULARGE_INTEGER
cout << "size: " << entry.GetSize() .LowPart << endl;
#else
// recls_filesize_ t is off_t
cout << "size: " << entry.GetSize() << endl;
#endif
The same problem (but) worse!) is that the time type is platform
specific. So:
#ifdef RECLS_PLATFORM_ API_WIN32
// recls_time_t is FILETIME
// you have to convert using FileTimeToSyste mTime
FILETIME t1 = entry.GetCreati onTime();
FILETIME t2 = entry.GetModifi cationTime();
FILETIME t3 = entry.GetLastAc cessTime();
FILETIME t4 = entry.GetLastSt atusChangeTime( );
#else
// recls_time_t is time_t
// you have to convert using gmtime
time_t t1 = entry.GetCreati onTime();
time_t t2 = entry.GetModifi cationTime();
time_t t3 = entry.GetLastAc cessTime();
time_t t4 = entry.GetLastSt atusChangeTime( );
#endif
If you have a cross-platform time class you can just convert to your
time class and not worry about it. Date/time is such a drag. recls
seems to just ignore the problema dn leave it to the user.
If you can get round the time/size hasslles, it's otherwise good. I use
it for recusrive searches whenever I need it.