powershelldirectorystructure

Create a folder with sub-folder structure with specific names and dates


Hi I'm looking for a little help here to create a folder structure using PowerShell.

Pointing at a root folder [Yearly Structure Folder], im looking for the following structure within this folder.

The first layer of subfolders would be the months of the year prefixed wiith a number to keep them in order.

[01. January] through to [12. December]

Inside each of the month folders would have the same sub structure as follows.

[01. General], [02. Housekeep in], [03. Housekeep out], [04. Record], [05. Timesheet]

The dates of the month would be in each of the above folders and named 01.01.2024, 02.01.2024 (dd-MM-yyyy) and so forth with the amount of days in each month.

[01.01.2024] through to [31.01.2024] with the correct dates within that month

Then within each of the single date folders would have these folders in each one.

[01. Request], [02. Copies], [03. Report], [04. Recom], [05. Checks], [06. Forms]

I have attached an image of the general structure for the one month. I have also been looking at these posts although they are similar to what I want, When I edit them and run, I get errors and not sure how to combine them.

Creating a sub-folder structure

create a folder structure with name and date +7 days

Appreciate anyone have a bit time to help me get started with this :)

I have also been looking at these posts although they are similar to what I want, When I edit them and run, I get errors and not sure how to combine them.

Creating a sub-folder structure

create a folder structure with name and date +7 days

Screenshot

This is what I have so far, although I dont need to exclude dates and dont need the single dates prefixed with a number.

   {New-Item -Path "C:\Yearly Structure Folder" -Type Directory

$folders = ("01. General", "02. Housekeep in", "03. Housekeep out", "04. Record", "05. Timesheet" )
$folders | ForEach { New-Item -Path "C:\Yearly Structure Folder" -Name $_ -Type Directory }


$startDate = [datetime]'01-01-2024'
$endDate = $startDate.AddMonths(12)
$startWeek = 1
$folderPath = "C:\Yearly Structure Folder\01. General"
$datesToExclude = @(
      # This will Exclude 30 of December 2021
) | ForEach-Object {$_ -as [datetime]}

do
{
if($startDate -notin $datesToExclude)
{
$folderName = "{0} {1}" -f $startWeek, $startDate.ToString('dd.MM.yyyy')
$newFolderPath = Join-Path $folderPath -ChildPath $folderName
New-Item $newFolderPath -ItemType Directory
}

$startDate = $startDate.AddDays(1)
$startWeek++

}until($startDate -ge $endDate)

Solution

  • You need to use Nested Loops

    Here the smallest example I managed to make
    I have no doubt there is some more efficient way, perhaps with hashtables

    # replace the value with whatever you need
    $Path = '.\'
    
    $MonthSubDirectoryNameList = '01. General', '02. Housekeep in', '03. Housekeep out', '04. Record', '05. Timesheet'
    $DaySubDirectoryNameList = '01. Request', '02. Copies', '03. Report', '04. Recom', '05. Checks', '06. Forms'
    
    
    # set the year you want.
    $Year = 2024
    
    # set the starting date to january 1
    $Date=Get-Date -Year $year -Month 1 -Day 1
    
    
    
    # Set culture for the name of the Month.
    # Invariant Culture FTW if you do not need some specific language
    $Culture = [cultureinfo]::InvariantCulture
    
    # for each day starting from 1 of January
    # as long the Year is equal to $Year
    # do stuff
    # adding 1 day at the end of each loop
    for ($Date; $date.year -eq $Year; $Date = $Date.AddDays(1)) {
    
        foreach ($MonthSub in $MonthSubDirectoryNameList) {
            foreach ($DaySub in $DaySubDirectoryNameList) { 
                #$DayDirectory = Join-Path -Path $Year -ChildPath $Date.ToString('MM. MMMM', $Culture) -AdditionalChildPath $MonthSub, $Date.ToString('dd.MM.yyyy'), $DaySub
                $DayDirectory = [System.IO.Path]::Combine($year, $Date.ToString('MM. MMMM', $Culture) , $MonthSub, $Date.ToString('dd.MM.yyyy'), $DaySub)
                # assigned to $null to hide the output.
                $Null = New-Item -ItemType Directory -Path $PAth -Name $DayDirectory -Force
            }
            
        }
        
    }