Get-ChildItem: Show only folders that do not contain a specific file

First message on the platform; -)

Scenario: I have a root folder containing subfolders with installation related files. Some sub-folders contain more sub-folders with more files, sub-folders. Each of the subfolders under the root folder or their subfolders MUST include one specific file.

Now I want all subfolders to be right under the root folder THAT DOES NOT INCLUDE that particular file, neither in the subfolder itself, nor in other subfolders below. I only want the names of the subfolders under the root. The search must of course be recursevely.

My current code looks like this:

Get-ChildItem $softwarePath -Recurse | ? Name -notlike "Prefix_*.cmd"

      

Unfortunately, the result is very large and includes all files in each subfolder. My wish: only get the names of the subfolders under the root to check that this particular file is not there.

+3


source to share


2 answers


Welcome to stackoverflow!

You're almost there. Here is a solution where I first extract all subfolders $softwarePath

and then select only the one that does not contain the file:



Get-ChildItem $softwarePath -Directory | 
? { -not (Get-ChildItem $_.FullName -Recurse -File -Filter 'Prefix_*.cmd')}

      

+1


source


While @ jisaak's approach solves your problem perfectly, you may find that the overhead of fetching objects FileInfo

for each file in the directory structure is enormous, which can cause the script to take a while to execute.

If so, you can speed up the process by extracting only the filename with the method Directory.GetFiles()

:



Get-ChildItem $softwarePath -Directory |Where-Object { 
    -not ([System.IO.Directory]::GetFiles($_.FullName,'Prefix_*.cmd',"AllDirectories"))
}

      

+2


source







All Articles