67

Pro/Engineer CAD システムの構成ファイルを生成する必要があります。サーバー上の特定のドライブからのフォルダーの再帰的なリストが必要です。ただし、さまざまなケースを含め、「ARCHIVE」が含まれるフォルダーはすべて除外する必要があります。

フォルダーを除外しないことを除いて機能する次のように書きました!!

$folder = "T:\Drawings\Design\*"
$raw_txt = "T:\Design Projects\Design_Admin\PowerShell\raw.txt"
$search_pro = "T:\Design Projects\Design_Admin\PowerShell\search.pro"
$archive = *archive*,*Archive*,*ARCHIVE*

Get-ChildItem -Path $folder -Exclude $archive -Recurse  | where {$_.Attributes -match 'Directory'}  | ForEach-Object {$_.FullName} > $search_pro   
4

18 に答える 18

61

いくつかのフォルダーをスキップするための私のKISSGet-ChildItemのアプローチは、通話の連鎖です。これにより、ルート レベルのフォルダーは除外されますが、より深いレベルのフォルダーは除外されます。

Get-ChildItem -Exclude folder1,folder2 | Get-ChildItem -Recurse | ...
  • 不要なフォルダーの除外を開始します
  • 次に、不要なフォルダーを除外して再帰検索を実行します。

このアプローチの好きなところは、シンプルで覚えやすいことです。最初の検索でフォルダーとファイルを混在させたくない場合は、フィルターが必要になります。

于 2017-02-02T16:21:58.793 に答える
52

私は次のようにします:

Get-ChildItem -Path $folder -r  | 
? { $_.PsIsContainer -and $_.FullName -notmatch 'archive' }

-notmatch正規表現を受け入れることに注意してください。

https://docs.microsoft.com/powershell/module/microsoft.powershell.core/where-object#parameters

于 2013-03-08T13:31:25.893 に答える
48

この回答が以前の回答と重複しているように思われる場合は、お詫び申し上げます。これを解決する更新された(POSH 5.0でテストされた)方法を示したかっただけです。以前の回答は 3.0 より前のものであり、最新のソリューションほど効率的ではありませんでした。

ドキュメントはこれについて明確ではありませんが、親パス ( ) ではなく、Get-ChildItem -Recurse -Excludeリーフ ( ) の除外のみに一致します。除外を一致させると、葉が一致するアイテムが削除されるだけです。その葉にまだ再帰します。Split-Path $_.FullName -LeafSplit-Path $_.FullName -ParentGet-ChildItem

POSH 1.0 または 2.0 で

Get-ChildItem -Path $folder -Recurse  | 
          ? { $_.PsIsContainer -and $_.FullName -inotmatch 'archive' }

注: @CB と同じ答え。

POSH 3.0+ で

Get-ChildItem -Path $folder -Directory -Recurse  | 
          ? { $_.FullName -inotmatch 'archive' }

注: @CB からの回答を更新しました。

複数の除外

これは、パラメーターでリーフを除外し、 (大文字と小文字を区別しない like) 比較Excludeで親を除外しながら、ディレクトリを明確にターゲットにします。ilike

#Requires -Version 3.0
[string[]]$Paths = @('C:\Temp', 'D:\Temp')
[string[]]$Excludes = @('*archive*', '*Archive*', '*ARCHIVE*', '*archival*')

$files = Get-ChildItem $Paths -Directory -Recurse -Exclude $Excludes | %{ 
    $allowed = $true
    foreach ($exclude in $Excludes) { 
        if ((Split-Path $_.FullName -Parent) -ilike $exclude) { 
            $allowed = $false
            break
        }
    }
    if ($allowed) {
        $_
    }
}

注:$Excludes大文字と小文字を区別する場合は、次の 2 つの手順があります。

  1. Excludeからパラメータを削除しますGet-ChildItem
  2. 最初のif条件を次のように変更します。
    • if ($_.FullName -clike $exclude) {

注:このコードには、本番環境では決して実装しない冗長性があります。正確なニーズに合わせて、これをかなり単純化する必要があります。これは詳細な例として役立ちます。

于 2016-03-14T16:56:28.523 に答える
15

除外パターンでは大文字と小文字が区別されないため、すべてのケースを除外対象として指定する必要はありません。

つまり、-Excludeパラメーターは文字列の配列を受け入れるため、そのように定義する限り、$archive設定する必要があります。

$archive = ("*archive*","*Archive*","*ARCHIVE*");

また、末尾のアスタリスクを削除する必要があり$folderます - を指定-recurseしているため、最上位フォルダーのみを指定する必要があります。

$folder = "T:\Drawings\Design\"

完全に改訂されたスクリプト。これにより、ディレクトリが見つかったかどうかを検出する方法も変更されForeach-Object、プロパティを直接プルしてすべてをファイルにダンプできるため、スキップされます。

$folder = "T:\Drawings\Design\";
$raw_txt = "T:\Design Projects\Design_Admin\PowerShell\raw.txt";
$search_pro = "T:\Design Projects\Design_Admin\PowerShell\search.pro";
$archive = ("*archive*","*Archive*","*ARCHIVE*");

Get-ChildItem -Path $folder -Exclude $archive -Recurse  | where {$_.PSIsContainer}  | select-Object -expandproperty FullName |out-file $search_pro 
于 2013-03-08T13:33:01.983 に答える
8

これがかなり古いことは知っていますが、簡単な解決策を探していると、このスレッドに出くわしました...質問が正しければ、Get-ChildItemを使用して複数のディレクトリを一覧表示する方法を探していました。powershell 5.0 を使用するもっと簡単な方法があるようです - 例

Get-ChildItem -Path D:\ -Directory -Name -Exclude tmp,music
   chaos
   docs
   downloads
   games
   pics
   videos

-Exclude 句がなければ、tmp と music は引き続きそのリストに含まれます。-Name を使用しない場合、Get-ChildItem の詳細な出力のため、-Exclude 句は機能しません。これが、特定のディレクトリ名を除くすべてのディレクトリ名を簡単に一覧表示する方法を探している一部の人々に役立つことを願っています。

于 2016-10-17T17:46:59.393 に答える
3

必要なファイルが除外するフォルダーと同じ名前を持っていないと仮定すると、正規表現の「または」記号のように除外できます。

$exclude = 'dir1|dir2|dir3'
ls -r | where { $_.fullname -notmatch $exclude }

ls -r -dir | where fullname -notmatch 'dir1|dir2|dir3'
于 2017-08-08T19:20:37.950 に答える
2

VertigoRay は、彼の回答で、 -Exclude はパスのリーフ レベルでのみ機能すると説明しました (ファイルの場合はパスが取り除かれたファイル名、サブディレクトリの場合はパスが取り除かれたディレクトリ名)。そのため、-Exclude を使用してディレクトリ (「bin」など) を指定し、そのディレクトリ内のすべてのファイルとサブディレクトリを除外することはできません。

これは、1つ以上のディレクトリのファイルとサブディレクトリを除外する機能です(これが質問に直接答えていないことはわかっていますが、-Excludeの制限を回避するのに役立つかもしれないと思いました):

$rootFolderPath = 'C:\Temp\Test'
$excludeDirectories = ("bin", "obj");

function Exclude-Directories
{
    process
    {
        $allowThrough = $true
        foreach ($directoryToExclude in $excludeDirectories)
        {
            $directoryText = "*\" + $directoryToExclude
            $childText = "*\" + $directoryToExclude + "\*"
            if (($_.FullName -Like $directoryText -And $_.PsIsContainer) `
                -Or $_.FullName -Like $childText)
            {
                $allowThrough = $false
                break
            }
        }
        if ($allowThrough)
        {
            return $_
        }
    }
}

Clear-Host

Get-ChildItem $rootFolderPath -Recurse `
    | Exclude-Directories

ディレクトリ ツリーの場合:

C:\Temp\Test\
|
├╴SomeFolder\
|  |
|  └╴bin (file without extension)
|
└╴MyApplication\
  |
  ├╴BinFile.txt
  ├╴FileA.txt
  ├╴FileB.txt
  |
  └╴bin\
    |
    └╴Debug\
      |
      └╴SomeFile.txt

結果は次のとおりです。

C:\Temp\Test\
|
├╴SomeFolder\
|  |
|  └╴bin (file without extension)
|
└╴MyApplication\
  |
  ├╴BinFile.txt
  ├╴FileA.txt
  └╴FileB.txt

bin\ サブフォルダーとそのすべてのコンテンツを除外しますが、ファイル Bin.txt または bin (拡張子なしの「bin」という名前のファイル) は除外しません。

于 2016-11-02T22:30:07.813 に答える
2

私にとって最も単純な短い形式は次のようなものです:

#find web forms in my project except in compilation directories
(gci -recurse -path *.aspx,*.ascx).fullname -inotmatch '\\obj\\|\\bin\\'

さらに複雑なロジックが必要な場合は、フィルターを使用します。

  filter Filter-DirectoryBySomeLogic{
  
      param(
      [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
      $fsObject,
      [switch]$exclude
      )
          
      if($fsObject -is [System.IO.DirectoryInfo])
      {
          $additional_logic = $true ### replace additional logic here
  
          if($additional_logic){
              if(!$exclude){ return $fsObject }
          }
          elseif($exclude){ return $fsObject }
      }
          
  }
  
  gci -Directory -Recurse | Filter-DirectoryBySomeLogic | ....
于 2020-09-02T20:41:46.647 に答える
0
$CurrentPath = (Get-Location -PSProvider FileSystem).ProviderPath # Or your favorite path
$IncludeNames = "okFolder1", "okFolder2"  # Items names to search
$ExcludeNames = "koFolder1", "koFolder2"  # Items names not to search
$depth = 3                                # Max level of depth to search

$FoldersToRemove = Get-ChildItem .\ -include $IncludeNames -Recurse -Depth $depth 
-Attributes D                             # If you want only directories, change it as you desire
| ? { $_.fullname -inotmatch ($ExcludeNames -join '|') }  #case insensitive or use -cnotmatch for case sensitive comparison
| foreach {$_.fullname}                   # If you want to project only full path information of matches
于 2021-04-01T14:14:33.317 に答える