powershelldatetimedatetime-conversion

PowerShell script to create subfolders in folders


Am getting errors when executing powershell script.I got folders on the name of date DD MMM example 01 May,02 May go on to 31 May.I want to get the name of the folder 01 May covert it to 01 May 2025 to check if the date is monday then add subfolders of different name in it.But I got stuck with this part of code guving me errors when run

if([datetime]::TryParseExact("$folderName $currentYear", "dd MMM yyyy", $null, [System.Globalization.DateTimeStyles]::None, [ref]$dateParsed)

The error I am getting:

Cannot find an overload for "TryParseExact" and the argument count: "5".

The full code block:

$basePath = "C:\Path\To\Your\Folders"
$currentYear = (Get-Date).Year

# Loop through each subfolder
Get-ChildItem -Path $basePath -Directory | ForEach-Object {
    $folder = $_
    $folderName = $folder.Name

    # Attempt to parse date with current year
    $dateParsed = $null
    if ([datetime]::TryParseExact("$folderName $currentYear", "dd MMM yyyy", $null, [System.Globalization.DateTimeStyles]::None, [ref]$dateParsed)) {
        $dayOfWeekNum = [int]$dateParsed.DayOfWeek  # 0=Sunday, 1=Monday, ..., 6=Saturday

        switch ($dayOfWeekNum) {
            1 { $subfolders = @("vishal", "rajnita", "tashi") }   # Monday
            2 { $subfolders = @("rajoo", "vidula") }              # Tuesday
            default { 
                Write-Host "Skipping $folderName - No action for $($dateParsed.DayOfWeek)"
                return
            }
        }

        # Create the subfolders if needed
        foreach ($sub in $subfolders) {
            $newPath = Join-Path $folder.FullName $sub
            if (-not (Test-Path $newPath)) {
                New-Item -Path $newPath -ItemType Directory | Out-Null
                Write-Host "Created: $newPath"
            } else {
                Write-Host "Exists: $newPath"
            }
        }
    } else {
        Write-Host "Invalid format: $folderName"
    }
}

Solution

  • You can check the weekday name by using the Get-Date cmdlet with UFormat format specifiers.

    The format specifier %w returns the numeric day of the week (0-6, Sunday = 0, Saturday = 6).

    The format specifier %u returns the numeric day of the week (1-7, Monday = 1, Sunday = 7, changed in PowerShell 7.2).

    The format specifier %A returns the day of the week as full name, e.g. Monday.

    $folderName = "05 May"
    $day, $month = $folderName.Split(' ')
    
    if ((Get-Date -Date "$currentYear $month $day" -UFormat %w) -eq 1) {
        # do add subfolders
    }
    

    Here comes an edited version of the original code that should work:

    $basePath = "C:\Path\To\Your\Folders"
    $currentYear = (Get-Date).Year
    
    # Loop through each subfolder
    Get-ChildItem -Path $basePath -Directory | ForEach-Object {
        $folder = $_
        $folderName = $folder.Name
    
        $day, $month = $folderName.Split(' ')
    
        # Attempt to get date with current year
        try {
            switch (Get-Date -Date "$currentYear $month $day" -UFormat %w) {
                0 { $subfolders = @("test1", "test2", "test3") }      # Sunday
                1 { $subfolders = @("vishal", "rajnita", "tashi") }   # Monday
                2 { $subfolders = @("rajoo", "vidula") }              # Tuesday
                default { 
                    Write-Host "Skipping $folderName - No action for $(Get-Date -Date "$currentYear $month $day" -UFormat %A)"
                    return
                }
            }
    
            # Create the subfolders if needed
            foreach ($sub in $subfolders) {
                $newPath = Join-Path $folder.FullName $sub
                if (-not (Test-Path $newPath)) {
                    New-Item -Path $newPath -ItemType Directory | Out-Null
                    Write-Host "Created: $newPath"
                }
                else {
                    Write-Host "Exists: $newPath"
                }
            }
        }
        catch {
            Write-Host "Invalid format: $folderName"
        }
    }