您好,我在这里寻求一些帮助来使用 PowerShell 创建文件夹结构。
指向根文件夹[
Yearly Structure Folder
],我正在该文件夹中寻找以下结构。
第一层子文件夹是一年中的月份,并带有数字前缀以保持顺序。
[
01. January
] 到 [12. December
]
每个月份文件夹内都具有相同的子结构,如下所示。
[
01. General
]、[02. Housekeep in
]、[03. Housekeep out
]、[04. Record
]、[05. Timesheet
]
每月的日期将位于上述每个文件夹中,并命名为 01.01.2024、02.01.2024 (dd-MM-yyyy) 等等以及每个月的天数。
[
01.01.2024
] 至 [31.01.2024
],且日期在该月内
然后在每个单个日期文件夹中都会包含这些文件夹。
[
01. Request
]、[02. Copies
]、[03. Report
]、[04. Recom
]、[05. Checks
]、[06. Forms
]
我附上了一个月的总体结构图像。我也一直在看这些帖子,尽管它们与我想要的类似,当我编辑它们并运行时,我收到错误,并且不知道如何组合它们。
感谢任何人有时间帮助我开始这个:)
我也一直在看这些帖子,虽然它们与我想要的类似,但当我编辑它们并运行时,我收到错误,并且不知道如何组合它们。
这就是我到目前为止所拥有的,尽管我不需要排除日期,也不需要以数字为前缀的单个日期。
{New-Item -Path "C:\Yearly Structure Folder" -Type Directory
$folders = ("01. General", "02. Housekeep in", "03. Housekeep out", "04. Record", "05. Timesheet" )
$folders | ForEach { New-Item -Path "C:\Yearly Structure Folder" -Name $_ -Type Directory }
$startDate = [datetime]'01-01-2024'
$endDate = $startDate.AddMonths(12)
$startWeek = 1
$folderPath = "C:\Yearly Structure Folder\01. General"
$datesToExclude = @(
# This will Exclude 30 of December 2021
) | ForEach-Object {$_ -as [datetime]}
do
{
if($startDate -notin $datesToExclude)
{
$folderName = "{0} {1}" -f $startWeek, $startDate.ToString('dd.MM.yyyy')
$newFolderPath = Join-Path $folderPath -ChildPath $folderName
New-Item $newFolderPath -ItemType Directory
}
$startDate = $startDate.AddDays(1)
$startWeek++
}until($startDate -ge $endDate)
您需要使用嵌套循环
这是我制作的最小的例子
我毫不怀疑有一些更有效的方法,也许使用哈希表
# replace the value with whatever you need
$Path = '.\'
$MonthSubDirectoryNameList = '01. General', '02. Housekeep in', '03. Housekeep out', '04. Record', '05. Timesheet'
$DaySubDirectoryNameList = '01. Request', '02. Copies', '03. Report', '04. Recom', '05. Checks', '06. Forms'
# set the year you want.
$Year = 2024
# set the starting date to january 1
$Date=Get-Date -Year $year -Month 1 -Day 1
# Set culture for the name of the Month.
# Invariant Culture FTW if you do not need some specific language
$Culture = [cultureinfo]::InvariantCulture
# for each day starting from 1 of January
# as long the Year is equal to $Year
# do stuff
# adding 1 day at the end of each loop
for ($Date; $date.year -eq $Year; $Date = $Date.AddDays(1)) {
foreach ($MonthSub in $MonthSubDirectoryNameList) {
foreach ($DaySub in $DaySubDirectoryNameList) {
#$DayDirectory = Join-Path -Path $Year -ChildPath $Date.ToString('MM. MMMM', $Culture) -AdditionalChildPath $MonthSub, $Date.ToString('dd.MM.yyyy'), $DaySub
$DayDirectory = [System.IO.Path]::Combine($year, $Date.ToString('MM. MMMM', $Culture) , $MonthSub, $Date.ToString('dd.MM.yyyy'), $DaySub)
# assigned to $null to hide the output.
$Null = New-Item -ItemType Directory -Path $PAth -Name $DayDirectory -Force
}
}
}