【问题标题】:Rename file on FTP with PowerShell使用 PowerShell 重命名 FTP 上的文件
【发布时间】:2012-08-17 17:45:29
【问题描述】:

有没有办法重命名 FTP 目录中的文件? 我正在将实时图像从计算机传输到 FTP,但问题是当它将图像上传到 FTP 时,它会立即替换文件。我想先上传带有临时名称的图像,然后重命名为 live.jpg。就像缓存文件上传一样。

while($true)
{
    $i++
    $File = "c:\live\temp.jpg"
    $ftp = "ftp://username:password@example.com/camera/temp.jpg"

    $webclient = New-Object System.Net.WebClient
    $uri = New-Object System.Uri($ftp)

    $webclient.UploadFile($uri, $File)
}

如何在脚本中正确使用它?

Rename-Item ..\camera\temp.jpg live.jpg

谢谢!

【问题讨论】:

    标签: powershell ftp rename webclient


    【解决方案1】:

    试试这个:

    $ftp = [System.Net.FtpWebRequest]::Create("ftp://username:password@example.com/camera/temp.jpg")
    $ftp.KeepAlive = $true
    $ftp.UsePassive = $true
    $ftp.Method = "Rename"
    $ftp.RenameTo = "camera/temp1.jpg"
    $ftp.UseBinary = $true
    $response = [System.Net.FtpWebResponse] $ftp.GetResponse()
    

    【讨论】:

    • UsePassiveUseBinary 属性对 Rename 方法没有影响,并且默认情况下它们是 $true。默认情况下,KeepAlive 也是 $true。所以这三个都可以删除,对代码结果没有任何影响。
    【解决方案2】:
    $ftp = [System.Net.FtpWebRequest]::Create("ftp://username:password@example.com/camera/temp.jpg")
    $ftp.KeepAlive = $true
    $ftp.UsePassive = $true
    $ftp.Method = "Rename"
    $ftp.RenameTo = "camera/temp1.jpg"
    $ftp.UseBinary = $true
    $response = [System.Net.FtpWebResponse] $ftp.GetResponse()
    

    我不得不调整 $ftp.RenameTo 行并删除目录以使其适合我:

    $ftp.RenameTo = "temp1.jpg"
    

    还是谢谢

    【讨论】:

    • 我认为这是意料之中的,因为当前目录设置为 /camera。查看更多解释here ... 在“备注”部分下The URI may be relative or absolute. If the URI is of the form "ftp://contoso.com/path", first the .NET Framework logs into the FTP server, then the current directory is set to <UserLoginDirectory>/path.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 2021-08-27
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    相关资源
    最近更新 更多