1. int main() {
    2. //testing program
    3. //string timenow = getDateTime();
    4. //string timenow = getDate();
    5. string timenow = getMonth();
    6. cout << "time now is" << timenow << endl;
    7. //testWriteAtEnd("testINDSDFDSFSG");
    8. //testWriteAtEnd(timenow);
    9. //createDirectory("testagain\\AGAIN");
    10. //createDirectory(string(timenow));
    11. //createDirectoryHierachy(timenow, '_');
    12. //createDirectoryHierachy("2017_08_07", '_');
    13. createDirectoryHierachy("2017/08/09", '//');
    14. //Test(timenow);
    15. //Test("asdf\\efgh\\zxcv");
    16. cout << "done" << endl;
    17. getchar();
    18. return 0;
    19. }
    20. void testWriteAtEnd(string str) {
    21. cout << "trying to append at end of file" << endl;
    22. ofstream file;
    23. /*
    24. * std::ios_base::app: Locate cursor to EOF when writing. If path not exist, create it.
    25. */
    26. file.open("file.txt", std::ios_base::app); //open a file
    27. file << str << endl; //write to it
    28. file.close(); //close it
    29. cout << "appended" << endl;
    30. }
    31. void createDirectory(string dir_name) {
    32. cout << "trying to create folder" << endl;
    33. /*
    34. * access()/_access(): return 0 if exist folder in pwd and -1 if not
    35. * mkdir()/_mkdir(): return 0 if success and -1 if not
    36. */
    37. #if defined(_WIN32)
    38. if (_access(dir_name.c_str(), 0) == -1) {
    39. cout << dir_name << " not existing" << endl;
    40. if (_mkdir(dir_name.c_str()) == 0) {
    41. cout << dir_name << " existing now" << endl;
    42. }
    43. else {
    44. cout << dir_name << " cannot be created" << endl;
    45. }
    46. }
    47. else {
    48. cout << dir_name << " already existed. No need to create" << endl;
    49. }
    50. #else
    51. if (access(dir_name.c_str(), 0) == -1) {
    52. cout << dir_name << " not existing" << endl;
    53. if (mkdir(dir_name.c_str(), 0777) == 0) {
    54. cout << dir_name << " existing now" << endl;
    55. }
    56. else {
    57. cout << dir_name << " cannot be created" << endl;
    58. }
    59. }
    60. else {
    61. cout << dir_name << " already existed. No need to create" << endl;
    62. }
    63. #endif
    64. }
    65. void createDirectoryHierachy(string dir_name, char splitter) {
    66. char delimiter;
    67. #if defined(_WIN32)
    68. delimiter = '\\';
    69. #else
    70. delimiter = '/';
    71. #endif
    72. cout << "trying to create folder by hierachy" << endl;
    73. std::string folder_builder;
    74. std::string sub;
    75. sub.reserve(dir_name.size());
    76. for (auto it = dir_name.begin(); it != dir_name.end(); ++it)
    77. {
    78. //cout << *(folder.end()-1) << endl;
    79. const char c = *it;
    80. sub.push_back(c);
    81. if (c == splitter) {
    82. sub.pop_back();
    83. sub.push_back(delimiter); //append / at end of file name, program will create and get into this folder
    84. }
    85. if (c == splitter || it == dir_name.end() - 1) {
    86. folder_builder.append(sub);
    87. createDirectory(folder_builder);
    88. sub.clear();
    89. }
    90. //if (c == '_')
    91. //{
    92. // sub.push_back('\\');
    93. // folder_builder.append(sub);
    94. // createDirectory(folder_builder);
    95. // sub.clear();
    96. //}
    97. //else if (it == dir_name.end() - 1) {
    98. // //cout << "here" << endl;
    99. // sub.push_back(c);
    100. // folder_builder.append(sub);
    101. // createDirectory(folder_builder);
    102. // sub.clear();
    103. //}
    104. //else
    105. //{
    106. // sub.push_back(c);
    107. //}
    108. }
    109. }
    110. string getDateTime() {
    111. char timenow[20];
    112. time_t now = time(NULL);
    113. #if defined(_WIN32)
    114. struct tm p;
    115. localtime_s(&p, &now);
    116. strftime(timenow, 20, "%Y_%m_%d_%H%M%S", &p);
    117. #else
    118. struct tm *p;
    119. p = localtime(&now);
    120. strftime(timenow, 20, "%Y_%m_%d_%H%M%S", p);
    121. #endif
    122. return string(timenow);
    123. }
    124. string getDate() {
    125. char timenow[20];
    126. time_t now = time(NULL);
    127. #if defined(_WIN32)
    128. struct tm p;
    129. localtime_s(&p, &now);
    130. strftime(timenow, 20, "%Y_%m_%d", &p);
    131. #else
    132. struct tm *p;
    133. p = localtime(&now);
    134. strftime(timenow, 20, "%Y_%m_%d", p);
    135. #endif
    136. return string(timenow);
    137. }
    138. string getMonth() {
    139. char timenow[20];
    140. time_t now = time(NULL);
    141. #if defined(_WIN32)
    142. struct tm p;
    143. localtime_s(&p, &now);
    144. strftime(timenow, 20, "%Y_%m", &p);
    145. #else
    146. struct tm *p;
    147. p = localtime(&now);
    148. strftime(timenow, 20, "%Y_%m", p);
    149. #endif
    150. return string(timenow);
    151. }