更新
感谢Mathias提供的答案,我能够创建这个脚本,这是FAR更简洁,并向我介绍了PSBoundParamater的概念
Param (
[Parameter(Mandatory=$true)]
[string]$Path,
[Parameter(Mandatory=$true)]
[int]$Days,
[Parameter(Mandatory=$false)]
[switch]$Recurse
)
# Define the age threshold
$timespan = New-TimeSpan -Days $days -Hours 0 -Minutes 0
# Remove the Days parameter from the input arguments
$PSBoundParameters.Remove('Days')
# Splat the remaining arguments and filter out files newer than $Days
Get-ChildItem @PSBoundParameters -File|Where-Object {((Get-Date) - $_.LastWriteTime) -gt $timespan} |%{ Remove-Item $_.FullName }
我在Powershell中编写了以下脚本,目的是删除目录中的所有文件,这些文件超过几天。用户可以选择应用-recursive
开关以包含所有子文件夹:
# CMD input command:
# .\ScriptName.ps1 -path '\\Path\to\files\here' -days x
#
# E.G:
# .\DeleteFiles.ps1 -path '\\file\IT\SK\scripts' -days 1 -recursive
Param (
[parameter(Mandatory=$true)]
[string]$path,
[parameter(Mandatory=$true)]
[int32]$days,
[parameter(Mandatory=$false)]
[switch]$recursive
)
# Set the timespan based on $days entered
$timespan = New-TimeSpan -Days $days -Hours 0 -Minutes 0
######################################################################################
# Delete-Files deletes all the files in the specified directory only
######################################################################################
function Delete-Files {
Param (
[parameter(Mandatory=$true)]
[string]$path,
[parameter(Mandatory=$true)]
[TimeSpan]$timespan
)
# | pipes the output and ` allows multiline command
Get-ChildItem $path | `
Foreach-Object{
$lastmodified = $_.LastWriteTime
$fullname = $_.FullName
If (((Get-Date) - $lastmodified) -gt $timespan) {
Remove-Item $fullname
}
}
}
######################################################################################
# Delete-All deletes all files frmo the specified directory and all subdirectories
######################################################################################
function Delete-All {
Param (
[parameter(Mandatory=$true)]
[string]$path,
[parameter(Mandatory=$true)]
[TimeSpan]$timespan
)
Get-ChildItem -Path $path -Include *.* -File -Recurse | `
foreach {
$lastmodified = $_.LastWriteTime
#$fullname = $_.FullName
If (((Get-Date) - $lastmodified) -gt $timespan) {
$_.Delete()
}
}
}
######################################################################################
# Main
######################################################################################
If ($recursive) {
Delete-All -path $path -timespan $timespan
}
Else {
Delete-Files -path $path -timespan $timespan
}
但是,我认为这不是解决此问题的最佳方式,因为每个选项(递归或非递归)都使用不同的方法来删除项目。
我尝试删除“删除所有”中的-Recurse
选项。方法并将其添加到Delete-Files
,但这导致根本没有删除任何文件:
function Delete-Files{
Param (
[parameter(Mandatory=$true)]
[string]$path,
[parameter(Mandatory=$true)]
[TimeSpan]$timespan
)
Get-ChildItem -Path $path -Include *.* -File | `
foreach {
$lastmodified = $_.LastWriteTime
#$fullname = $_.FullName
If (((Get-Date) - $lastmodified) -gt $timespan) {
$_.Delete()
}
}
}
这是必填项目吗?如果是这样,有人可以建议一种方法,我可以减少这个代码只包含1个方法,或2个方法调用相同的程序?
**代码顶部的示例输入**
答案 0 :(得分:2)
您可以将整个脚本主体简化为3行(如果将Get-Date
移动到Where-Object
过滤器,则为2行):
Param (
[Parameter(Mandatory=$true)]
[string]$Path,
[Parameter(Mandatory=$true)]
[int]$Days,
[Parameter(Mandatory=$false)]
[switch]$Recurse
)
# Define the age threshold
$Threshold = (Get-Date).AddDays(-$Days)
# Remove the Days parameter from the input arguments
$PSBoundParameters.Remove('Days')
# Splat the remaining arguments and filter out files newer than $Days
Get-ChildItem @PSBoundParameters -File|Where-Object {$_.LastAccessTime -lt $Threshold} |%{ Remove-Item $_.FullName }