1

The closest I got was using powershell Get-ChildItem given multiple -Filters

Get-ChildItem -Include "Intro", "*.mp4" -Recurse

I think, -Include with multiple params work as OR operator. It gives folders with either "Intro" folder OR "*.mp4" files.

But I need AND condition. Folder must contain a folder named "Intro" and "*.mp4" files.

I need folders structured following -

E:.
└───test1.mp4
└───test2.mp4
└───test3.mp4
└───test4.mp4
└───test5.mp4
└───test6.mp4
└───Intro

Update 1

I am searching for folders which meet two condition.

  1. It must have a subfolder named Intro AND

  2. It must have *.mp4 files.

The answer would look something like the following I guess.

Get-ChildItem -Directory -Recurse {HasSubFolderNamedIntro && HasMP4Files}

2 Answers 2

3

Just wanted to add another Method (one-liner) to get the folder which contains mp4 files and a folder Intro (probs to @HenrikStanleyMortensen for most of it):

(Get-ChildItem -include "*.mp4" -Recurse -File).DirectoryName | Select-Object -Unique | Where-Object {(Get-ChildItem $_ -Recurse -Include 'Intro' -Directory)}
3

Do you need the command to only return the file objects or do you also want the folder objects? If just the files I would do it like this:

Get-ChildItem -include "*.mp4" -Recurse -File | Where-Object {$_.Directory.Name -match 'Intro'}

So we use the include to find the mp4 files and reduce the amount of objects we pipe. Then we pipe it to Where-Object and look for the property with the name of the folder and says we want it to contain the word "intro". If the folder needs to be called Intro exactly and not just contain it you can change the -match to -eq

Edit
To get the directories then we could do it like this:

(Get-ChildItem -include "*.mp4" -Recurse | Where-Object {$_.Directory.Name -match 'Intro'}).DirectoryName | Select-Object -Unique

Now we say that all the files that we found that matches our search, we want to see the full directory path of those files.

To only get one match per directory, so if we have 1 directory that matches with multiple mp4 files, and we don't want to see that same directory in our output one time per file, we can pipe the result into Select-Object -Uniqueto only see each directory once.

Edit 2
After clarification from OP.
To find a folder that contains both mp4 files and a subfolder called intro I don't think we can do that only from the Get-ChildItem command in any way I know of, so we can loop through each folder like this:

$Files = (Get-ChildItem -include "*.mp4" -Recurse -File).DirectoryName  | Select-Object -Unique

foreach($File in $Files) {
Get-ChildItem -Path $File.DirectoryName -Recurse -Include 'Intro' -Directory
}

We Pipe to the Select-Object -Unique to make sure that folders with multiple mp4 files are not looped through more than once thus giving us an output with the same intro folder multiple times.

7
  • I have updated my answer to show how you would from that output find the folders . Jul 26, 2018 at 11:15
  • Any chance you could clarify our question then? There must be something lost in communication. As I understand it you want the following.. A way to find all folders that is called intro and contains mp4 files. If this is not the case, could you with words try to describe the output you expect? Jul 26, 2018 at 11:21
  • I am searching for folders which meet two condition. 1. It must have a subfolder named "Intro" AND 2. It must have *.mp4 files. Your updated solution return folders named "Intro" which has *.mp4 files. Jul 26, 2018 at 11:21
  • 1
    @HenrikStanleyMortensen I Updated the question. Thanks for being helpful though I was sort of direct to you. Jul 26, 2018 at 11:31
  • 1
    No problem, this is a community for helping each other :) I have updated my answer so we now first look for folders with mp4 files, then we check if the folders that the mp4 files also have a folder in them called intro. Jul 26, 2018 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.