You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:使用PowerShell创建带指定层级与日期的年度文件夹结构

PowerShell 年度文件夹结构生成脚本

以下是完全符合需求的PowerShell脚本,无需排除日期,日期文件夹无额外编号前缀:

# 定义根路径和目标年份
$rootPath = "C:\Yearly Structure Folder"
$targetYear = 2024

# 创建根文件夹
New-Item -Path $rootPath -ItemType Directory -Force

# 定义月份文件夹名称数组
$monthFolders = @(
    "01. January", "02. February", "03. March", "04. April",
    "05. May", "06. June", "07. July", "08. August",
    "09. September", "10. October", "11. November", "12. December"
)

# 定义每个月份下的子文件夹
$monthSubFolders = @(
    "01. General", "02. Housekeep in", "03. Housekeep out",
    "04. Record", "05. Timesheet"
)

# 定义日期文件夹下的子文件夹
$dateSubFolders = @(
    "01. Request", "02. Copies", "03. Report",
    "04. Recom", "05. Checks", "06. Forms"
)

# 遍历每个月份文件夹
foreach ($monthFolder in $monthFolders) {
    # 从文件夹名称提取月份数字(如"01. January"取01)
    $monthNumber = [int]$monthFolder.Split('.')[0]
    # 创建月份文件夹路径
    $monthPath = Join-Path -Path $rootPath -ChildPath $monthFolder
    New-Item -Path $monthPath -ItemType Directory -Force

    # 创建月份下的5个子文件夹
    foreach ($subFolder in $monthSubFolders) {
        $subFolderPath = Join-Path -Path $monthPath -ChildPath $subFolder
        New-Item -Path $subFolderPath -ItemType Directory -Force

        # 获取当前月份的第一天和最后一天
        $firstDayOfMonth = Get-Date -Year $targetYear -Month $monthNumber -Day 1
        $lastDayOfMonth = $firstDayOfMonth.AddMonths(1).AddDays(-1)

        # 遍历当前月份的每一天
        $currentDate = $firstDayOfMonth
        while ($currentDate -le $lastDayOfMonth) {
            # 格式化日期文件夹名称为dd.MM.yyyy
            $dateFolderName = $currentDate.ToString("dd.MM.yyyy")
            $dateFolderPath = Join-Path -Path $subFolderPath -ChildPath $dateFolderName
            New-Item -Path $dateFolderPath -ItemType Directory -Force

            # 创建日期文件夹下的6个子文件夹
            foreach ($dateSub in $dateSubFolders) {
                $finalPath = Join-Path -Path $dateFolderPath -ChildPath $dateSub
                New-Item -Path $finalPath -ItemType Directory -Force
            }

            # 日期加1天
            $currentDate = $currentDate.AddDays(1)
        }
    }
}

Write-Host "文件夹结构创建完成!"

关键细节说明:

  • -Force 参数:确保文件夹已存在时不会报错,直接跳过重复创建操作
  • 月份数字提取:通过拆分月份文件夹名称的前缀,精准获取对应月份,保证日期范围计算准确
  • 日期范围计算:通过Get-Date定位当月第一天,再通过AddMonths(1).AddDays(-1)自动获取当月最后一天,适配不同月份的天数差异
  • 多层循环嵌套:依次遍历月份、月份子文件夹、日期、日期子文件夹,完整构建指定的层级结构

内容的提问来源于stack exchange,提问作者user24911908

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 11:41:12