[英]Is there a way in Powershell to not search in multiple folders
I want to search for all files in a PC but I want to exclude some of the folders.我想搜索 PC 中的所有文件,但我想排除一些文件夹。 I'm currently using
Where-Object { ($_.FullName -notmatch $excludepath)
but the problem with this is it looks up in those paths and then filters it.我目前正在使用
Where-Object { ($_.FullName -notmatch $excludepath)
但问题是它在这些路径中查找然后过滤它。 I want my program to not look up in some paths at all because it takes up a lot of time!我希望我的程序根本不查找某些路径,因为它会占用大量时间!
Edit: This is the code I'm working with.编辑:这是我正在使用的代码。 I want to search a PC for either all the files or with some specific filters like files with specific name, extension and also give the option to exclude a path entirely.
我想在 PC 上搜索所有文件或使用某些特定过滤器(例如具有特定名称、扩展名的文件)并提供完全排除路径的选项。 This code does all that but while excluding a path it searches in the path and then filters out using
Where-Object { ($_.FullName -notmatch $excludepath)
.此代码执行所有操作,但在排除路径时会在路径中搜索,然后使用
Where-Object { ($_.FullName -notmatch $excludepath)
过滤掉。 Because C drive is so big I want my program to not look up in the certain multiple path mentioned rather than searching in them and then filtering.因为 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 trhis:试试这个:
$dirtoexclude=@(
'C:\temp\sqldeveloper\sqldeveloper\svnkit\licenses',
'C:\temp\sqldeveloper\sqldeveloper\sqldeveloper\lib' ,
'C:\temp\sqldeveloper\sqldeveloper\sqldeveloper\extensions\oracle.olap',
'C:\temp\sqldeveloper'
)
#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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.