删除7天前的文件

删除7天前的文件

powershell 删除7天前的文件

$today=Get-Date
#"今天是:$today"
#昨天
#"昨天是:$($today.AddDays(-1))"
#明天
#"明天是:$($today.AddDays(1))"
#一周前
#"一周前是: $($today.AddDays(-7))"
#五个月前
#"五个月前:$($today.AddMonths(-5))"
#十年之前
#"十年之前:$($today.AddYears(-10).Year)年,我们是朋友."
#日期格式化
#"格式化日期:" + $today.ToString('yyyy-MM-dd')
$7day=$($today.AddDays(-7).ToString('yyyy-MM-dd'))
#$7day $localdbdir="D:\database_bak"
$remotedbdir="D:\master_database_bak"
$downonlinefiledir="D:\db_online_bak" #删除7天前的文件 function delfiles
{
#布尔类型转换成整数
$result=Test-Path $_.FullName |foreach { [int] $_ }
if ($result -eq 1) {
del $_.FullName
"删除文件成功!"
}
else{
"文件不存在"
break
}
}
#Test-Path $downwebfiles function delfile
{
if($args.Count -eq 0)
{
"No argument!"
}
else
{ $args | foreach { delfiles "$($_)"}
}
} function delfilelist
{
#$args
if($args.Count -eq 0)
{
"No argument!"
}
elseif($args -eq $downonlinefiledir)
{
Get-ChildItem -Path $args -Recurse -ErrorAction SilentlyContinue -Filter *.rar |Where-Object { $_.Extension -eq '.rar' }|`Where-Object -FilterScript {($_.LastWriteTime -lt $7day) -and ` ($_.PsISContainer -ne $True)} |
Select-Object FullName|
ForEach-Object {delfiles $_.FullName}
}
else
{
Get-ChildItem -Path $args -Recurse -ErrorAction SilentlyContinue -Filter *.bak |Where-Object { $_.Extension -eq '.bak' }|`Where-Object -FilterScript {($_.LastWriteTime -lt $7day) -and ` ($_.PsISContainer -ne $True)} |
Select-Object FullName|
ForEach-Object {delfiles $_.FullName}
}
} delfilelist $localdbdir
delfilelist $remotedbdir
delfilelist $downonlinefiledir

  

05-11 14:45