【问题标题】:Copy files to FTP and archive with today's date将文件复制到 FTP 并以今天的日期存档
【发布时间】:2018-05-08 11:40:56
【问题描述】:

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

  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 来简化它,我只是希望它能够工作。

【问题讨论】:

    标签: powershell ftp winscp winscp-net


    【解决方案1】:

    我不确定您对代码有什么顾虑。设置$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

    【讨论】:

    • 在添加日志以查看哪里出错后让它工作。谢谢您的帮助! (是权限问题阻止了最后一步的工作)
    猜你喜欢
    • 1970-01-01
    • 2011-04-18
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    相关资源
    最近更新 更多