【问题标题】:Is there a way in Powershell to not search in multiple foldersPowershell中有没有办法不在多个文件夹中搜索
【发布时间】:2021-03-03 09:51:05
【问题描述】:

我想搜索 PC 中的所有文件,但我想排除一些文件夹。 我目前正在使用Where-Object { ($_.FullName -notmatch $excludepath),但问题是它在这些路径中查找然后过滤它。我希望我的程序根本不查找某些路径,因为它会占用大量时间!

编辑:这是我正在使用的代码。我想在 PC 上搜索所有文件或使用某些特定过滤器(例如具有特定名称、扩展名的文件)并提供完全排除路径的选项。这段代码做了所有这些,但在排除路径的同时,它在路径中搜索,然后使用 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 

【问题讨论】:

  • 哪些路径?也许向您展示当前的代码并描述它有什么问题

标签: powershell file search


【解决方案1】:

尝试一下:

$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
}

}

【讨论】:

  • 嘿!感谢您的解决方案,但您能否告诉我您提供的代码中发生了什么以及如何将其集成到我的程序中。我是 powershell 的初学者,所以我无法真正理解发生了什么!
  • 我对 powershell 也比较陌生,但通过使用Powershell learning resources 上列出的资源,我很快就能跟上进度。我会从videos on channel9
猜你喜欢
  • 2017-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-12
  • 1970-01-01
  • 2021-08-02
  • 2012-09-22
  • 2019-09-27
相关资源
最近更新 更多