Re: List Directories
This will be available in the new release:
https://winscp.net/tracker/1356
https://winscp.net/tracker/1356
Before posting, please read how to report bug or request support effectively.
Bug reports without an attached log file are usually useless.
function Get-WinScpChildItem ($WinScpSession, $BasePath) {
#Get the directory listing
$Directory = $WinScpSession.ListDirectory($BasePath)
#Initialize an empty collection
$MyCollection = @()
#Browse through each entry
foreach ($DirectoryEntry in $Directory.Files) {
#We want to ignore . and .. folders
if (($DirectoryEntry.Name -ne '.') -and ($DirectoryEntry.Name -ne '..')) {
#Initialize an object to which we will add custom properties. You can add all properties you may need later such as Last Write Time etc
$TempObject = New-Object System.Object
#If the current entry is a directory, we need to add it to our collection, as a directory, then recurse
if ($DirectoryEntry.IsDirectory) {
#We need to save the BasePath before recursing
$SavePath = $BasePath
#Special case : Avoid adding an extraneous '/' if we are at the root directory
if ($BasePath -eq '/') {
$BasePath += "$($DirectoryEntry.Name)"
}
else {
$BasePath += "/$($DirectoryEntry.Name)"
}
$TempObject | Add-Member -MemberType NoteProperty -name 'Name' -Value $BasePath
$TempObject | Add-Member -MemberType NoteProperty -name 'IsDirectory' -Value $true
$MyCollection += $TempObject
#Now we recurse, and when done, we reset the BasePath
$MyCollection += Get-WinScpChildItem $WinScpSession $BasePath
$BasePath = $SavePath
}
#If the current entry is a file, it's time to add it to our collection, as a file
else {
$TempObject | Add-Member -MemberType NoteProperty -name 'Name' -Value "$BasePath/$DirectoryEntry"
$TempObject | Add-Member -MemberType NoteProperty -name 'IsDirectory' -Value $false
$MyCollection += $TempObject
}
}
}
#The function finally returns the collection
return $MyCollection
}
Session.ListDirectory()
recursively.