使用单个文件夹中的本地文件更新FTP服务器文件夹层次结构上的文件

时间:2016-10-23 17:34:33

标签: ftp

我有一个小问题。我需要将JPG文件从本地FOLDER复制/覆盖到服务器FOLDERS。

有没有办法搜索和匹配SERVER上的JPG文件和LOCAL上的文件,并在服务器文件夹中覆盖它们?我手动完成它需要很多时间。

服务器上有5万个JPG,我需要在短时间内覆盖2万个JPG。

非常感谢您的回答!!

2 个答案:

答案 0 :(得分:0)

没有神奇的方法可以完成你的特定任务。你必须编写脚本。

如果您使用的是Windows,使用PowerShell script及其WinSCP .NET assemblySession.EnumerateRemoteFiles method编写WinSCP .NET assembly是相当简单的:

# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"

# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Ftp
    HostName = "ftp.example.com"
    UserName = "username"
    Password = "password"
}

$remotePath = "/remote/path";
$localPath = "C:\local\Path";

# Connect
Write-Host "Connecting..."
$session = New-Object WinSCP.Session
$session.SessionLogPath = "upload.log"
$session.Open($sessionOptions)

# Enumerate remote files
$fileInfos =
    $session.EnumerateRemoteFiles(
        $remotePath, "*.*", [WinSCP.EnumerationOptions]::AllDirectories)

# And look for a matching local file for each of them
foreach ($fileInfo in $fileInfos)
{
    $localFilePath = (Join-Path $localPath $fileInfo.Name)
    if (Test-Path $localFilePath)
    {
        Write-Host ("Found local file $localFilePath matching remote file " +
            "$($fileInfo.FullName), overwriting..."
        # Command-out this line with # for a dry-run
        $session.PutFiles($localFilePath, $fileInfo.FullName).Check()
    }
    else
    {
        Write-Host ("Found no local file matching remote file " +
            "$($fileInfo.FullName), skipping..."
    }
}

Write-Host "Done"

将脚本保存到文件(SortOutFiles.ps1),提取How To Backup and Restore PostgreSQL Database Using pg_dump and psql包的内容以及脚本,然后运行它:

C:\myscript>powershell -ExecutionPolicy Bypass -File SortOutFiles.ps1
Connecting...
Found local file C:\local\path\aaa.txt matching remote file /remote/path/1/aaa.txt, overwritting...
Found local file C:\local\path\bbb.txt matching remote file /remote/path/2/bbb.txt, overwritting...
Found local file C:\local\path\ccc.txt matching remote file /remote/path/ccc.txt, overwritting...
Done

您可以先通过$session.PutFiles调用注释掉该行来干运行脚本。

(我是WinSCP的作者)

答案 1 :(得分:-1)

下载“Filezilla”...上传您的本地文件(所有50000张图片)..如果服务器中已有图片,它会询问您选项..选择'覆盖'并使用'申请全部'。 ..