andy
andy

Reputation: 35

Sort files according to creation date?

First Problem is solved. Files is perfectly arrange.

for (int i = 0; i < allFiles.GetSize(); i++)
{
    box1.AddString(allFiles[i]);
}

vector<file> files;

vector<tstring> vec;
vec.insert(vec.begin(), &allFiles[0], &allFiles[allFiles.GetSize() - 1] + 1);

transform(vec.begin(), vec.end(),back_inserter(files),[](wstring const &fname)
{
    WIN32_FIND_DATA d;
    HANDLE h = FindFirstFile(fname.c_str(), &d);
    FindClose(h);
    return d;
}

);

sort(files.begin(), files.end());
CStringArray sortFiles;
files.insert(files.begin(), &sortFiles[0], &sortFiles[sortFiles.GetSize() - 1] + 1);

Now The problem is how i save sort files in CStringArray. Last Statement show the error see the comments?

Upvotes: 3

Views: 6804

Answers (2)

akram Halder
akram Halder

Reputation: 9

Simple way to sort file based on creation date or last write date is store the files in to a map where Key is the date and value is the file name. Map will sort it for you.. my code example as below..

BOOL GetLastWriteTime(FILETIME ftWrite, LPWSTR lpszString, DWORD dwSize)
{
     DWORD dwRet;
     SYSTEMTIME st, stLocal;
    // Convert the last-write time to local time.
    FileTimeToSystemTime(&ftWrite, &st);
    SystemTimeToTzSpecificLocalTime(NULL, &st, &stLocal);

    // Build a string showing the date and time.
    dwRet = StringCchPrintf(lpszString, dwSize, 
        TEXT("%02d%02d%d%02d%02d%02d%04d"),
        stLocal.wMonth, stLocal.wDay, stLocal.wYear,
        stLocal.wHour, stLocal.wMinute, stLocal.wSecond, stLocal.wMilliseconds);

    if( S_OK == dwRet )
        return TRUE;
    else return FALSE;
}


int _tmain(int argc, _TCHAR* argv[])
{
    std::map<wstring, wstring> first;

     wstring directory = L"D:\\SourceCode\\FilesTemp\\*";
     std::wstring name;
     WCHAR szBuf[MAX_PATH];
     WIN32_FIND_DATA d;
     HANDLE hFindFile   = FindFirstFile ( directory.c_str(), &d );

     if (hFindFile == INVALID_HANDLE_VALUE) 
    {
        DWORD dwLastError = ::GetLastError();

        if( dwLastError != ERROR_NO_MORE_FILES &&
            dwLastError != ERROR_FILE_NOT_FOUND)
        {
            return FALSE;
        }

        // No files found
        return TRUE;
    }
     do
     {
         if( d.cFileName[0] &&  _tcscmp( d.cFileName, _T( "." )) != 0 &&_tcscmp( d.cFileName, _T( ".." )) != 0 )
         {
             name = d.cFileName;
             if(GetLastWriteTime( d.ftLastWriteTime, szBuf, MAX_PATH ))
             {
                _tprintf(TEXT("Last write time is: %s\n"), szBuf);
                first[szBuf] = name;
             }
         }
     }while (FindNextFile ( hFindFile, &d ));

    return 0;
}

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490278

I would:

  1. Create a type to hold a file name and timestamp.
  2. Use FindFirstFile to get the timestamp you care about for each file.
  3. Create a name/timestamp object and store it into a vector.
  4. Call FindClose for that file.
  5. Repeat from 2 for each input file name.
  6. Sort the vector on the timestamp.
  7. Display the results.

So, one possible implementation might look something like this:

#include <iostream>
#include <Windows.h>
#include <algorithm>
#include <string>
#include <vector>
#include <iomanip>

class file {
    std::string name;
    FILETIME time;
public:
    bool operator<(file const &other) const { 
        return CompareFileTime(&time, &other.time) == 1;
    }

    friend std::ostream &operator<<(std::ostream &os, file const &f) {
        SYSTEMTIME st;
        FileTimeToSystemTime(&f.time, &st);
        return os << std::setw(20) << f.name << "\t" << st.wHour << ":" << st.wMinute << ":" << st.wSecond << " " << st.wYear << "/" << st.wMonth << "/" << st.wDay;
    }

    file(WIN32_FIND_DATA const &d) : name(d.cFileName), time(d.ftCreationTime) {}
};

int main(){ 
    std::vector<std::string> inputs{ "a.txt", "b.txt" };

    std::vector<file> files;

    std::transform(inputs.begin(), inputs.end(), 
        std::back_inserter(files),
        [](std::string const &fname) {
            WIN32_FIND_DATA d;
            HANDLE h = FindFirstFile(fname.c_str(), &d);
            FindClose(h);
            return d;
        }
    );

    std::sort(files.begin(), files.end());
    std::copy(files.begin(),files.end(),
        std::ostream_iterator<file>(std::cout, "\n"));
}

To deal with "wide" (Unicode) strings for the file names, you'd modify that (slightly) to look something like this:

#include <iostream>
#include <Windows.h>
#include <algorithm>
#include <string>
#include <vector>
#include <iomanip>

class file {
    std::wstring name;
    FILETIME time;
public:
    bool operator<(file const &other) const { 
        return CompareFileTime(&time, &other.time) == 1;
    }

    friend std::wostream &operator<<(std::wostream &os, file const &f) {
        SYSTEMTIME st;
        FileTimeToSystemTime(&f.time, &st);
        return os << std::setw(20) << f.name << L"\t" << st.wHour << L":" << st.wMinute << L":" << st.wSecond << L" " << st.wYear << L"/" << st.wMonth << L"/" << st.wDay;
    }

    file(WIN32_FIND_DATA const &d) : name(d.cFileName), time(d.ftCreationTime) {}
};

int main(){ 
    std::vector<std::wstring> inputs{ L"a.txt", L"b.txt" };

    std::vector<file> files;

    std::transform(inputs.begin(), inputs.end(), 
        std::back_inserter(files),
        [](std::wstring const &fname) {
            WIN32_FIND_DATA d;
            HANDLE h = FindFirstFile(fname.c_str(), &d);
            FindClose(h);
            return d;
        }
    );

    std::sort(files.begin(), files.end());
    for (auto const &f : files)
        std::wcout << f << L"\n";
}

Then when you build it you need to tell the compiler you want the Unicode versions of the Windows functions by defining UNICODE when you compile:

cl -DUNICODE files.cpp

Upvotes: 5

Related Questions