View Single Post
Old 06-21-2007   #4 (permalink)
Roman Kuzmin


 
 

Re: Optimizing PS Script to List directories

Here is the prototype. You may get rid of used Get-Drive or use it from
http://nightroman.spaces.live.com/bl...39FA!131.entry

##
## Author : Roman Kuzmin
## Synopsis : Get all directories
##
## Returns System.IO.DirectoryInfo objects or full names.
## -Root: root directory(s); single \\ or // denotes all physical drives.
## -Name: return full names as strings.
##
## *) requires Get-Drive.ps1.
## *) access errors are silently ignored.
## *) hidden and system items are included.
## *) reparse points directories are excluded.
##

param
(
[string[]]$Root = '.',
[switch]$Name
)

function GetDirectories([System.IO.DirectoryInfo]$di)
{
# ignore errors
trap {continue}

# directories
foreach($d in $di.GetDirectories()) {
if (!($d.Attributes -band [System.IO.FileAttributes]::ReparsePoint)) {
if ($Name) {
$d.FullName
} else {
$d
}
GetDirectories $d
}
}
}

if ($Root.Count -eq 1 -and ($Root[0] -eq '\\' -or $Root[0] -eq '//')) {
foreach($d in Get-Drive) {
GetDirectories $d.RootDirectory
}
} else {
foreach($d in $Root) {
GetDirectories $d
}
}


--
Thanks,
Roman Kuzmin

"Roman Kuzmin" <z@z.z> wrote in message
news:e72Mjb%23sHHA.4236@TK2MSFTNGP05.phx.gbl...
> You may slightly change Get-File.ps1 at
> http://nightroman.spaces.live.com/bl...39FA!131.entry so
> that it returns directories and ignores files completely.
>
> Hope this helps.
>
> --
> Thanks,
> Roman Kuzmin
>



My System SpecsSystem Spec