atilas1
atilas1

Reputation: 441

PowerShell: Finding folders with no file in subfolders

Question rewriten, due to clarity issues and not being able to delete it.

I have script that creates folder tree based on 7 number code. Lets assume our number is YYYYYYY then my folder hierarchy would be something like (number shows "layer" of folder) (sorry for calling it layers, as I don't know proper terminology)

  1. YYYYYxx (might have up to 100 folders for different projects)
  2. YYYYYYY (every folder in this layer has same structer deeper as it always consists of single project)
  3. Subfolder A
  4. This "layer" has two folders
  5. This "layer" has project files

"Layers" 1-3, has no files ever. Files might be only in layer 5. Now I need to make script that would show in which projects (2 layer) has no files, only empty folders.

Upvotes: 0

Views: 1243

Answers (2)

Moerwald
Moerwald

Reputation: 11254

Since I'm not sure if "empty folders" are folders without subdirectories or only folders without files, I posted another query possibility, that also regards subfolders in folders. This should return all empty directories:

dir \*\* | ? { $_.PsisContainer } | ? {$_.GetFiles().Count -eq 0 -and $_.GetDirectories().Count -eq 0}

GetFiles checks for files in the directory, and GetDirectories checks for subdirectories.

Upvotes: 0

woxxom
woxxom

Reputation: 73586

PowerShell 3+ has -Directory and -File parameters of Get-Child-Item.

$L1withEmptyL2subfolder = dir $startFolder\*\* -Directory |
    Where { !(dir $_ -Recurse -File | select -first 1) }

Pipelining is used to stop processing if even one file exists without enumerating the entire structure.

Upvotes: 2

Related Questions