gpt4 book ai didi

powershell - 创建 subdir 结构,在个位数月份添加零

转载 作者:行者123 更新时间:2023-12-02 22:51:35 25 4
gpt4 key购买 nike

我是 Powershell 的 super 新手。

正在尝试创建基本目录结构 yyyy/mm

它创造了什么:

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

我正在尝试将零添加到月份 jan = 01...september 09.oct = 10, nov = 11, dec = 12

期望的结果:

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

这是我的嵌套循环。任何意见表示赞赏。提前谢谢你

      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
}
}

最佳答案

您可以使用格式运算符添加前导零。参见 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
}
}

关于powershell - 创建 subdir 结构,在个位数月份添加零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63641146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com