Powershell 文件扩展名匹配 .*
Powershell file extension match .*
我需要将“.*”的扩展名与 return 给定源文件夹中 LastWriteTime 为 $lwt 的所有文件匹配,如代码所示。用户可以提供一个特定的扩展名,如“.csv”,但没有提供,那么脚本将简单地搜索所有文件。但是我无法将带有“.*”的扩展名匹配到 return 所有文件。
[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."
}
如何实现?
文件的 Extension
永远不会是 .*
。
你可以试试:
$exten = "*.$exten"
Get-ChildItem $source -ErrorAction Stop -Filter $exten | ?{$_.LastWriteTime -lt $lwt} | foreach { ... }
将您的扩展过滤器移回子项目并使用 . 或 *.
[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 -Filter $exten -ErrorAction Stop | ?{$_.LastWriteTime -lt $lwt} | foreach {
Write-Output $_.FullName
# Move-item $_.FullName $destination -ErrorAction Stop
}
}
catch{
Write-Output "Something went wrong while moving items. Aborted operation."
}
我需要将“.*”的扩展名与 return 给定源文件夹中 LastWriteTime 为 $lwt 的所有文件匹配,如代码所示。用户可以提供一个特定的扩展名,如“.csv”,但没有提供,那么脚本将简单地搜索所有文件。但是我无法将带有“.*”的扩展名匹配到 return 所有文件。
[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."
}
如何实现?
文件的 Extension
永远不会是 .*
。
你可以试试:
$exten = "*.$exten"
Get-ChildItem $source -ErrorAction Stop -Filter $exten | ?{$_.LastWriteTime -lt $lwt} | foreach { ... }
将您的扩展过滤器移回子项目并使用 . 或 *.
[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 -Filter $exten -ErrorAction Stop | ?{$_.LastWriteTime -lt $lwt} | foreach {
Write-Output $_.FullName
# Move-item $_.FullName $destination -ErrorAction Stop
}
}
catch{
Write-Output "Something went wrong while moving items. Aborted operation."
}