Mr. Starck
Mr. Starck

Reputation: 232

Convert FILETIME to string, or else

I want to store the filename and the creation time of every file present in a directory. I found the following code which browses a directory and detects every file. The problem is that I don't know how to store the values from WIN32_FIND_DATA. cFilename shouldn't be too hard, it's TCHAR (but I'm beginner in C++), and ftCreationTime is a structure so it can't be stored into a vector because it has no constructor.

The final goal of this piece of code is to detect if a new file with the same has been created in the directory. Some pictures are regularly created and deleted by a software, and regarding if the file is new or not it sends an alert to a pager. So I have to find a way to check if the file is new or not, otherwise the pager will always ring :p

std::map<std::string, std::string> pictures;

HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA ffd;

hFind = FindFirstFile(TEXT("C:\\temp\\*"), &ffd);
do
{
    Sleep(1000);
    bool isDirectory = ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
    if(isDirectory)
    {
        _tprintf(TEXT("%s\n"),ffd.cFileName);

    }
    else
    {
        //here is where I want to store the cFilename and ftCreationTime in the map
        //something strange here
        //tprintf returns the good filename but cout returns a character string like 0019FB2C for every file found
        _tprintf(TEXT("%s\n"),ffd.cFileName );
        std::cout << "FileTime: " << ffd.cFileName << std::endl;
    }
}while(FindNextFile(hFind, &ffd) != 0);
FindClose(hFind);

Upvotes: 2

Views: 6953

Answers (1)

jliv902
jliv902

Reputation: 1658

Here's a quick example of how you could do for this.
I used a vector, but you can adjust this to your needs.
Please note that this hasn't been tested or debugged.
I also did this all in one .cpp file; you should probably break this up.

First, create a Picture struct:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#include <tchar.h>
#include <Windows.h>

typedef std::basic_string <TCHAR> tstring ;
typedef std::basic_ostream <TCHAR> tstream ;

#ifdef _UNICODE
#define tcout std::wcout
#else
#define tcout std::cout
#endif

struct Picture
{
    Picture (const tstring &name, const FILETIME &ft) ;

    tstring name ;
    FILETIME creation_time ;

    friend bool operator== (const Picture &lhs, const Picture &rhs) ;
    friend bool operator!= (const Picture &lhs, const Picture &rhs) ;
    friend tstream& operator<< (tstream& ts, const Picture &pic) ;
};

Picture::Picture (const tstring &name, const FILETIME &ft) : name (name), creation_time (ft)
{
}

bool operator== (const Picture &lhs, const Picture &rhs)
{
    return ((lhs.name == rhs.name) && (::CompareFileTime (&lhs.creation_time, &rhs.creation_time) == 0)) ;
}

bool operator!= (const Picture &lhs, const Picture &rhs)
{
    return !(operator== (lhs, rhs)) ;
}

tstream& operator<< (tstream& ts, const Picture &pic)
{
    ts  << pic.name << _T (", FileTime (HI, LO): (") 
        << pic.creation_time.dwHighDateTime << _T (", ")
        << pic.creation_time.dwLowDateTime << _T (")") ;
    return ts ;
}

Then implement a function that prints out new files.

void PrintNewPictures (std::vector <Picture> &vecPicsOld, const tstring &dir)
{
    HANDLE hFind = INVALID_HANDLE_VALUE;
    WIN32_FIND_DATA ffd ;

    std::vector <Picture> vecPics ;

    hFind = FindFirstFile(dir.data (), &ffd) ;
    if (hFind == INVALID_HANDLE_VALUE) {
        // Return an error or throw an exception
        return ;
    }

    do {
        Picture pic (ffd.cFileName, ffd.ftCreationTime) ;
        if (std::find (vecPicsOld.begin (), vecPicsOld.end (), pic) == vecPicsOld.end ()) {
            // Print that this is a new Picture.
            tcout << pic << std::endl ;
        }

        vecPics.push_back (pic) ;

    } while (::FindNextFile (hFind, &ffd) != NULL) ;
    ::FindClose (hFind) ;

    // This keeps the vector fresh so it won't build up old values.
    std::swap (vecPics, vecPicsOld) ;
}

Here's and example of how you would use this:

int main (void) 
{
    std::vector <Picture> vecPics ;
    while (1) {
        ::Sleep (1000) ;
        PrintNewPictures (vecPics, _T ("C:\\temp\\*")) ;
    }

    return 0 ;
}

Upvotes: 1

Related Questions