Skip to content

Instantly share code, notes, and snippets.

@taisyo7333
Created September 10, 2015 15:26
Show Gist options
  • Save taisyo7333/3ec092bafd849643eedc to your computer and use it in GitHub Desktop.
Save taisyo7333/3ec092bafd849643eedc to your computer and use it in GitHub Desktop.
Powershell Recursive
<#
.SYNOPSIS
.DESCRIPTION
.PARAMETER base_path
.PARAMETER max_depth
.PARAMETER depth
.EXAMPLE
Get-FolderHierarchy "C:\Windows"
.EXAMPLE
Get-FolderHierarchy "C:\Windows" 3
.EXAMPLE
Get-FolderHierarchy "C:\Windows" 3 0
.LINK
.NOTES
#>
param (
[string]$base_folder_path,
[int]$max_depth=0,
[int]$depth = 0
)
# 再帰的にフォルダ構成を調べて、フォルダ名をリストアップする。
# フォルダ階層の深さに合わせてインデントする
function SearchFolders([string]$base_path,[int]$max_depth,[int]$depth)
{
if( $max_depth -ne 0 )
{
if( $depth -ge $max_depth )
{
return;
}
}
$search = $base_path + "\*"
$folders = Get-ChildItem $search -Directory -Name
foreach ($folder in $folders )
{
$space = ' '
$padding = $space.PadLeft( $depth * 4, ' ')
Write-Output "$padding + $folder"
$child_path = $base_path + "\" + $folder
SearchFolders $child_path $max_depth ($depth + 1)
}
}
SearchFolders $base_folder_path $max_depth $depth
> .\Get-FolderHierarchy.ps1 "C:\users\ユーザー名\Music" 3
+ iTunes
+ Album Artwork
+ Cache
+ Cloud Purchases
+ Download
+ iTunes Media
+ Downloads
+ iTunes に自動的に追加
+ Music
+ Podcasts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment