Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Sunday, September 4, 2016 8:01 PM
Hi,
Another question... I would like to be able to count when nested subdirectories are > x levels deep.
e.g. if I run this over my folder of audiobooks I would get.
..\author\bookname\chapter1.mp3 would give a result of "2",
but
..\author\chapter1.mp3 would give a result of "1"
and
..\author\seriesname\bookname\chapter1.mp3 would give a result of 3
.. so basically nesting deeper recursively through the subdirectories and then counting how deep each one goes in an array or something until it hits bottom, then jumping up to the next one etc.
Any ideas? I can generally do work with get-childitems but I can't quite get my brain around this topic
Thanks,
Gavin
All replies (4)
Monday, September 5, 2016 6:45 AM ✅Answered
( $path.ToCharArray() | ? { $_ -eq "\" } | measure ).count - 1
Gleb.
Monday, September 5, 2016 10:44 PM ✅Answered
Function Recurse-Folders([string]$path, $Depth=($Path.ToCharArray() | ? { $_ -eq "\" } | measure ).count) {
Set-Location -Path $Path
$CurrentFolder = Get-Location
Write-Host "Depth=$Depth $CurrentFolder"
[String[]]$Folders = Get-ChildItem "." -Directory
ForEach ($Folder in $Folders)
{
$Depth++
Recurse-Folders $Folder $Depth
Set-Location -Path ".."
$Depth--
}
}
<# Make a sample folder structure
MD C:\Test
MD C:\Test\Folder1
MD C:\Test\Folder1\SubFolder1
MD C:\Test\Folder1\SubFolder2
MD C:\Test\Folder2\SubFolder1
MD C:\Test\Folder2\SubFolder2
#>
Recurse-Folders "C:\Test"
Gives this output (using Gleb's suggestion for initial depth)
Depth=1 C:\Test
Depth=2 C:\Test\Folder1
Depth=3 C:\Test\Folder1\SubFolder1
Depth=3 C:\Test\Folder1\SubFolder2
Depth=2 C:\Test\Folder2
Depth=3 C:\Test\Folder2\SubFolder1
Depth=3 C:\Test\Folder2\SubFolder2
Sunday, September 4, 2016 8:59 PM
You are asking arbitrary statements. Please show your script and as a related quesiotn. Wee cannot guess at what you are asking.
\(ツ)_/
Monday, September 5, 2016 6:48 AM
Hi Gavin,
Based on my understanding, something might be like this:
PS C:\share\1\2> $test = Get-ChildItem C:\share -Recurse
PS C:\share\1\2> $test.pspath
Microsoft.PowerShell.Core\FileSystem::C:\share\1
Microsoft.PowerShell.Core\FileSystem::C:\share\1\2
Microsoft.PowerShell.Core\FileSystem::C:\share\1\2\3
PS C:\share\1\2> $test.pspath | Measure-Object
Count : 3
Average :
Sum :
Maximum :
Minimum :
Property :
PS C:\share\1\2> $test.pspath | Measure-Object | select count
Count
3
If you need more assistances, please feedback here and giving us your script if possible.
Best regards,
Andy_Pan
Please remember to mark the replies as an answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft..com.