1. 程式人生 > 其它 >win32 - 按檔案的建立日期排序

win32 - 按檔案的建立日期排序

因專案中使用檔案的建立日期來命名檔案,所以不用額外查詢檔案的建立日期再進行排序,記錄一下

bool AscendingSortByCreationTime(const std::wstring& file_a,
                                 const std::wstring& file_b) {
  int n_time_less = CompareFileTime(&FtCreatetionTime(file_a),
                                    &FtCreatetionTime(file_b));
  
if (n_time_less < 0) return true; else return false; } FILETIME FtCreatetionTime(const std::wstring& file) { WIN32_FIND_DATA wfd; FILETIME time_create = {}; HANDLE hFind = FindFirstFile(file.c_str(), &wfd); if (hFind == INVALID_HANDLE_VALUE) { std::cout << "the handle of file is invalid
"; return time_create; } time_create = wfd.ftCreationTime; FindClose(hFind); return time_create; } ... // 呼叫地方,files 是 std::vector<std::wstring> 定義,裡面是需要排序的檔案路徑 std::sort(files.begin(), files.end(), AscendingSortByCreationTime);