代码之家  ›  专栏  ›  技术社区  ›  mappetshow

创建包含日期等的多个文件夹190101到191231

  •  0
  • mappetshow  · 技术社区  · 6 年前

    我试图创建多个文件夹,但一年内的日期除外。 此外,我一直在使用PowerShell并尝试创建批处理脚本。 我试过几种方法,但没有一种能满足我的需要。 所以,我需要创建一个文件夹,从190101到191231,一整年都是空的。但无论我做什么我都得不到我想要的。

    实例:

    01..31 | foreach $_{ New-Item -ItemType Directory -Name $("1901" + $_)}
    
    mkdir $(01..31 | %{"ch$_"})
    
    md(01..31|%{"1901$_"})
    

    但是这里的问题,他们在“天”里没有给我0,所以,我有 19011年取代190101年。

    我找不到如何提取日期和推送PowerShell来创建我需要的内容。

    3 回复  |  直到 6 年前
        1
  •  1
  •   Ansgar Wiechers    6 年前

    使用 format operator ( -f )它就是为了这个目的而制造的。

    1..31 | ForEach-Object {
        New-Item -Type Directory -Name ('1901{0:d2}' -f $_)
    }
    
        2
  •  4
  •   Lee_Dailey    6 年前

    这里有一个稍微更通用的版本,可以在任何给定的月份使用。这个 -f 字符串格式操作符真的很方便…[ 露齿而笑 ]

    $Today = (Get-Date).Date
    $Year = $Today.Year
    $Month = $Today.Month
    $DaysInMonth = (Get-Culture).Calendar.GetDaysInMonth($Year, $Month)
    
    foreach ($Day in 1..$DaysInMonth)
        {
        '{0}{1:D2}' -f $Today.ToString('yyMM'), $Day
        }
    

    截断的输出…

    190101
    190102
    [*...snip...*] 
    190130
    190131
    
        3
  •  2
  •   LotPings    6 年前

    每天创建文件夹的一种方法

    • 定义/获取年份
    • 将开始日期设置为1月1日
    • 以零为基础的抵销,得到12月30日的年度日。
    • 使用范围将days添加到startdate并迭代

    $year = (Get-Date).Year
    $startdate = Get-Date -Year $year -Month 1 -Day 1
    0..(Get-Date -Year $year -Month 12 -Day 30).DayOfYear| ForEach-Object{
      mkdir ($startdate.AddDays($_).ToString('yyMMdd')
    )
    
    推荐文章