英文:
How can I show files being downloaded in WinSCP .NET assembly from PowerShell
问题
以下是翻译好的部分:
"I have a script that downloads some recordings from an FTP server and the downloads are saved in a folder inside a local path on my computer."
我有一个脚本,从FTP服务器下载一些录音,然后将它们保存在我的计算机上本地路径内的文件夹中。
"I have been able to trigger a message when all the recordings have been downloaded successfully, but I wanted instead to display a message every time each of the FTP recordings are downloaded. How can I do that?"
我已经成功地触发了一个消息,当所有录音都成功下载时,但我想在每次下载每个FTP录音时显示一条消息。我该如何做?
"This is the script:"
这是脚本:
英文:
I have a script that downloads some recordings from an FTP server and the downloads are saved in a folder inside a local path on my computer.
I have been able to trigger a message when all the recordings have been downloaded successfully, but I wanted instead to display a message every time each of the FTP recordings are downloaded. How can I do that?
This is the script:
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Ftp
HostName = "ip of ftp"
PortNumber = port number
UserName = "user"
Password = "credentials"
}
$session = New-Object WinSCP.Session
try
{
$session.Open($sessionOptions)
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferResult =
$session.GetFiles($remotePath, $localPath, $False, $transferOptions)
$transferResult.Check()
foreach ($transfer in $transferResult.Transfers)
{
Write-Host ("Download of {0} succeeded" -f $transfer.FileName)
}
}
finally
{
$session.Dispose()
}
I have looked for ways to do it but I can't find the answer.
答案1
得分: 1
If you want to show just the names of the files as they finish transferring, use Session.FileTransferred
event:
function FileTransferred
{
param($e)
if ($e.Error -eq $Null)
{
Write-Host "Download of $($e.FileName) succeeded"
}
else
{
Write-Host "Download of $($e.FileName) failed: $($e.Error)"
}
}
$session.add_FileTransferred( { FileTransferred($_) } )
$session.Open($sessionOptions)
...
$session.GetFiles($remotePath, $localPath, $False, $transferOptions).Check()
If you want to display progress of individual file transfers, you can use Session.FileTransferProgress
.
英文:
If you want to show just the names of the files as they finish transferring, use Session.FileTransferred
event:
function FileTransferred
{
param($e)
if ($e.Error -eq $Null)
{
Write-Host "Download of $($e.FileName) succeeded"
}
else
{
Write-Host "Download of $($e.FileName) failed: $($e.Error)"
}
}
$session.add_FileTransferred( { FileTransferred($_) } )
$session.Open($sessionOptions)
...
$session.GetFiles($remotePath, $localPath, $False, $transferOptions).Check()
If you want to display progress of individual file transfers, you can use Session.FileTransferProgress
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论