【发布时间】:2021-01-26 15:56:47
【问题描述】:
我对 Powershell 和一般编程相当陌生。我想使用具有多个条件的 Powershell 搜索文件。我已经设法编写了这段代码
$Drives = Get-PSDrive -PSProvider 'FileSystem'
$Filename= 'Result'
$IncludeExt= '*csv,*docx'
$StartDate= '11/1/20'
$EndDate= '1/26/21'
Get-ChildItem -Path $Drives.Root -Recurse |Where-Object {$IncludeExt -match $_.Extension} | Where-Object { $_.BaseName -match $Filename} | Where-Object {$_.lastwritetime -ge $StartDate -AND $_.lastwritetime -le $EndDate} |
foreach{
$Item = $_.Basename
$Path = $_.FullName
$Type = $_.Extension
$Age = $_.CreationTime
$Path | Select-Object `
@{n="Name";e={$Item}},`
@{n="Created";e={$Age}},`
@{n="filePath";e={$Path}},`
@{n="Folder/File";e={if($Folder){"Folder"}else{$Type}}}`
}| Export-Csv D:\FFNew.csv -NoTypeInformation
当提到所有变量时,这很有效。但是当
案例 1:如果 $Filename 为空,则给出所有具有上述扩展名的文件和在日期范围内修改的文件
Case2:如果 $IncludeExt 留空,那么它会给出所有提到 $Filename 的文件,目前它只给出在日期范围内修改的文件夹和文件
案例 3:如果 $Filename 和 $IncludeExt 为空,它会给出在 $StartDate 和 $EndDate 之间修改的所有文件
【问题讨论】:
标签: powershell multiple-conditions file-search