英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论