我需要将扩展名匹配到“。*”才能返回给定源文件夹中所有具有$ lwt的LastWriteTime的文件,如代码中所示。用户可以提供特定的扩展名,例如“.csv”,但没有提供,那么脚本将简单搜索所有文件。但是我无法将扩展名与“。*”匹配以返回所有文件。Powershell文件扩展名匹配*
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)][string]$source,
[Parameter(Mandatory=$true)][string]$destination,
[string]$exten=".*",
[int]$olderthandays = 30
)
$lwt = (get-date).AddDays(-$olderthandays)
if(!(Test-Path $source)){
Write-Output "Source directory does not exist."
exit
}
if(!(Test-Path $destination)){
Write-Output "Source directory does not exist."
exit
}
if($olderthandays -lt 0){
Write-Output "You can't provide a negative number of days. If you want everything deleted simply provide 0 for number of days."
exit
}
if(!$exten.StartsWith(".")){
$exten = "."+$exten
$exten
}
try{
Get-ChildItem $source -ErrorAction Stop | ?{$_.LastWriteTime -lt $lwt -AND $_.Extension -eq $exten} | foreach {
Write-Output $_.FullName
# Move-item $_.FullName $destination -ErrorAction Stop
}
}
catch{
Write-Output "Something went wrong while moving items. Aborted operation."
}
这怎么能实现?
“扩展名匹配'。*'”是什么意思?那么,比如,“/Users/foo/Documents/bar.*”的文件名就会匹配?或者别的,因为“。*”在正则表达式中非常有意义。 – RamenChef
我的意思是说任何扩展名就像.csv,.pdf或任何其他扩展名。 – Junaid