文件夹下载为ftp中的文件

时间:2013-07-05 15:09:14

标签: powershell ftp

因此,在我的powershell脚本启动时,它会轮询一个ftp服务器并下载不在本地文件夹中的所有文件。问题是,当它到达文件夹时,它将它们作为文件下载。这是我检查新文件的代码:

$LocFolder = 'C:\EMSDropBox\*'
Remove-Item $LocFolder
$ftprequest = [System.Net.FtpWebRequest]::Create("ftp://NZHQFTP1/tbarnes")
$ftprequest.Proxy = $null
$ftprequest.KeepAlive = $false
$ftprequest.TimeOut = 10000000
$ftprequest.UsePassive = $False
$ftprequest.Credentials = New-Object System.Net.NetworkCredential("tbarnes", "Static_flow2290") 
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectory

$FTPResponse = $ftprequest.GetResponse()
$ResponseStream = $FTPResponse.GetResponseStream()
$FTPReader = New-Object System.IO.Streamreader($ResponseStream)
$filename = $FTPReader.ReadLine()
while($filename -ne $null)
{
    try
    {
        if((Test-Path ("C:\emsdropbox\"+$filename)) -ne $true)
        {
            downloadFtp($filename)
        }
        $filename = $FTPReader.ReadLine()
    }
    catch
    {
        Write-Host $_
    }
}
$FTPReader.Close()
$FTPResponse.Close()
$ResponseStream.Close()

这是downloadFtp函数:

     # FTP Config
$FTPHost = "****"
$Username = "******"
$Password = "*********"
$FTPFile = $file

# FTP Log File Url
$FTPFileUrl = "ftp://" + $FTPHost + "/tbarnes/" + $FTPFile

    # Create FTP Connection
$FTPRequest = [System.Net.FtpWebRequest]::Create("$FTPFileUrl") 
$FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username, $Password) 
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$FTPRequest.UsePassive = $false
$FTPRequest.UseBinary = $true
$FTPRequest.KeepAlive = $false
$targetfile = New-Object IO.FileStream (("C:\emsdropbox\"+$file),[IO.FileMode]::Create)
# Get FTP File
$FTPResponse = $FTPRequest.GetResponse()
$ResponseStream = $FTPResponse.GetResponseStream()
$FTPReader = New-Object -typename System.IO.StreamReader -ArgumentList $ResponseStream
[byte[]]$readbuffer = New-Object byte[] 1024

#loop through the download stream and send the data to the target file
do{
    $readlength = $ResponseStream.Read($readbuffer,0,1024)
    $targetfile.Write($readbuffer,0,$readlength)
}
while ($readlength -ne 0)
$FTPReader.Close()

我不知道为什么它不会把它们作为文件夹拉下来所以任何帮助或指针都会很棒!

1 个答案:

答案 0 :(得分:1)

FTP方法本身不支持下载文件夹或递归,因此我没有其他方法可以考虑这样做,但我在下面提出了建议。

更改方法,以便区分文件和目录并相应地处理它们。

$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectory更改为$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectoryDetails

将以此格式列出:

-rwxrwxrwx   1 owner    group          277632 Mar  4 17:15 xml_socket.log.rar

目录将在行首开始d代替-

这是一个修改后的try块,它只匹配文件,因此您可以传递给downloadFtp函数:

    try
{
    if(!($filename -match '^d')){if((Test-Path ("C:\emsdropbox\"+$filename.Split(" ")[8])) -ne $true)
    {
        downloadFtp(($filename -split '\s+')[8])
    }}
    $filename = $FTPReader.ReadLine()
}

如果您想获取目录列表,请对同一个ftpresponse流使用以下try块,并为每个ListDirectoryDetails获取每个目录中的文件列表以处理它们:

    try
{
    if($filename -match '^d'){if((Test-Path ("C:\emsdropbox\"+$filename.Split(" ")[8])) -ne $true)
    {
        listdir(($filename -split '\s+')[8])
    }}
    $filename = $FTPReader.ReadLine()
}

您可能还需要创建本地目录,您可以在powershell中执行以下操作:

New-Item c:\emsdropbox\newdir -type directory