{"id":3360,"date":"2020-07-28T06:58:18","date_gmt":"2020-07-28T06:58:18","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=3360"},"modified":"2020-07-28T06:59:10","modified_gmt":"2020-07-28T06:59:10","slug":"search-files-in-a-directory","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/file-input-and-output\/working-with-directories\/search-files-in-a-directory","title":{"rendered":"Search files in a directory"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Find the oldest or newest DateTime filename in a directory<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tDIR *directory1;\n\tstruct dirent *entry1;\n\tstring filename;\n\tstring filename_name;\n\tstring filename_extension;\n\tuint64_t this_file_value;\n\tuint64_t selected_file_value;\n\tstring selected_file_name;\n\tstringstream ss1;\n\t\n\t\/\/This function looks for the oldest or newest filename, our filenames that are created using YYYYMMDDHHMMSS.extension format so it simply\n\t\/\/converts their names to numeric and does a compare\n\n\tprintf(\"Starting work through directory for next livedata file...\\n\");\n\t\/\/selected_file_value = 0xffffffffffff;\t\t\t\t\/\/&lt;Use for oldest file\n\tselected_file_value = 0;\t\t\t\t\t\/\/&lt;Use for newest file\n\tdirectory1 = opendir(\"\/home\/pi\/myfolder\");\t\t\t\/\/Open a directory stream\n\tif (directory1 != NULL)\n\t{\n\t\twhile (entry1 = readdir(directory1))\n\t\t{\n\t\t\tif (entry1->d_type == DT_REG)\t\t\t\/\/DT_REG=regular file, DT_DIR=directory\n\t\t\t{\n\t\t\t\tprintf(\"File: %s\\n\", entry1->d_name);\n\t\t\t\tfilename = entry1->d_name;\n\t\t\t\tsize_t dot = filename.find_last_of(\".\");\n\t\t\t\tif (dot != std::string::npos)\n\t\t\t\t{\n\t\t\t\t\tfilename_name = filename.substr(0, dot);\n\t\t\t\t\tfilename_extension  = filename.substr(dot, filename.size() - dot);\n\n\t\t\t\t\tif (filename_extension.compare(\".txt\") == 0)\t\t\/\/0=strings are the same\n\t\t\t\t\t{\n\t\t\t\t\t\t\/\/We have: \"2015092110\" &amp; \".extension\"\n\t\t\t\t\t\t\/\/We want: 2015092110\n\n\t\t\t\t\t\tprintf(\"File: %s\\n\", filename_name.c_str());\n\n\t\t\t\t\t\tss1.clear();\n\t\t\t\t\t\tss1.str(std::string());\n\t\t\t\t\t\tss1 &lt;&lt; filename_name;\n\t\t\t\t\t\tss1 >> this_file_value;\n\t\t\t\t\t\tif ((this_file_value > 0) &amp;&amp; (this_file_value > selected_file_value))\t\t\t\/\/&lt;&lt;&lt;Oldest or newest check here\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\/\/This file is older\/newer than any previously found\n\t\t\t\t\t\t\tselected_file_value = this_file_value;\n\t\t\t\t\t\t\tselected_file_name = filename_name;\n\t\t\t\t\t\t\tprintf(\"New selected file: %s\\n\", selected_file_name.c_str());\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t}\n\t}\n\tclosedir(directory1);\n\n\tif ((selected_file_value == 0xffffffffffff) || (selected_file_value == 0))\n\t{\n\t\t\/\/-------------------------\n\t\t\/\/----- NO FILE FOUND -----\n\t\t\/\/-------------------------\n\t\tprintf(\"No valid filename found - exiting\\n\");\n\t\treturn(0);\n\t}\n\t\n\t\/\/We have:\n\t\/\/\tselected_file_name\t\t\t\/\/Filename without the extension\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Find the oldest or newest DateTime filename in a directory<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[159],"tags":[],"class_list":["post-3360","post","type-post","status-publish","format-standard","hentry","category-working-with-directories"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/3360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=3360"}],"version-history":[{"count":2,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/3360\/revisions"}],"predecessor-version":[{"id":3362,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/3360\/revisions\/3362"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=3360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=3360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=3360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}