brs
brs

Reputation: 11

List directories which do not contain a particular file

I am trying to list all the sub-directories which do not contain a file that matches *Quick*install*.pdf.

I have tried the below script but it is listing all the directories.

$a = "F:\Test"

Get-ChildItem -Path $a -recurse *Documents* | Foreach {
    Get-ChildItem -Path $a -recurse | where {$_.psiscontainer} | % {
        if((Get-ChildItem -Path $_.FullName -File).name -notcontains "*Quick*install*"){ 
            $_.FullName 
        }
    }
}

Upvotes: 1

Views: 189

Answers (3)

Esperento57
Esperento57

Reputation: 17472

try this

$Base = "F:\Test"
$Search= "*Quick*install*.pdf"

$dirtoexclude=Get-ChildItem $Base -Recurse -File -Filter $Search | select Directory
Get-ChildItem $Base -Recurse -Directory | where FullName -Notin $dirtoexclude.Directory.FullName

Upvotes: 0

user6811411
user6811411

Reputation:

Assuming you only want to process folders which contain Documents in the name

$Base = "F:\Test"
$Search= "*Quick*install*.pdf"

Get-ChildItem -Path $Base -Recurse -Directory -Filter "*Documents*" | 
  Where {(Get-ChildItem -Path $_.FullName -File -Filter $Search).Count -eq 0}|
    Select-Object -Expandproperty FullName

Upvotes: 1

AdamL
AdamL

Reputation: 13161

Your script will list all directories, that contains any files that are not QuickInstall, even if they contain QuickInstall itself. Also, it'll return directory FullName multiple times for every file. Try this instead:

Get-ChildItem *documents* -Directory | ForEach-Object {
  if (-not (ls $_.FullName -file -Recurse *Quick*Install*)) {
    write-output $_.FullName
  }
}

Upvotes: 1

Related Questions