- Muslim Wookie
- Jul 6, 2005
-
|
I have what I would guess to be an ultra-easy Powershell question that I'm just slammed for time on so I'm asking instead of trying to work it out myself...
I need to get a count of all sub-subdirectories in a folder and no files. I.e.
C:\Temp
-> Folder1
--> FolderA
--> FolderB
--> FolderC
-> Folder2
-> Folder3
--> FolderD
Total would be 4, it's counting FolderA, FolderB, FolderC, and FolderD while ignoring the folders above it and any files encountered.
|
#
¿
Nov 1, 2010 09:31
|
|
- Adbot
-
ADBOT LOVES YOU
|
|
#
¿
May 4, 2024 13:37
|
|
- Muslim Wookie
- Jul 6, 2005
-
|
Victor posted:
Get-ChildItem -Recurse | ? { $_ -is [System.IO.DirectoryInfo] }
Thanks, I'd almost gotten this far myself but this doesn't stop after one level of sub-directories. It recurses all the way down the tree. Is it possible to tell Powershell how many levels of recursion to do?
Either way, this will help heaps in that while I can't necessarily | measure-object the output is still useful.
|
#
¿
Nov 1, 2010 09:47
|
|
- Muslim Wookie
- Jul 6, 2005
-
|
Victor posted:
This should do ya:
code:Get-ChildItem |
? { $_ -is [System.IO.DirectoryInfo] } |
% { Get-ChildItem $_ } |
? { $_ -is [System.IO.DirectoryInfo] }
(To those better at PowerShell, I'd love to know if there are better ways to do the above.)
Mate you are a lifesaver, that's done the trick nicely. If you have some time, would you mind explaining what each piece is doing, it seems like something I should have been able to quickly get my head around but for some reason haven't.
I did also find this bit of code elsewhere:
code:1.function Get-ChildItemRecurse {
2.<#
3..Synopsis
4. Does a recursive search through a PSDrive up to n levels.
5..Description
6. Does a recursive directory search, allowing the user to specify the number of
7. levels to search.
8..Parameter path
9. The starting path.
10..Parameter fileglob
11. (optional) the search string for matching files
12..Parameter levels
13. The numer of levels to recurse.
14..Example
15. # Get up to three levels of files
16. PS> Get-ChildItemRecurse *.* -levels 3
17.
18..Notes
19. NAME: Get-ChildItemRecurse
20. AUTHOR: tojo2000
21.#Requires -Version 2.0
22.#>
23. Param([Parameter(Mandatory = $true,
24. ValueFromPipeLine = $false,
25. Position = 0)]
26. [string]$path = '.',
27. [Parameter(Mandatory = $false,
28. Position = 1,
29. ValueFromPipeLine = $false)]
30. [string]$fileglob = '*.*',
31. [Parameter(Mandatory = $false,
32. Position = 2,
33. ValueFromPipeLine = $false)]
34. [int]$levels = 0)
35.
36. if (-not (Test-Path $path)) {
37. Write-Error "$path is an invalid path."
38. return $false
39. }
40.
41. $files = @(Get-ChildItem $path)
42.
43. foreach ($file in $files) {
44. if ($file -like $fileglob) {
45. Write-Output $file
46. }
47.
48. #if ($file.GetType().FullName -eq 'System.IO.DirectoryInfo') {
49. if ($file.PSIsContainer) {
50. if ($levels -gt 0) {
51. Get-ChildItemRecurse -path $file.FullName `
52. -fileglob $fileglob `
53. -levels ($levels - 1)
54. }
55. }
56. }
57.}
|
#
¿
Nov 1, 2010 11:20
|
|