将文件复制到 FTP 并使用今天的日期存档

Copy files to FTP and archive with today's date

我需要创建一个执行以下操作的脚本:

  1. 将文件夹中的所有文件复制到 FTP 站点。
  2. 如果复制成功,将文件移至存档。
  3. 存档应该是一个新创建的文件夹,日期是今天(所以我们知道它们是什么时候传输的)。

我试图蚕食其他脚本以使某些东西起作用,但我没有取得任何进展,所以我需要一些帮助我已经为此工作了几个小时。

我使用 WinSCP DLL 只是因为我的其他(工作)脚本使用需要它的 SFTP。我知道正常的 FTP 没有,但我找不到任何易于传输的代码,所以尝试修改它。

所以,这是我的代码,它甚至没有 运行,没关系 运行 正确,有人可以帮助我正确吗?抱歉有点乱。

param (
    $localPath = "c:\test\source",
    $remotePath = "/upload",
    $folder = ($_.CreationTime | Get-Date -Format yyyyMMdd)
    # not sure this works but don't see how to point the destination
    # to the newly created folder
    $backupPath = "c:\test\destination$folder"  
)

try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "C:\Windows\System32\WindowsPowerShell\v1.0\WinSCPnet.dll"

    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::ftp
        HostName = "xxxxxxxx"
        UserName = "xxxxxxxx"
        Password = "xxxxxxxx"
    }

    $session = New-Object WinSCP.Session

    try
    {
        # Connect
        $session.Open($sessionOptions)

        # Upload files, collect results
        $transferResult = $session.PutFiles($localPath, $remotePath)

        # Iterate over every transfer
        foreach ($transfer in $transferResult.Transfers)
        {
            # Success or error?
            if ($transfer.Error -eq $Null)
            {
                # If today's folder doesn't exist, create it
                if (!(Test-Path $BackupPath))
                {
                    New-Item -ItemType Directory -Force -Path $BackupPath
                }

                Write-Host ("Upload of {0} succeeded, moving to Uploaded folder" -f
                    $transfer.FileName)
                # Upload succeeded, move source file to backup
                Move-Item $transfer.FileName $backupPath
            }
            else
            {
                Write-Host ("Upload of {0} failed: {1}" -f
                    $transfer.FileName, $transfer.Error.Message)
            }
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }

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

这是代码。我很高兴在 FTP 端使用内置的 PowerShell 来简化它,我只是想让它工作。

我不确定您对代码有何担忧。设置 $folder:

时,除了语法错误外,看起来还不错

您为什么还要尝试使用 $_.CreationTime 作为文件夹时间戳?只使用当前日期:

$folder = (Get-Date -Format "yyyyMMdd")

请参阅 WinSCP 文档中的 Formatting timestamps in PowerShell

我也没有看到在 params 块中设置 $folder$backupPath 的意义。将它移到 params 块之后。 如果你仍然想要这个,你在 $folder 赋值后缺少一个逗号。


除此之外,您的代码应该可以工作。

您无法使用内置的 PowerShell(或 .NET)FTP 功能来简化它,因为它没有 WinSCP .NET 程序集那样强大的命令。


我会把代码写成:

$localPath = "C:\source\local\path\*"
$remotePath = "/dest/remote/path/"
$folder = (Get-Date -Format "yyyyMMdd")
$backupPath = "C:\local\backup\path$folder"

# If today's folder doesn't exist, create it
if (!(Test-Path $BackupPath))
{
    New-Item -ItemType Directory -Force -Path $BackupPath | Out-Null
}

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

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

    $session = New-Object WinSCP.Session

    try
    {
        # Connect
        $session.Open($sessionOptions)

        # Upload files, collect results
        $transferResult = $session.PutFiles($localPath, $remotePath)

        # Iterate over every transfer
        foreach ($transfer in $transferResult.Transfers)
        {
            # Success or error?
            if ($transfer.Error -eq $Null)
            {
                Write-Host ("Upload of $($transfer.FileName) succeeded, " +
                    "moving to backup")
                # Upload succeeded, move source file to backup
                Move-Item $transfer.FileName $backupPath
            }
            else
            {
                Write-Host ("Upload of $($transfer.FileName) failed: " +
                    "$($transfer.Error.Message)")
            }
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }

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

基于Moving local files to different location after successful upload.