我编写了一个代码,该代码基本上花费了上个月的时间,将其复制,重命名为下个月,并在保留文件夹结构的同时删除了所有文件。此代码有效。一月份,我试图变得幻想起来并失败了。我想为一月份做的就是开一张支票…是新的一年吗?如果否:查看当年并用新年创建一个新文件夹,将当年的12月复制到新年文件夹中,然后继续执行我的工作脚本。如果是,请执行以下操作:使用新年创建文件夹,复制去年的12月,然后继续使用工作脚本。
-----
$arubaBuildsRootPath = "***"
$oldMonth = "12 - December"
$year = (Get-Date).year
$newMonth = "01 - January"
$newYear = $year + 1
$oldYear = $year - 1
#Create a variable $correctYear so the $correctYear variable always shows the new year date as defined in the if statement above correctly
if( -Not (Test-Path -Path $arubaBuildsRootPath\$year ) )
{
$correctYear = (Get-Date).year
}
Else
{
$correctYear = (Get-Date).year + 1
}
#Determine if it is the old year or the new year at time scipt is run
if( -Not (Test-Path -Path $arubaBuildsRootPath\$year ) )
{
New-Item -ItemType directory -Path $arubaBuildsRootPath\$year
}
Else
{
New-Item -ItemType directory -Path $arubaBuildsRootPath\$newYear
}
#$dir = '$arubaBuildsRootPath\2019'
#
#$year = (Get-Date).Year
#if ("$year" -eq (Split-Path $dir -Leaf)) {
# do something
#} else {
# $parent = Split-Path $dir -Parent
# $newdir = Join-Path $parent ($year + 1)
# New-Item $newdir -Type Directory | Out-Null
#}
if( -Not (Test-Path -Path $arubaBuildsRootPath\$correctYear\$newMonth ) )
{
Copy-Item -Path "$arubaBuildsRootPath\$oldYear\$oldMonth\" -Destination "$arubaBuildsRootPath\$newYear\$newMonth" -recurse -Force
}
Else
{
Copy-Item -Path "$arubaBuildsRootPath\$year\$oldMonth\" -Destination "$arubaBuildsRootPath\$newYear\$newMonth" -recurse -Force
}
---
#Copy-Item -Path "$arubaBuildsRootPath\$year\$oldMonth\" -Destination "$arubaBuildsRootPath\$newYear\$newMonth" -recurse -Force
"Work, work";
Start-Sleep -Second 2;
Get-ChildItem -Path $newMonth -Include *.* -File -Recurse | foreach { $_.Delete()};
"";
"Job's Done";
Start-Sleep -Second 2;
stop-process -Id $PID
答案 0 :(得分:1)
您最好解释上个月要复制的 。
鉴于此旧结构
> Tree A:\ /F
A:\
└───Test
└───2019
└───03 - Mar
│ bar.txt
│
└───foo
baz.txt
此脚本:
$arubaBuildsRootPath = 'A:\Test'
# $arubaBuildsRootPath = "***"
$ThisMonth = Join-Path $arubaBuildsRootPath (Get-Date -f 'yyyy\\MM - MMM')
$PrevMonth = Join-Path $arubaBuildsRootPath (Get-Date).AddMonths(-1).ToString('yyyy\\MM - MMM')
if (!(Test-Path $ThisMonth)){
New-Item -Path $ThisMonth -ItemType Directory | Out-Null
Robocopy $PrevMonth $ThisMonth /XJ /E /NOCOPY
}
Tree A:\ /F
这里的产量:
> Tree A:\ /F
A:\
└───Test
└───2019
├───03 - Mar
│ │ bar.txt
│ │
│ └───foo
│ baz.txt
│
└───04 - Apr
└───foo
因此,仅将文件夹结构复制到实际月份。
答案 1 :(得分:0)
那呢?
$curr = Get-Date
$next = $curr.AddMonths(1)
$srce = 'arubaBuildsRootPath\' + $curr.Year + '\' + $curr.Month + '\'
$dest = 'arubaBuildsRootPath\' + $next.Year + '\' + $next.Month
$excl = Get-ChildItem $dest
Copy-Item -Path $srce -Destination $dest -Filter {PSIsContainer} -Exclude $excl -Recurse