我有一个 poweshell 文件,它会创建一个文件夹,并为其指定一个年份的名称,然后在其中创建 12 个文件夹,并将它们标记为 4 月 1 日至 3 月 12 日。
我想补充的是,主文件夹显示年份(这是来自用户输入)是显示年份加上 1 年的文件夹,因为文件夹的内容跨越 2 年,即:2021 - 2022。我希望我已经解释好了。
新增加的是,12 个文件夹中的每一个都包含相同的文件,是否有办法在创建时或创建后将该文件放在文件夹中,该文件与 powershell 脚本位于同一个主文件夹中,基本上所有事情都发生在主文件夹中。
目前的情况如下:
###Get the year entered by the user
$year = Read-Host "Please enter year"
###Set the starting date
$startdate = [DateTime] "01 April $($year)"
$count = 1
###Create folder based on the year entered by the user
New-Item -ItemType Directory -Name "$($year)"
###Change directory to the newly created folder
cd "$($year)"
While ( $count -lt 13)
{
###Create folders
New-Item -ItemType Directory -Name "$($count.ToString('00')) - $($startdate.ToString('MMMM yyyy'))"
#Add one month
$startdate = $startdate.AddMonths(1)
#Increment the counter
$count = $count + 1
}
答案1
我修改了代码,在创建文件夹时包含这两个年份。此外,在a.txt
创建所有 12 个文件夹后,将文件(在本例中为)复制到这些文件夹中。您需要更改a.txt
为正确的文件名。
###Get the year entered by the user
$year = Read-Host "Please enter year"
$nextyear = [int]$year + 1
$yearfolder = "$year-$nextyear"
###Set the starting date
$startdate = [DateTime] "01 April $($year)"
$count = 1
###Set the file's path by combining the folder path and the filename
$filename = join-Path -Path (Get-location) -ChildPath "a.txt"
###Create folder based on the year entered by the user
New-Item -ItemType Directory -Name "$($yearfolder)"
###Change directory to the newly created folder
cd "$($yearfolder)"
While ( $count -lt 13)
{
###Create folders
$newfolder = "$($count.ToString('00')) - $($startdate.ToString('MMMM yyyy'))"
New-Item -ItemType Directory -Name "$newfolder"
#Copy file to new folder
Copy-Item -Path $filename -Destination $newfolder
#Add one month
$startdate = $startdate.AddMonths(1)
#Increment the counter
$count = $count + 1
}