Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell - Exclude folders in Get-ChildItem

Tags:

powershell

How to exclude folders ? Now I hardcode the folder names but i want it to be more flexible.

foreach($file in Get-ChildItem $fileDirectory -Exclude folderA,folderb)
like image 626
user664481 Avatar asked Mar 04 '16 06:03

user664481


People also ask

How do I Get-ChildItem to exclude folders?

To exclude directories, use the File parameter and omit the Directory parameter, or use the Attributes parameter. To get directories, use the Directory parameter, its "ad" alias, or the Directory attribute of the Attributes parameter.

What is Get-ChildItem in PowerShell?

Description. The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the Recurse parameter to get items in all child containers and use the Depth parameter to limit the number of levels to recurse.

How do I get a list of directories in PowerShell?

To display the directory content, Get-ChildItem cmdlet is used. You need to provide the path of the directory or if you are in the same directory, you need to use only Get-ChildItem directly. In the below example, we need to display the contents of the D:\temp directory.


3 Answers

"How to exclude folders ?" , if you mean all folders :

get-childitem "$fileDirectory\\*" -file 

but it works only for the first level of $fileDirectory . This works recursevly :

Get-ChildItem "$fileDirectory\\*"  -Recurse | ForEach-Object { if (!($_.PSIsContainer)) { $_}}

or

 Get-ChildItem "$fileDirectory\\*"  -Recurse | where { !$_.PSisContainer }
like image 73
dvjz Avatar answered Nov 15 '22 07:11

dvjz


You can do this by using the pipeline and a Where-Object filter.

First of all, the idiomatic way to iterate over a group of files in PowerShell is to pipe Get-Childitem to Foreach-Object. So rewriting your command gets:

Get-ChildItem $fileDirectory | foreach {
   $file = $_
   ...
}

The advantage of using the pipeline is that now you can insert other cmdlets in between. Specifically, we use Where-Object to filter the list of files. The filter will pass on a file only if it isn't contained in a given array.

$excludelist = 'folderA', 'folderB'
Get-Childitem $fileDirectory | 
  where { $excludeList -notcontains $_ } |
  foreach {
    $file = $_
    ...
  }

If you're going to use this a lot, you can even write a custom filter function to modify the list of files in an arbitrary way before passing to foreach.

filter except($except, $unless = @()) {
  if ($except -notcontains $_ -or $unless -contains $_ ){
    $_ 
  }
}

$excludelist = 'folderA', 'folderB'
$alwaysInclude = 'folderC', 'folderD'
Get-ChildItem $fileDirectory |
  except $excludeList -unless $alwaysInclude | 
  foreach {
    ...
  }
like image 40
Ryan Bemrose Avatar answered Nov 15 '22 07:11

Ryan Bemrose


@dvjz said that -file works only in the first level of a folder, but not recursively. But it seems to work for me.

get-childitem "$fileDirectory\\*" -file -recurse
like image 25
Peter Constable Avatar answered Nov 15 '22 09:11

Peter Constable