我想搜索PC中的所有文件,但我想排除一些文件夹。我现在用的是Where-Object { ($_.FullName -notmatch $excludepath)
,但问题是它在这些路径中查找,然后过滤它。我希望我的程序根本不需要在某些路径中查找,因为它占用了很多时间!
Where-Object { ($_.FullName -notmatch $excludepath)
过滤掉。因为C盘太大了,我希望我的程序不查找提到的某些多条路径,而不是在它们中搜索然后过滤。
$Filename = "img"
$IncludeExt = "*.jpeg"
$excludepath = "^C:\Windows" ,"^C:\Program Files"
$GCIArgs = @{Path = $Drives.Root
Recurse = $True
}
If ($Null -ne $IncludeExt) {
$GCIArgs.Add("Include",$IncludeExt)
}
Get-ChildItem @GCIArgs | Where-Object { ($_.FullName -notmatch $excludepath) -and ($Ignore -notcontains $_.Extension) -and ($_.BaseName -match $Filename )} |
foreach{
$Item = $_.Basename
$Path = $_.FullName
$Type = $_.Extension
$Modified=$_.LastWriteTime
$Age = $_.CreationTime
$Type = &{if($_.PSIsContainer){"Folder"}else{$_.Extension}}
$Path | Select-Object @{n="Name";e={$Item}},
@{n="Created";e={$Age}},
@{n="filePath";e={$Path}},
@{n="Modified";e={$Modified}},
@{n="Folder/File";e={$Type}}
}| Export-Csv D:SF.csv -NoTypeInformation
try this:
$dirtoexclude=@(
'C:tempsqldevelopersqldevelopersvnkitlicenses',
'C:tempsqldevelopersqldevelopersqldeveloperlib' ,
'C:tempsqldevelopersqldevelopersqldeveloperextensionsoracle.olap',
'C:tempsqldeveloper'
)
#method 1 : if you want exclude specific directory
get-childitem "c:temp" -Recurse | where DirectoryName -notin $dirtoexclude
#method 2 : if you want exclude specific directory and sub directory
get-childitem "c:temp" -Recurse | foreach{
$Current=$_
#search if current directory element start by one of directory to exclude
$founded=$dirtoexclude | where {$Current.DirectoryName -like "$_*"} | select * -First 1
#not start by directory to exclude, send element to output
if (!$founded)
{
$Current
}
}