main.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <cstdio>
  2. #include "filefunctions.h"
  3. #include "debugging.h"
  4. #include <windows.h>
  5. #include <sstream>
  6. #include <string>
  7. #include <iomanip>
  8. using namespace filefunctions;
  9. int main(int argc, char **argv)
  10. {
  11. printf("Yo dude!\n");
  12. DEBUG(std::hex << INVALID_FILE_ATTRIBUTES << std::endl << argv[0]);
  13. cd();
  14. /*
  15. if(cd("testfiler", CF_RELATIVE))
  16. printf("FAIL!\n");
  17. else
  18. printf("SUCCESS! Current dir: %s\n", pwd().c_str());
  19. */
  20. std::queue<File_Struct> files;
  21. ls("", files, CF_RECURSIVE | CF_RELATIVE);
  22. std::cout << "Klar med listning av filer. Flyttar nu enl. önskemål" << std::endl;
  23. while(files.size())
  24. {
  25. File_Struct temp = files.front();
  26. DEBUG("Fil: " << temp.path);
  27. if(temp.file_ending == "exe")
  28. {
  29. if(temp.modified)
  30. delete temp.modified;
  31. files.pop();
  32. continue;
  33. }
  34. //DEBUG("Modified: " << asctime(temp.modified));
  35. /*
  36. if(temp.taken)
  37. DEBUG("Tagen: " << asctime(temp.taken));
  38. */
  39. INFO("Moving file: \"" << temp.path << "\"");
  40. std::ostringstream new_path;
  41. new_path << std::dec << pwd() << "\\sorterat\\";
  42. if(temp.taken)
  43. {
  44. INFO("Moving based on date taken");
  45. new_path << (temp.taken->tm_year+1900) << "\\" << std::setfill('0') << std::setw(2) << (temp.taken->tm_mon+1) << "\\" << (temp.taken->tm_year+1900) << "-" << std::setw(2) << (temp.taken->tm_mon+1) << "-" \
  46. << std::setw(2) << temp.taken->tm_mday << "_" << std::setw(2) << temp.taken->tm_hour << "'" << std::setw(2) << temp.taken->tm_min << "'" << std::setw(2) << temp.taken->tm_sec;
  47. }
  48. else if(temp.modified)
  49. {
  50. INFO("Moving based on date modified");
  51. new_path << (temp.modified->tm_year+1900) << "\\" << std::setfill('0') << std::setw(2) << (temp.modified->tm_mon+1) << "\\" << (temp.modified->tm_year+1900) << "-" << std::setw(2) << (temp.modified->tm_mon+1) << "-" \
  52. << std::setw(2) << temp.modified->tm_mday << "_" << std::setw(2) << temp.modified->tm_hour << "'" << std::setw(2) << temp.modified->tm_min << "'" << std::setw(2) << temp.modified->tm_sec;
  53. }
  54. else
  55. {
  56. DEBUG("No date found");
  57. new_path << "osorterat\\" << temp.path.substr(temp.path.find_last_of('/\\')+1, temp.path.find_last_of('.'));
  58. }
  59. std::ostringstream path_postfix;
  60. path_postfix << new_path.str() << "." << temp.file_ending;
  61. if(mv(temp.path, path_postfix.str(), CF_NONE) == SC_ALREADY_EXISTS)
  62. {
  63. int postfix = 1;
  64. do
  65. {
  66. path_postfix.str("");
  67. path_postfix << new_path.str() << "(" << postfix++ << ")" << "." << temp.file_ending;
  68. } while(mv(temp.path, path_postfix.str()) == SC_ALREADY_EXISTS);
  69. }
  70. INFO("New filename: " << path_postfix.str() << "\n\n");
  71. // Free memory
  72. if(temp.taken)
  73. delete temp.taken;
  74. if(temp.modified)
  75. delete temp.modified;
  76. files.pop();
  77. }
  78. printf("Klart! :D\n");
  79. char c;
  80. std::cin >> c;
  81. return 0;
  82. }