我有这个脚本可以下载所有.txt和.log文件。但是我需要在下载后将它们移动到服务器上的另一个目录中。使用WinSCP .NET程序集下载后移动(不复制)远程文件
到目前为止,我打的时候老是喜欢错误,“不能动‘文件’到‘/文件’。
try
{
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::ftp
$sessionOptions.HostName = "host"
$sessionOptions.PortNumber = "port"
$sessionOptions.UserName = "user"
$sessionOptions.Password = "pass"
$session = New-Object WinSCP.Session
try
{
# Connect
$session.DisableVersionCheck = "true"
$session.Open($sessionOptions)
$localPath = "C:\users\user\desktop\file"
$remotePath = "/"
$fileName = "*.txt"
$fileNamee = "*.log"
$remotePath2 = "/completed"
$directoryInfo = $session.ListDirectory($remotePath)
$directoryInfo = $session.ListDirectory($remotePath2)
# Download the file
$session.GetFiles(($remotePath + $fileName), $localPath).Check()
$session.GetFiles(($remotePath + $fileNamee), $localPath).Check()
$session.MoveFile(($remotePath + $fileName, $remotePath2)).Check()
$session.MoveFile(($remotePath + $fileNamee, $remotePath2)).Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}
我试过这个,它工作的很好,如果我离开“$ remotePath”,只有$ transferResult = $ session.GetFiles($ fileName,$ localPath),但自然它只是将文件复制到“已完成”文件夹而不是只是移动它们,但每当我尝试添加“$ remotePath + $ fileName”它什么都不做,甚至没有下载任何建议? – 2015-02-10 19:25:01
ok,correc灰。我修正了一些我做错了的事情,所以它下载并移动“.txt”和“.log”文件夹内的文件,我怎样才能将整个文件夹移动到“已完成”文件夹?因为每天总会有新的东西,所以我想保持清洁并移动整个文件夹而不仅仅是其内容。 – 2015-02-10 19:34:53
“文件夹”实际上是一种文件,所以只需在文件夹上调用'MoveFile',就像'$ session.MoveFile(“/ path/folder_to_move”,“/ another_path/folder_to_move_to /”)' – 2015-02-11 07:21:44