Topic "List Directories"

Author Message
thernandez
[View user's profile]

Joined: 2014-10-03
Posts: 1
Is there a way to list all of the directories and files, sub-directories and sub-files within an given ftp folder? The sub-directory could possibly have more directories and more files within it as well. I know there is a Session.ListDirectory() function but this only gets the files\directories of a given folder. I was hoping there was some sort of function as in powershell's function "get-childitem -recurse" which will list everything in a given directory.

Thanks.
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
There's no one-line solution for this. You have to use Session.ListDirectory() recursively.
_________________
Martin Prikryl
DaPlane

Guest


A bit late, but if it helps, the function below is working for me.

Takes your winscp session as first parameter, and the path you want to recurse.
Returns an object containing your data. You want to enrich this object with additional properties I did not include here...

Code:
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
}
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
This will be available in the new release:
http://winscp.net/tracker/show_bug.cgi?id=1356
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License