5

First post on the platform ;-)

Scenario: I have a root folder that contains subfolders with setup related files. Some subfolders contain more subfolders with more files, subsubfolders. Each of the subfolders underneath the root folder, or their subfolders, MUST include one specific file.

Now I want to get all subfolders directly located underneath the root folder THAT DO NOT INCLUDE this specific file, neither in the subfolder itself, nor in other subfolders below. I want only the names of the subfolders underneath the root. The search should be recursevely, of course.

My current code looks like that:

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

Unfortunately the result is very huge and it includes all files in every subfolder, too. My wish: Only get the names of the subfolders underneath the root to check, wy that specific file is not there.

2 Answers2

3

Welcome to stackoverflow!

You are almost there. Here a solution where I first retrieve all subfolders of $softwarePath and then select only the one which not contains the file:

Get-ChildItem $softwarePath -Directory | 
? { -not (Get-ChildItem $_.FullName -Recurse -File -Filter 'Prefix_*.cmd')}
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
2

While @jisaak's approach solves your problem perfectly, you might find that the overhead of retrieving FileInfo objects for each file in you huge directory structure might cause the script to take some time to execute.

If this is the case, you can speed up the process by retrieving just the filename with the Directory.GetFiles() method:

Get-ChildItem $softwarePath -Directory |Where-Object { 
    -not ([System.IO.Directory]::GetFiles($_.FullName,'Prefix_*.cmd',"AllDirectories"))
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Thanks for this smart approach. In fact, it is a little bit faster! –  Aug 06 '15 at 13:04