{"id":196,"date":"2010-04-28T18:38:42","date_gmt":"2010-04-28T18:38:42","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=196"},"modified":"2022-02-17T06:24:05","modified_gmt":"2022-02-17T06:24:05","slug":"working-with-directories","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/file-input-output-serialization\/working-with-directories","title":{"rendered":"Working With Directories"},"content":{"rendered":"<pre>\r\n<code>\r\nusing namespace System::IO;\r\n<\/code><\/pre>\n<h4>\nDoes directory exist?<br \/>\n<\/h4>\n<p>\n(Note paths can include a filename if you wish)\n<\/p>\n<pre>\r\n<code>\r\n\tif (!Directory::Exists(Path::GetDirectoryName(MyFileName)))\r\n\t\/\/or\r\n\tString ^path;\r\n\tpath = &quot;C:\\\\Atest\\\\&quot;;\t\t\t\t\/\/\t\t&#39;\\\\&#39;=&#39;\\&#39;\r\n\tif (!Directory::Exists(path))\r\n\t\/\/or\r\n\tif (!Directory::Exists(&quot;C:\\\\Atest\\\\&quot;))\t\/\/\t\t&#39;\\\\&#39;=&#39;\\&#39;\r\n<\/code><\/pre>\n<h4>\nCreate Directory<br \/>\n<\/h4>\n<p>\n(Note paths can include a filename if you wish)\n<\/p>\n<pre>\r\n<code>\r\n\tDirectory::CreateDirectory(Path::GetDirectoryName(MyFileName));\r\n\t\/\/or\r\n\tString ^path;\r\n\tpath = &quot;C:\\\\Atest\\\\&quot;;\t\t\t\t\/\/\t\t&#39;\\\\&#39;=&#39;\\&#39;\r\n\tDirectory::CreateDirectory(path);\r\n\t\/\/or\r\n\tDirectory::CreateDirectory(&quot;C:\\\\Atest\\\\&quot;);\t\/\/\t\t&#39;\\\\&#39;=&#39;\\&#39;\r\n<\/code><\/pre>\n<h4>\nDelete Directory<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\t\/\/----- DELETE FOLDER IF IT ALREADY EXISTS -----\r\n\tif (Directory::Exists(&quot;C:\\\\myfolder\\\\&quot;))\r\n\t{\r\n\t\ttry\r\n\t\t{\r\n\t\t\tarray&lt;String^&gt; ^files = Directory::GetFiles(&quot;C:\\\\myfolder\\\\&quot;);\r\n\t\t\tfor each (String ^sTemp in files)\r\n\t\t\t{\r\n\t\t\t\ttry\r\n\t\t\t\t{\r\n\t\t\t\t\tif (File::Exists(sTemp))\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\t\/\/If file has read only attribute set clear it so that delete can occur\r\n\t\t\t\t\t\tif ((File::GetAttributes(sTemp) &amp; FileAttributes::ReadOnly) == FileAttributes::ReadOnly)\r\n\t\t\t\t\t\t\tFile::SetAttributes(sTemp, FileAttributes::Normal);\r\n\r\n\t\t\t\t\t\tFile::Delete(sTemp);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\tcatch (IOException ^)\r\n\t\t\t\t{\r\n\t\t\t\t\t\/\/Can&#39;t delete file\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tDirectory::Delete(&quot;C:\\\\myfolder\\\\&quot;, true);\t\/\/including true will cause subdirectories to be deleted\r\n\r\n\t\t}\r\n\t\tcatch (IOException ^)\r\n\t\t{\r\n\t\t\t\/\/Can&#39;t delete folder\r\n\t\t}\r\n\t}\r\n<\/code><\/pre>\n<h4>\nMove Up 1 Subdirectory (when a directory no longer exists)<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\t\/\/If last directory is not valid then try directory before it\r\n\tif (!Directory::Exists(Path::GetDirectoryName(LastFileDirectory)))\r\n\t{\r\n\t\tif (LastFileDirectory-&gt;LastIndexOf(&quot;\\\\&quot;) &gt;= 0)\r\n\t\t\tLastFileDirectory = LastFileDirectory-&gt;Substring(0, (LastFileDirectory-&gt;LastIndexOf(&quot;\\\\&quot;)));\t\t\/\/Delete trailing &#39;\\&#39;\r\n\t\tif (LastFileDirectory-&gt;LastIndexOf(&quot;\\\\&quot;) &gt;= 0)\r\n\t\t\tLastFileDirectory = LastFileDirectory-&gt;Substring(0, (LastFileDirectory-&gt;LastIndexOf(&quot;\\\\&quot;) + 1));\t\/\/Delete directory name up to next &#39;\\&#39;\r\n\t}\r\n<\/code><\/pre>\n<h4>\nGet Directory Attributes<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\t txtOutput-&gt;Text = &quot;Name: &quot; + dir-&gt;FullName + &quot;\\r\\n&quot;;\r\n\t txtOutput-&gt;Text += &quot;Created: &quot; + dir-&gt;CreationTime.ToShortDateString() +  &quot; &quot; + dir-&gt;CreationTime.ToLongTimeString();\r\n<\/code><\/pre>\n<h4>\nGet List Of Files In A Directory<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tString ^sTemp;\r\n\tarray&lt;String^&gt; ^files = Directory::GetFiles(SelectedDirectory);\r\n\tfor each(sTemp in files)\r\n\t{\r\n\t\tsTemp = sTemp-&gt;Substring(SelectedDirectory-&gt;Length + 1);\r\n\t\ttxtFilesToConvert-&gt;Text += sTemp + &quot;\\r\\n&quot;;\r\n\t}\r\n<\/code><\/pre>\n<h4>\nGet List Of SubDirectories In A Directory<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tString ^sTemp;\r\n\tarray&lt;string^&gt; ^subDirs = Directory::GetDirectories(SelectedDirectory);\r\n\tfor each(sTemp in subDirs)\r\n\t{\r\n\t\tsTemp = sTemp-&gt;Substring(SelectedDirectory-&gt;Length + 1);\r\n\t\ttxtFilesToConvert-&gt;Text += sTemp + &quot;\\r\\n&quot;;\r\n\t}\r\n<\/code><\/pre>\n<h4>\nDelete Old Files In A Directory<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tDateTime DeleteBeforeDateTime = DateTime::Now - TimeSpan(2, 0, 0, 0);\t\t\/\/Delete files &gt; 2 days old\r\n\tarray&lt;String^&gt; ^files = Directory::GetFiles(&quot;C:\\\\MyFolder\\\\&quot;);\r\n\tfor each(sTemp in files)\r\n\t{\r\n\t\tif (File::GetCreationTime(sTemp) &lt; DeleteBeforeDateTime)\r\n\t\t{\r\n\t\t\ttry\r\n\t\t\t{\r\n\t\t\t\tif (File::Exists(sTemp))\r\n\t\t\t\t{\r\n\t\t\t\t\t\/\/If file has read only attribute set clear it so that delete can occur\r\n\t\t\t\t\tif ((File::GetAttributes(sTemp) &amp; FileAttributes::ReadOnly) == FileAttributes::ReadOnly)\r\n\t\t\t\t\t\tFile::SetAttributes(sTemp, FileAttributes::Normal);\r\n\r\n\t\t\t\t\tFile::Delete(sTemp);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tcatch (IOException ^)\r\n\t\t\t{\r\n\t\t\t\tGlobalObjects::EventLog1-&gt;AddEvent(true, 0x00, &quot;ExportVideoServer::ProcessTcpServerEvent can&#39;t delete old file: &quot; + sTemp);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>using namespace System::IO; Does directory exist? (Note paths can include a filename if you wish) if (!Directory::Exists(Path::GetDirectoryName(MyFileName))) \/\/or String ^path; path = &quot;C:\\\\Atest\\\\&quot;; \/\/ &#39;\\\\&#39;=&#39;\\&#39; if (!Directory::Exists(path)) \/\/or if (!Directory::Exists(&quot;C:\\\\Atest\\\\&quot;)) \/\/ &#39;\\\\&#39;=&#39;\\&#39; Create Directory (Note paths can include a filename if you wish) Directory::CreateDirectory(Path::GetDirectoryName(MyFileName)); \/\/or String ^path; path = &quot;C:\\\\Atest\\\\&quot;; \/\/ &#39;\\\\&#39;=&#39;\\&#39; Directory::CreateDirectory(path); \/\/or Directory::CreateDirectory(&quot;C:\\\\Atest\\\\&quot;); [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,30],"tags":[],"class_list":["post-196","post","type-post","status-publish","format-standard","hentry","category-file-input-output-serialization","category-ouptut-serialization"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=196"}],"version-history":[{"count":11,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/196\/revisions"}],"predecessor-version":[{"id":1128,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/196\/revisions\/1128"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}