Powershell 脚本根据上次写入时间移动错误的文件
Powershell script is moving wrong files based on lastwritetime
我有一个 powershell 脚本,它使用文件的最后写入时间将文件从一个文件夹移动到另一个文件夹。该脚本正在检查 lastwritetime 是否小于或等于指定日期。似乎随着年份的变化,这不再正常工作。我有修改日期为 12/29/2016、12/30/2016 和 1/2/2017 的文件。 12/29 文件应该是唯一移动的文件,因为今天是 2017 年 1 月 3 日,脚本使用该日期减去 5 天。因此,它应该只移动小于或等于 12/29/2016 的任何东西。但是,1/2/2017 文件也被移动了。脚本在下面,关于为什么会发生这种情况的任何想法。
#Grab the day of the week it is TODAY
$DayofWeek = (Get-Date).DayofWeek
#Set the location we are going to be Pulling Files From
$FromPath = "path i'm copying files from goes here"
#Set the Location we are going to copy file To
$ToPath = "path i'm copying files to"
#Set a Default Value for DaysBack to Zero
$DaysBack = 0
#Set the Days Back if it is Tuesday through Friday
switch ($DayofWeek)
{
"Tuesday" { $DaysBack = -5 }
"Wednesday" { $DaysBack = -5 }
"Thursday" { $DaysBack = -3 }
"Friday" { $DaysBack = -3 }
"Saturday" { $DaysBack = -3 }
#If today is not an above day then tell the user today no files should be moved
default { Write-host "No files to move!" }
}
#if DaysBack does not equal ZERO then there are files that need to be moved!
if($DaysBack -ne 0) {
Get-ChildItem -Path $FromPath |
Where-Object { $_.LastWriteTime.ToString("MMddyyyy") -le (Get-Date).AddDays($DaysBack).ToString("MMddyyyy") } |
Move-Item -Destination $ToPath
}
$date1 = Get-Date -Year 2017 -Month 1 -Day 2
$date2 = Get-Date -Year 2016 -Month 12 -Day 29
$date1 -gt $date2
#returns True
$date1.ToString("MMddyyyy") -gt $date2.ToString("MMddyyyy")
# "01022017" -gt "12292016"
# returns False
您可以使用
if($DaysBack -ne 0) {
Get-ChildItem -Path $FromPath |
Where-Object { $_.LastWriteTime -le (Get-Date).AddDays($DaysBack) } |
Move-Item -Destination $ToPath
}
或
if($DaysBack -ne 0) {
Get-ChildItem -Path $FromPath |
Where-Object { $_.LastWriteTime.ToString("yyyyMMdd") -le (Get-Date).AddDays($DaysBack).ToString("yyyyMMdd") } |
Move-Item -Destination $ToPath
}
我有一个 powershell 脚本,它使用文件的最后写入时间将文件从一个文件夹移动到另一个文件夹。该脚本正在检查 lastwritetime 是否小于或等于指定日期。似乎随着年份的变化,这不再正常工作。我有修改日期为 12/29/2016、12/30/2016 和 1/2/2017 的文件。 12/29 文件应该是唯一移动的文件,因为今天是 2017 年 1 月 3 日,脚本使用该日期减去 5 天。因此,它应该只移动小于或等于 12/29/2016 的任何东西。但是,1/2/2017 文件也被移动了。脚本在下面,关于为什么会发生这种情况的任何想法。
#Grab the day of the week it is TODAY
$DayofWeek = (Get-Date).DayofWeek
#Set the location we are going to be Pulling Files From
$FromPath = "path i'm copying files from goes here"
#Set the Location we are going to copy file To
$ToPath = "path i'm copying files to"
#Set a Default Value for DaysBack to Zero
$DaysBack = 0
#Set the Days Back if it is Tuesday through Friday
switch ($DayofWeek)
{
"Tuesday" { $DaysBack = -5 }
"Wednesday" { $DaysBack = -5 }
"Thursday" { $DaysBack = -3 }
"Friday" { $DaysBack = -3 }
"Saturday" { $DaysBack = -3 }
#If today is not an above day then tell the user today no files should be moved
default { Write-host "No files to move!" }
}
#if DaysBack does not equal ZERO then there are files that need to be moved!
if($DaysBack -ne 0) {
Get-ChildItem -Path $FromPath |
Where-Object { $_.LastWriteTime.ToString("MMddyyyy") -le (Get-Date).AddDays($DaysBack).ToString("MMddyyyy") } |
Move-Item -Destination $ToPath
}
$date1 = Get-Date -Year 2017 -Month 1 -Day 2
$date2 = Get-Date -Year 2016 -Month 12 -Day 29
$date1 -gt $date2
#returns True
$date1.ToString("MMddyyyy") -gt $date2.ToString("MMddyyyy")
# "01022017" -gt "12292016"
# returns False
您可以使用
if($DaysBack -ne 0) {
Get-ChildItem -Path $FromPath |
Where-Object { $_.LastWriteTime -le (Get-Date).AddDays($DaysBack) } |
Move-Item -Destination $ToPath
}
或
if($DaysBack -ne 0) {
Get-ChildItem -Path $FromPath |
Where-Object { $_.LastWriteTime.ToString("yyyyMMdd") -le (Get-Date).AddDays($DaysBack).ToString("yyyyMMdd") } |
Move-Item -Destination $ToPath
}