Differences
This shows you the differences between the selected revisions of the page.
| library_session_listdirectory 2013-05-07 | library_session_listdirectory 2026-01-16 (current) | ||
| Line 8: | Line 8: | ||
| <code vbnet *> | <code vbnet *> | ||
| - | Public Function ListDirectory(ByVal path As String) As RemoteDirectoryInfo | + | Public Function ListDirectory(path As String) As RemoteDirectoryInfo |
| </code> | </code> | ||
| Line 24: | Line 24: | ||
| | [[library_sessionremoteexception|SessionRemoteException]] | Session has failed. \\ Listing of remote directory has failed. \\ See the exception documentation for details. | | | [[library_sessionremoteexception|SessionRemoteException]] | Session has failed. \\ Listing of remote directory has failed. \\ See the exception documentation for details. | | ||
| | TimeoutException | Timeout waiting for ''winscp.com'' to respond. | | | TimeoutException | Timeout waiting for ''winscp.com'' to respond. | | ||
| + | |||
| + | ===== Remarks ===== | ||
| + | You can use ''[[library_session_enumerateremotefiles|Session.EnumerateRemoteFiles]]'' method instead, if you want to: | ||
| + | |||
| + | * List only files matching a wildcard; | ||
| + | * List the files recursively; | ||
| + | * Have references to this (''.'') and parent (''..'') directories be excluded form the listing. | ||
| ===== [[example]] Examples ===== | ===== [[example]] Examples ===== | ||
| + | |||
| ==== [[csharp]] C# Example ==== | ==== [[csharp]] C# Example ==== | ||
| <code csharp> | <code csharp> | ||
| Line 38: | Line 46: | ||
| { | { | ||
| // Setup session options | // Setup session options | ||
| - | SessionOptions sessionOptions = new SessionOptions { | + | SessionOptions sessionOptions = new SessionOptions |
| + | ···········{ | ||
| Protocol = Protocol.Sftp, | Protocol = Protocol.Sftp, | ||
| HostName = "example.com", | HostName = "example.com", | ||
| UserName = "user", | UserName = "user", | ||
| Password = "mypassword", | Password = "mypassword", | ||
| - | SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | + | SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
| }; | }; | ||
| Line 51: | Line 60: | ||
| session.Open(sessionOptions); | session.Open(sessionOptions); | ||
| - | RemoteDirectoryInfo directory = session.ListDirectory("/home/martin/public_html"); | + | RemoteDirectoryInfo directory = |
| + | ···················session.ListDirectory("/home/martin/public_html"); | ||
| foreach (RemoteFileInfo fileInfo in directory.Files) | foreach (RemoteFileInfo fileInfo in directory.Files) | ||
| { | { | ||
| - | Console.WriteLine("{0} with size {1}, permissions {2} and last modification at {3}", | + | Console.WriteLine( |
| - | fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions, fileInfo.LastWriteTime); | + | ························"{0} with size {1}, permissions {2} and last modification at {3}", |
| + | fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions, | ||
| + | ·······················fileInfo.LastWriteTime); | ||
| } | } | ||
| } | } | ||
| Line 73: | Line 85: | ||
| ==== [[vbnet]] VB.NET Example ==== | ==== [[vbnet]] VB.NET Example ==== | ||
| <code vbnet> | <code vbnet> | ||
| - | Imports System | ||
| Imports WinSCP | Imports WinSCP | ||
| Line 84: | Line 95: | ||
| Dim sessionOptions As New SessionOptions | Dim sessionOptions As New SessionOptions | ||
| With sessionOptions | With sessionOptions | ||
| - | .Protocol = Protocol.Sftp, | + | .Protocol = Protocol.Sftp |
| - | .HostName = "example.com", | + | .HostName = "example.com" |
| - | .UserName = "user", | + | .UserName = "user" |
| - | .Password = "mypassword", | + | .Password = "mypassword" |
| - | .SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | + | .SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
| End With | End With | ||
| - | Using session As Session = New Session | + | Using session As New Session |
| ' Connect | ' Connect | ||
| session.Open(sessionOptions) | session.Open(sessionOptions) | ||
| - | Dim directory As RemoteDirectoryInfo = session.ListDirectory("/home/martin/public_html") | + | Dim directory As RemoteDirectoryInfo = |
| + | ···················session.ListDirectory("/home/martin/public_html") | ||
| Dim fileInfo As RemoteFileInfo | Dim fileInfo As RemoteFileInfo | ||
| For Each fileInfo In directory.Files | For Each fileInfo In directory.Files | ||
| - | Console.WriteLine("{0} with size {1}, permissions {2} and last modification at {3}", _ | + | Console.WriteLine( |
| - | fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions, fileInfo.LastWriteTime) | + | ························"{0} with size {1}, permissions {2} and last modification at {3}", |
| + | fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions, | ||
| + | ·······················fileInfo.LastWriteTime) | ||
| Next | Next | ||
| End Using | End Using | ||
| Line 116: | Line 130: | ||
| ==== [[powershell]] PowerShell Example ==== | ==== [[powershell]] PowerShell Example ==== | ||
| + | Learn more about [[library_powershell|using WinSCP .NET assembly from PowerShell]]. | ||
| + | |||
| <code powershell> | <code powershell> | ||
| try | try | ||
| { | { | ||
| # Load WinSCP .NET assembly | # Load WinSCP .NET assembly | ||
| - | [Reflection.Assembly]::LoadFrom("WinSCP.dll") | Out-Null | + | Add-Type -Path "WinSCPnet.dll" |
| # Setup session options | # Setup session options | ||
| - | $sessionOptions = New-Object WinSCP.SessionOptions | + | $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ |
| - | ···$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp | + | ········Protocol = [WinSCP.Protocol]::Sftp |
| - | $sessionOptions.HostName = "example.com" | + | ·······HostName = "example.com" |
| - | $sessionOptions.UserName = "user" | + | ·······UserName = "user" |
| - | $sessionOptions.Password = "mypassword" | + | ·······Password = "mypassword" |
| - | $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | + | ·······SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
| + | } | ||
| $session = New-Object WinSCP.Session | $session = New-Object WinSCP.Session | ||
| Line 141: | Line 158: | ||
| foreach ($fileInfo in $directory.Files) | foreach ($fileInfo in $directory.Files) | ||
| { | { | ||
| - | Write-Host ("{0} with size {1}, permissions {2} and last modification at {3}" -f | + | Write-Host ("$($fileInfo.Name) with size $($fileInfo.Length), " + |
| - | $fileInfo.Name, $fileInfo.Length, $fileInfo.FilePermissions, $fileInfo.LastWriteTime) | + | "permissions $($fileInfo.FilePermissions) and " + |
| + | ················"last modification at $($fileInfo.LastWriteTime)") | ||
| } | } | ||
| } | } | ||
| Line 153: | Line 171: | ||
| exit 0 | exit 0 | ||
| } | } | ||
| - | catch [Exception] | + | catch |
| { | { | ||
| - | Write-Host $_.Exception.Message | + | Write-Host "Error: $($_.Exception.Message)" |
| exit 1 | exit 1 | ||
| } | } | ||
| </code> | </code> | ||
| + | |||
| + | ==== [[jscript]] JScript (WSH) Example ==== | ||
| + | In this example the JScript script is embedded into WSF file, to allow [[library_com_wsh#enums|access to enumeration values]]. | ||
| + | |||
| + | <code javascript> | ||
| + | <job> | ||
| + | <reference object="WinSCP.Session" /> | ||
| + | <script language="JScript"> | ||
| + | |||
| + | try | ||
| + | { | ||
| + | // Setup session options | ||
| + | var sessionOptions = WScript.CreateObject("WinSCP.SessionOptions"); | ||
| + | sessionOptions.Protocol = Protocol_Sftp; | ||
| + | sessionOptions.HostName = "example.com"; | ||
| + | sessionOptions.UserName = "user"; | ||
| + | sessionOptions.Password = "mypassword"; | ||
| + | sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..."; | ||
| + | |||
| + | var session = WScript.CreateObject("WinSCP.Session"); | ||
| + | |||
| + | try | ||
| + | { | ||
| + | // Connect | ||
| + | session.Open(sessionOptions); | ||
| + | |||
| + | var directoryInfo = session.ListDirectory("/home/martin/public_html") | ||
| + | |||
| + | var enumerator = new Enumerator(directoryInfo.Files); | ||
| + | for (; !enumerator.atEnd(); enumerator.moveNext()) | ||
| + | { | ||
| + | var fileInfo = enumerator.item(); | ||
| + | WScript.Echo( | ||
| + | fileInfo.Name + " with size " + fileInfo.Length + | ||
| + | ", permissions " + fileInfo.FilePermissions + | ||
| + | " and last modification at " + fileInfo.LastWriteTime); | ||
| + | } | ||
| + | } | ||
| + | finally | ||
| + | { | ||
| + | // Disconnect, clean up | ||
| + | session.Dispose(); | ||
| + | } | ||
| + | } | ||
| + | catch (e) | ||
| + | { | ||
| + | WScript.Echo("Error: " + e.message); | ||
| + | WScript.Quit(1); | ||
| + | } | ||
| + | |||
| + | </script> | ||
| + | </job> | ||
| + | </code> | ||
| + | |||
| + | |||
| + | ==== [[vbscript]] VBScript (WSH) Example ==== | ||
| + | In this example the VBScript script is embedded into WSF file, to allow [[library_com_wsh#enums|access to enumeration values]]. | ||
| + | |||
| + | <code vb> | ||
| + | <job> | ||
| + | <reference object="WinSCP.Session" /> | ||
| + | <script language="VBScript"> | ||
| + | |||
| + | Option Explicit | ||
| + | |||
| + | ' Setup session options | ||
| + | Dim sessionOptions | ||
| + | Set sessionOptions = WScript.CreateObject("WinSCP.SessionOptions") | ||
| + | With sessionOptions | ||
| + | .Protocol = Protocol_Sftp | ||
| + | .HostName = "example.com" | ||
| + | .UserName = "user" | ||
| + | .Password = "mypassword" | ||
| + | .SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." | ||
| + | End With | ||
| + | |||
| + | Dim session | ||
| + | Set session = WScript.CreateObject("WinSCP.Session") | ||
| + | |||
| + | ' Connect | ||
| + | session.Open sessionOptions | ||
| + | |||
| + | Dim directoryInfo | ||
| + | Set directoryInfo = session.ListDirectory("/home/martin/public_html") | ||
| + | |||
| + | Dim fileInfo | ||
| + | For Each fileInfo In directoryInfo.Files | ||
| + | WScript.Echo fileInfo.Name & " with size " & fileInfo.Length & _ | ||
| + | ", permissions " & fileInfo.FilePermissions & _ | ||
| + | " and last modification at " & fileInfo.LastWriteTime | ||
| + | Next | ||
| + | |||
| + | ' Disconnect, clean up | ||
| + | session.Dispose | ||
| + | |||
| + | </script> | ||
| + | </job> | ||
| + | </code> | ||
| + | |||
| + | ==== Real-Life Examples ==== | ||
| + | |||
| + | * [[script_download_most_recent_file|*]]; | ||
| + | * [[script_custom_listing_format_csv|*]]. | ||
| + | |||