在PowerShell中检查FTP服务器上的文件是否存在 [英] Check file existence on FTP server in PowerShell

查看:229
本文介绍了在PowerShell中检查FTP服务器上的文件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查FTP服务器上是否存在某些文件.我用Test-Path编写了代码,但是没有用.然后,我编写了代码来获取FTP服务器文件的大小,但是它也不起作用.

I want to check if some file exists on FTP server. I wrote code with Test-Path but it's not working. Then I wrote code to get FTP server file size, but it's not working either.

我的代码

function File-size()
{
   Param ([int]$size)
   if($size -gt 1TB) {[string]::Format("{0:0.00} TB ",$size /1TB)}
   elseif($size -gt 1GB) {[string]::Format("{0:0.00} GB ",$size/1GB)}
   elseif($size -gt 1MB) {[string]::Format("{0:0.00} MB ",$size/1MB)}
   elseif($size -gt 1KB) {[string]::Format("{0:0.00} KB ",$size/1KB)}
   elseif($size -gt 0) {[string]::Format("{0:0.00} B ",$size)}
   else                {""}
}

$urlDest = "ftp://ftpxyz.com/folder/ABCDEF.XML"
$sourcefilesize = Get-Content($urlDest)
$size = File-size($sourcefilesize.length)
Write-Host($size)

此代码不起作用.

错误

获取内容:找不到驱动器.名称为``ftp''的驱动器不存在.C:\ documents \ upload-file.ps1:67 char:19 + $ sourcefilesize = Get-Item($ urlDest) + ~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:ObjectNotFound:(ftp:String)[获取内容],DriveNotFoundException + FullyQualifiedErrorId:DriveNotFound,Microsoft.PowerShell.Commands.GetContentCommand

Get-Content : Cannot find drive. A drive with the name 'ftp' does not exist.At C:\documents\upload-file.ps1:67 char:19 + $sourcefilesize = Get-Item($urlDest) + ~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (ftp:String) [Get-Content], DriveNotFoundException + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetContentCommand

任何想法如何解决此错误?有什么办法可以检查FTP服务器中是否存在某些文件?关于此的任何线索都将有所帮助.

Any idea how to solve this error? Is there any way I can check some exists into FTP server? Any clue regarding this will be helpful.

推荐答案

您不能将Test-PathGet-Content与FTP URL一起使用.

You cannot use Test-Path nor Get-Content with FTP URL.

您必须使用FTP客户端,例如WebRequest( FtpWebRequest ).

You have to use FTP client, like WebRequest (FtpWebRequest).

尽管它没有任何明确的方法来检查文件是否存在(部分原因是FTP协议本身不具有这种功能).您需要滥用" GetFileSizeGetDateTimestamp之类的请求.

Though it does not have any explicit method to check file existence (partly because FTP protocol itself does not have such functionality). You need to "abuse" a request like GetFileSize or GetDateTimestamp.

$url = "ftp://ftp.example.com/remote/path/file.txt"

$request = [Net.WebRequest]::Create($url)
$request.Credentials = New-Object System.Net.NetworkCredential("username", "password");
$request.Method = [System.Net.WebRequestMethods+Ftp]::GetFileSize

try
{
    $request.GetResponse() | Out-Null
    Write-Host "Exists"
}
catch
{
    $response = $_.Exception.InnerException.Response;
    if ($response.StatusCode -eq [System.Net.FtpStatusCode]::ActionNotTakenFileUnavailable)
    {
        Write-Host "Does not exist"
    }
    else
    {
        Write-Host ("Error: " + $_.Exception.Message)
    }
}

该代码基于如何在FtpWebRequest之前在FTP上检查文件是否存在的C#代码.

The code is based on C# code from How to check if file exists on FTP before FtpWebRequest.

如果您想要更直接的代码,请使用一些第三方FTP库.

If you want a more straightforward code, use some 3rd party FTP library.

例如,对于 WinSCP .NET程序集,您可以使用其

For example with WinSCP .NET assembly, you can use its Session.FileExists method:

Add-Type -Path "WinSCPnet.dll"

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

$session = New-Object WinSCP.Session
$session.Open($sessionOptions)

if ($session.FileExists("/remote/path/file.txt"))
{
    Write-Host "Exists"
}
else
{
    Write-Host "Does not exist"
}

(我是WinSCP的作者)

这篇关于在PowerShell中检查FTP服务器上的文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆