なので、C# で記述する。
folder_tree.ps1
param($root_path=".\")
$provider = New-Object Microsoft.CSharp.CSharpCodeProvider
$params = New-Object System.CodeDom.Compiler.CompilerParameters
$params.GenerateInMemory = $True
$params.TreatWarningsAsErrors = $True
$refs = "System.dll","mscorlib.dll"
$params.ReferencedAssemblies.AddRange($refs)
$txtCode = '
using System;
using System.IO;
public class MyFileSystem {
public string [] SubTree(string path){
string [] list1 = null;
string [] list2 = null;
string [] list3 = null;
try{
list1 = Directory.GetFiles(path);
list2 = Directory.GetDirectories(path);
int fs = list1.Length;
Array.Resize(ref list1, fs + list2.Length);
list2.CopyTo(list1, fs);
foreach(string elm in list2){
DirectoryInfo di = new DirectoryInfo(elm);
if((di.Attributes & FileAttributes.ReparsePoint)
== FileAttributes.ReparsePoint){ continue; }
list3 = SubTree(elm);
fs = list1.Length;
Array.Resize(ref list1, fs + list3.Length);
list3.CopyTo(list1, fs);
}
}catch(System.UnauthorizedAccessException uae){
if(list1 == null){ Array.Resize(ref list1, 1); }
list1[0] = uae.Message;
Console.Write(uae.Message+"\n");
}
return list1;
}
}
'
$results = $provider.CompileAssemblyFromSource($params, $txtCode)
$results.Errors
$mAssembly = $results.CompiledAssembly
$i = $mAssembly.CreateInstance("MyFileSystem")
if(-not (Test-Path $root_path)){
Write-Host "指定されたパスが見つかりません。"
Exit 1
}
if($root_path -match "^[a-zA-Z]:$"){ $root_path = $root_path+"\" }
$i.SubTree($root_path)