{"id":4676,"date":"2012-11-13T09:47:00","date_gmt":"2012-11-13T09:47:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/11\/13\/use-powershell-to-create-a-bunch-of-folders-based-upon-the-date\/"},"modified":"2012-11-13T09:47:00","modified_gmt":"2012-11-13T09:47:00","slug":"use-powershell-to-create-a-bunch-of-folders-based-upon-the-date","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-create-a-bunch-of-folders-based-upon-the-date\/","title":{"rendered":"Use PowerShell to Create a Bunch of Folders Based upon the Date"},"content":{"rendered":"<p><strong>Summary:<\/strong>&nbsp;Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create a bunch of folders based upon the date.\nMicrosoft Scripting Guy, Ed Wilson, is here. Well, this morning I took the Scripting Wife to the Charlotte airport to begin our trip to Europe. It is beyond me why Teresa must fly nearly 1,200 miles in the opposite direction&mdash;to Dallas, Texas&mdash;before she gets on another air plane, and then flies 1,200 miles back towards Charlotte, North Carolina, on her way to Frankfurt, Germany. Anyway, I leave this evening and will meet her at the train station at the Frankfurt airport. Tomorrow, we will be in Dortmund, Germany, and will have dinner with the past winner of the Scripting Games Klaus Shulte. We are looking forward to that.<\/p>\n<h2>Create folders to manage pictures<\/h2>\n<p>Anyway, Teresa was adamant&mdash;create a bunch of folders on our external USB 3 &ldquo;indestructible&rdquo; hard drive. Each folder should be based upon the date. In this way, when it is late at night, I will not just &ldquo;dump&rdquo; 128 GB of pictures in the root folder and call it a day (or a night). I guess she knows how I am&mdash;I love taking pictures and I hate organizing pictures.<\/p>\n<h2>First figure out how many folders to create<\/h2>\n<p>The first thing I need to do is to figure out how many folders to create. I could&nbsp;<em>count,&nbsp;<\/em>but dude &hellip; that is sooooo last century. Instead, I decided to use <strong>New-TimeSpan<\/strong> because I know when I leave&mdash; today&mdash;and when we return. Here is the code I ran along with the associated output.<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; New-TimeSpan -Start (get-date) -End 12\/3\/12<\/p>\n<p style=\"padding-left: 30px\"> Days&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 19&nbsp;<br \/> Hours&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 11&nbsp;<br \/> Minutes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 24&nbsp;<br \/> Seconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 59&nbsp;<br \/> Milliseconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 191&nbsp;<br \/> Ticks&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 16826991917033&nbsp;<br \/> TotalDays&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 19.4756850891586&nbsp;<br \/> TotalHours&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 467.416442139806&nbsp;<br \/> TotalMinutes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 28044.9865283883&nbsp;<br \/> TotalSeconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1682699.1917033&nbsp;<br \/> TotalMilliseconds : 1682699191.7033<\/p>\n<h2>Now the names from dates&mdash;it&rsquo;s easy<\/h2>\n<p>All I need to do is to create 19 folders based upon dates in the future. So this means a little repetition, which means I will use the <strong>Foreach-Object<\/strong> cmdlet. I can get the dates by using the&nbsp;<strong>adddays<\/strong><em>&nbsp;<\/em>method, and there is a&nbsp;<strong>tostring<\/strong><em>&nbsp;<\/em>method that turns the <strong>datetime<\/strong> object into a string. I can also specify the type of string to create. Here is the code I came up with to create the names (later I will add the code to create the folders). (I do not need a folder for today, so I begin at 1. Also, we fly home on December 3rd, but we are coming by train back from Prague, so we will need a folder for the December 3rd. So, I add one number there and end at 20 instead of 19.<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; 1..20 | % {(get-date).adddays($_).tostring(&#8220;MMddyyyy&#8221;)}&nbsp;<br \/> 11142012&nbsp;<br \/> 11152012&nbsp;<br \/> 11162012&nbsp;<br \/> 11172012&nbsp;<br \/> 11182012&nbsp;<br \/> 11192012&nbsp;<br \/> 11202012&nbsp;<br \/> 11212012&nbsp;<br \/> 11222012&nbsp;<br \/> 11232012&nbsp;<br \/> 11242012&nbsp;<br \/> 11252012&nbsp;<br \/> 11262012&nbsp;<br \/> 11272012&nbsp;<br \/> 11282012&nbsp;<br \/> 11292012&nbsp;<br \/> 11302012&nbsp;<br \/> 12012012&nbsp;<br \/> 12022012&nbsp;<br \/> 12032012<\/p>\n<h2>Ready to create the folders (from the dates) all at once<\/h2>\n<p>Now, I need to create the folders on my portable USB drive. It is drive F (I set it in computer management and treat it as if it were a fixed disk). Instead of just creating folder names, this time, I add the <strong>New-Item<\/strong> cmdlet. The name is obvious and the type is a directory. Because I changed my working drive to the F drive, I do not need to worry about a path. I use <strong>PushD<\/strong> to store the current location, and then I use <strong>Set-Location<\/strong> to change to the F drive. Next, I create the folders and, finally, I return to my working drive via popd. The commands is shown here.<\/p>\n<p style=\"padding-left: 30px\">pushd&nbsp;<br \/> sl f:<\/p>\n<p style=\"padding-left: 30px\">1..20 | % {New-Item -Name (get-date).adddays($_).tostring(&#8220;MMddyyyy&#8221;) -ItemType directory}<\/p>\n<p style=\"padding-left: 30px\">popd\nWell, that is it. I need to finish packing so I can join Teresa in Frankfurt tomorrow. Until then, take care, and happy scripting.\nI invite you to follow me on&nbsp;<a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a>&nbsp;and&nbsp;<a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at&nbsp;<a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the&nbsp;<a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary:&nbsp;Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create a bunch of folders based upon the date. Microsoft Scripting Guy, Ed Wilson, is here. Well, this morning I took the Scripting Wife to the Charlotte airport to begin our trip to Europe. It is beyond me why Teresa must fly nearly 1,200 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[11,377,3,4,12],"class_list":["post-4676","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-folders","tag-powershell","tag-scripting-guy","tag-scripting-techniques","tag-storage"],"acf":[],"blog_post_summary":"<p>Summary:&nbsp;Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create a bunch of folders based upon the date. Microsoft Scripting Guy, Ed Wilson, is here. Well, this morning I took the Scripting Wife to the Charlotte airport to begin our trip to Europe. It is beyond me why Teresa must fly nearly 1,200 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4676","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=4676"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4676\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=4676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}