herrDeng網內搜尋

自訂搜尋

Ads

2022年11月9日 星期三

C++17 filesystem整理資料夾檔案實作

 
 C++程式碼如下:
  1. #include <iostream>
  2. #include <filesystem>
  3. #include <string>
  4. using namespace std;
  5. using namespace std::filesystem;
  6. int main()
  7. try
  8. {
  9. cout << "使用filesystem要先調專案屬性到C++17!!\n";
  10. cout << "如果不知本專案所在path就試一下下面:\n";
  11. path basePath = current_path();
  12. cout <<basePath << endl;
  13.  
  14. if (!exists(path("images"))) {
  15. create_directory("images");
  16. cout << "mkdir images\n";
  17. }
  18. int i = 0;
  19. for (const auto& entry : recursive_directory_iterator("D:\\cpp\\test_cv_dnn"))
  20. {
  21. if (entry.is_regular_file())
  22. {
  23. path ff = entry.path();
  24. string fn = entry.path().string();
  25. int pos = -1;
  26. if ((pos = fn.rfind(".jpg")) == fn.length() - 4) {
  27. cout << "pos=" << pos << "\t" << fn << endl;
  28. string toFile ="images\\"+ to_string(i) + ".jpg";
  29. copy_file(ff, path(toFile), copy_options::overwrite_existing);
  30. cout << "已經copy圖檔" << fn << "到" << toFile << endl;
  31. i++;
  32. }
  33. }
  34. }
  35. return 0;
  36. }
  37. catch (exception& e) {
  38. cerr << e.what();
  39. return 1;
  40. }

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章