Powershell script to create folders based on week ending date
We are frequently on projects subcontracted by IBM. As such, we have to submit our time and expenses for each week which ends on Friday’s in IBM’s time tracking system. Sometimes we have multiple projects going on at the same time and every Friday afternoon (or Sunday evening depending upon travel and general motivation), I spend about an hour writting these ups. I like to keep each week in its own folder, with a specific naming convention, but its always tedious and error prone to create a new folder each week (was last Friday the 25 or 26th??). Thus to simplify this (and to avoid some more productive work), I wrote a Powershell script to create these folders in bulk. Thus if I know I’m on a project for 4 weeks, I’ll just run this and create all the folders for that month, and possibly the following month.
################################################ # Usage: # 1. Initialize this script by clicking the play button first. # 2. Navigate to the desired directory to create the folders # 3. Type the function name Build-DateFOlders and the parameters, Year, Month, and an integer for the number of weeks to create for that month # example: Issue the command "Build-DateFOlders 2018 1 4" will create the following folders # 2018-01-05 # 2018-01-12 # 2018-01-19 # # Author: Kenny Smith, Strongback Consulting # License: Creative Commons Attribution. Share, but show the love! # https://creativecommons.org/licenses/by/3.0/us/ ###################################################### function Get-NthWeekday ( [int] $yr, [int] $mo, [int] $nth, [string] $WeekDayToFind ) ##################################################################### # # PURPOSE: Get the Nth Weekday of a given month. # ###################################################################### { # Error checking if ($yr -lt 1990 -or $yr -gt 2038) {Write-Host "Year must be 1990 to 2038";throw "*** YEAR NOT BETWEEN 1990 and 2038! ***"} if ($mo -lt 1 -or $mo -gt 12) {Write-Host "Bad month! Try again."; throw "*** BAD MONTH! ***"} if ($nth -lt 1 -or $nth -gt 5) {Write-Host "Nth must be between 1 and 5"; throw "*** BAD Nth! ***" } if ( 'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday' ` -notcontains $WeekDayToFind ) {Write-Host "Not a weekday!"; throw "*** NOT A WEEKDAY! ***"} # start from the first day of the month $TargetMonthFirstDate = New-Object System.DateTime $yr, $mo, 1 # $TargetMonthFirstWeekday $WorkingDate = $TargetMonthFirstDate # loop until we get to the $nth instance of $WeekDayToFind while ($nth) { if ($WorkingDate.DayOfWeek -eq $WeekDayToFind) {$nth = $nth-1} # this second IF is needed if the 1st falls on the $WeekDayToFind # to get the correct result. if ($nth -gt 0) { $WorkingDate = $WorkingDate.AddDays(1) } } $WorkingDate } #end function function Build-DateFOlders([int] $yr, [int] $mo, [int]$countOfWeeks){ ##################################################################### # # PURPOSE: Creates a folder for each Friday of the given month in the format yyyy-MM-dd # ###################################################################### for($i=1;$i -le $countOfWeeks;$i++){ $datestuff = Get-NthWeekday $yr $mo $i "Friday" $dirstring = $datestuff.ToString('yyyy-MM-dd') New-Item -ItemType Directory -Path ".\$dirstring" } }