November 13th, 2012

Use PowerShell to Create a Bunch of Folders Based upon the Date

Summary: 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 miles in the opposite direction—to Dallas, Texas—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.

Create folders to manage pictures

Anyway, Teresa was adamant—create a bunch of folders on our external USB 3 “indestructible” hard drive. Each folder should be based upon the date. In this way, when it is late at night, I will not just “dump” 128 GB of pictures in the root folder and call it a day (or a night). I guess she knows how I am—I love taking pictures and I hate organizing pictures.

First figure out how many folders to create

The first thing I need to do is to figure out how many folders to create. I could count, but dude … that is sooooo last century. Instead, I decided to use New-TimeSpan because I know when I leave— today—and when we return. Here is the code I ran along with the associated output.

PS C:> New-TimeSpan -Start (get-date) -End 12/3/12

Days              : 19 
Hours             : 11 
Minutes           : 24 
Seconds           : 59 
Milliseconds      : 191 
Ticks             : 16826991917033 
TotalDays         : 19.4756850891586 
TotalHours        : 467.416442139806 
TotalMinutes      : 28044.9865283883 
TotalSeconds      : 1682699.1917033 
TotalMilliseconds : 1682699191.7033

Now the names from dates—it’s easy

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 Foreach-Object cmdlet. I can get the dates by using the adddays method, and there is a tostring method that turns the datetime 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.

PS C:> 1..20 | % {(get-date).adddays($_).tostring(“MMddyyyy”)} 
11142012 
11152012 
11162012 
11172012 
11182012 
11192012 
11202012 
11212012 
11222012 
11232012 
11242012 
11252012 
11262012 
11272012 
11282012 
11292012 
11302012 
12012012 
12022012 
12032012

Ready to create the folders (from the dates) all at once

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 New-Item 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 PushD to store the current location, and then I use Set-Location 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.

pushd 
sl f:

1..20 | % {New-Item -Name (get-date).adddays($_).tostring(“MMddyyyy”) -ItemType directory}

popd Well, that is it. I need to finish packing so I can join Teresa in Frankfurt tomorrow. Until then, take care, and happy scripting. I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace. Ed Wilson, Microsoft Scripting Guy

Author

0 comments

Discussion are closed.