使用 WinSCP 将文件移动到本地路径

问题描述 投票:0回答:2

我目前正在使用 WinSCP 将远程路径和本地路径之间的文件同步到桌面计算机。我想更改此设置,以便发生以下任一情况:

  1. 将文件从远程路径移动到本地路径
  2. 从远程路径复制文件到本地路径,然后只删除被删除的文件 下载到本地路径 - 在远程服务器上。

这可能吗?

这是我目前的剧本,非常感谢任何帮助。

@echo on
cls
:SetFileLogVariables
SET localdir=C:\Users\User1\Received
SET remotedir=\folder_1
SET logfile=C:\Users\Users1\Logs\Syncanddelete.log

:SetPrgVariables
SET 
prgwinscp=C:\Users\Users1\AppData\Local\Programs\WinSCP\WinSCP.com
SET winscplogin="SyncandDelete"
SET winscpfile=%temp%\~tmpWinSCPFTPSyncT_%~N0.txt
IF EXIST "%winscpfile%" DEL /Q /F "%winscpfile%"

:SetWinSCPSyncCommand
REM synchronize command: 
https://winscp.net/eng/docs/scriptcommand_synchronize
SET ftpcmd=synchronize local -delete -mirror "%localdir%\"
   
:ftpout
>>"%logfile%" ECHO.
>>"%logfile%" ECHO ***************************  FTP OUT  
***************************
     >>"%logfile%" ECHO Synchronizing files to %winscplogin% 
server  on 
%date% at %time%

>>"%winscpfile%" ECHO option batch on
>>"%winscpfile%" ECHO option confirm off
>>"%winscpfile%" ECHO option transfer binary
>>"%winscpfile%" ECHO open %winscplogin%
>>"%winscpfile%" ECHO cd "%remotedir%"
>>"%winscpfile%" ECHO %ftpcmd%
>>"%winscpfile%" ECHO close
>>"%winscpfile%" ECHO exit

>>"%logfile%" ECHO %winscpfile%                                
TYPE "%winscpfile%" >> %logfile%
>>"%logfile%" ECHO ------------------------------------------ 
"%prgwinscp%" /script="%winscpfile%" >>"%logfile%" 2>&1
>>"%logfile%" ECHO ------------------------------------------
IF EXIST "%winscpfile%" DEL /Q /F "%winscpfile%"
>>"%logfile%" ECHO Transmission complete on %date% at %time%

ping -n 2 -w 1000 127.0.0.1 > nul
batch-file sftp winscp ftp-client
2个回答
0
投票

基于https://winscp.net/eng/docs/library_example_delete_after_successful_download

param (
    $localPath = "C:\Users\Users1\Folder1",
    $remotePath = "/foldersource/"
)
  
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property 
    @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "*****"
        UserName = "*****"
        Password = "*****"
        SshHostKeyFingerprint = "ssh-rsa ********************"
        PortNumber = "****"
    }
 
    $session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        # Synchronize files to local directory, collect results
        $synchronizationResult = $session.SynchronizeDirectories(
            [WinSCP.SynchronizationMode]::Local, $localPath, 
            $remotePath, $False)
 
        # Deliberately not calling $synchronizationResult.Check
        # as that would abort our script on any error.
 
        # Iterate over every download
        foreach ($download in $synchronizationResult.Downloads)
        {
            # Success or error?
            if ($download.Error -eq $Null)
            {
                Write-Host "Download of $($download.FileName) succeeded, removing from source"
                # Download succeeded, remove file from source
                $filename = 
                    [WinSCP.RemotePath]::EscapeFileMask($download.FileName)
                $removalResult = $session.RemoveFiles($filename)

                if ($removalResult.IsSuccess)
                {
                    Write-Host "Removing of file $($download.FileName) succeeded"
                }
                else
                {
                    Write-Host "Removing of file $($download.FileName) failed"
                }
            }
            else
            {
                Write-Host (
                    "Download of $($download.FileName) failed: $($download.Error.Message)")
            }
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }

    exit 0
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}

0
投票

虽然您的自我回答中的代码(从 WinSCP 网站逐字复制)解决了问题,但对于大多数用途来说,这太过分了。

参见:


一旦你有了一个工作的 WinSCP 脚本,你可以使用 -delete

 命令的 
get
开关轻松实现将远程文件移动到本地文件夹

get -delete /source/remote/path/* C:\target\local\path\

如果您选择使用 PowerShell,您只需调用

Session.GetFilesToDirectory
并将
remove
参数设置为
true
:

$session.GetFilesToDirectory($remotePath, $localPath, $null, $true).Check()

上面一行替换了大约 40 行代码。

© www.soinside.com 2019 - 2024. All rights reserved.