powershell - create subdir struct adding a zero on single digit months

Viewed 127

i'm super new to powershell.

attempting to create a basic directory structure yyyy/mm

what it creates:

C:\2020
├───1
├───10
├───11
├───12
├───2
├───3
├───4
├───5
├───6
├───7
├───8
└───9

i'm trying to add a zero to months jan = 01...september 09. oct = 10, nov = 11, dec = 12

desired result:

C:\2020
├───01
├───02
├───03
├───04
├───05
├───06
├───07
├───08
├───09
├───10
├───11
└───12

this is the nested loop i have. any inputs are appreciated. thank you in advance

      for ($i=2020; $i -le 2022;$i++) #years from 2020 to 2022
            { 
                for ($j=01; $j -le 12;$j++) # for months eg 01-12
                    {
                        New-Item -ItemType Directory -Path $path\$i\$j -Force
                    }
            }    
4 Answers

You can use the format operator to add leading zeros. See https://ss64.com/ps/syntax-f-operator.html

2020..2022 | % {
    $year = $_
    1..12 | % {
        $month = "{0:d2}" -f $_
        New-Item -ItemType Directory -Path "$path\$year\$month" -Force
    }
}

You need to use PadLeft to add a preceding zero here.

New-Item -ItemType Directory -Path "$path\$i\$(([string]$j).PadLeft(2, '0'))" -Force

2 = How many characters in total the string should be.
'0' = The character you want to use preceding the given value.

Also, your variable needs to be a string for PadLeft so I've cast it to that type - ([string]$j).PadLeft()

For example

PS >  "2".PadLeft(2, '0')
02
PS >  "2".PadLeft(3, '0')
002
PS >  "2".PadLeft(4, '0')
0002
PS >  "2".PadRight(4, '0')
2000
PS >  "2".PadLeft(5, 'a') 
aaaa2

Another way with a datetime object in the for loop. "| % tostring 00" pads 0's with integer input.

for ($date = [datetime]'1/2020'; $date -le '12/2022'; $date = $date.addMonths(1)) {
  mkdir "$($date.year)\$($date.month | % tostring 00)" -whatif
}


What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2020\01".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2020\02".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2020\03".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2020\04".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2020\05".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2020\06".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2020\07".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2020\08".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2020\09".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2020\10".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2020\11".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2020\12".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2021\01".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2021\02".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2021\03".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2021\04".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2021\05".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2021\06".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2021\07".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2021\08".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2021\09".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2021\10".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2021\11".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2021\12".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2022\01".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2022\02".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2022\03".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2022\04".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2022\05".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2022\06".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2022\07".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2022\08".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2022\09".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2022\10".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2022\11".
What if: Performing the operation "Create Directory" on target "Destination: C:\Users\js\2022\12".
for ($i=2020; $i -le 2022;$i++) { 
    for ($j=01; $j -le 12;$j++) {
        if ($j -le 9) {
            [system.io.directory]::CreateDirectory("$path\$i\0$j")
        } else {
            [system.io.directory]::CreateDirectory("$path\$i\$j")
        }
    }
}

I added an if statement with a '0'

You'd do less computation on the entire list of objects, so it is a bit faster, and not too dissimilar to your original attempt.

Related