删除使用WinSCP脚本下载的远程文件。

huangapple go评论62阅读模式
英文:

Delete downloaded remote file with WinSCP script

问题

我有一个PowerShell脚本,用于从远程SFTP服务器下载文件,运行得很好。但是我无法让代码在下载后删除这些文件。以下是我的代码:

$session.Open($sessionOptions)

# 设置传输选项
$transferOptions = New-Object WinSCP.TransferOptions -Property @{
    ResumeSupport = New-Object WinSCP.TransferResumeSupport -Property @{ State = [WinSCP.TransferResumeSupportState]::Off }
}

# 传输文件
$session.GetFiles("/remote/*.txt", "\\Local\*", $False, $transferOptions).Check()

# 删除文件
$session.RemoveFiles($difference.Remote.FileName)

但是我收到了一个错误信息:

Removals Failures                                                                                                                                                    
-------- --------                                                                                                                                                    ---------
{}       {WinSCP.SessionRemoteException: Error listing directory '/Files'....

你尝试删除文件的部分似乎出现了错误。你可以检查一下$difference.Remote.FileName的值,确保它包含你要删除的文件的正确路径和名称。此外,确保文件路径是正确的,以免出现访问权限问题。

英文:

I have a PowerShell script that is working great to grab some files from a remote SFTP. But I can not get the code to delete the files after download. Here is what I have.

$session.Open($sessionOptions)

# Set up transfer options
$transferOptions = New-Object WinSCP.TransferOptions -Property @{
    ResumeSupport = New-Object WinSCP.TransferResumeSupport -Property @{ State = [WinSCP.TransferResumeSupportState]::Off }
}

# Transfer files
$session.GetFiles("/remote/*.txt", "\\Local\*", $False, $transferOptions).Check()

# Delete Transfers
$session.RemoveFiles($difference.Remote.FileName)

Instead I get an error that states:

Removals Failures                                                                                                                                                    
-------- --------                                                                                                                                                    ---------
{}       {WinSCP.SessionRemoteException: Error listing directory '"/Files'....                                                                                           False

答案1

得分: 0

删除从服务器下载的文件,只需将true传递给Session.GetFiles的第三个参数(remove):

$session.GetFiles("/remote/*.txt", "C:\local\*", $True, $transferOptions).Check()
英文:

To delete the downloaded files from the server, just pass true to the remove (3rd) argument of Session.GetFiles:

$session.GetFiles("/remote/*.txt", "C:\local\*", $True, $transferOptions).Check()

huangapple
  • 本文由 发表于 2023年2月10日 05:00:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404369.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定