【问题标题】:PowerShell FTP download files and subfoldersPowerShell FTP 下载文件和子文件夹
【发布时间】:2016-09-02 00:09:48
【问题描述】:

我喜欢编写一个 PowerShell 脚本来从我的 FTP 服务器下载所有文件子文件夹。我找到了一个脚本,可以从一个特定文件夹下载所有文件,但我也喜欢下载子文件夹及其文件。

#FTP Server Information - SET VARIABLES
$ftp = "ftp://ftp.abc.ch/" 
$user = 'abc' 
$pass = 'abc'
$folder = '/'
$target = "C:\LocalData\Powershell\"

#SET CREDENTIALS
$credentials = new-object System.Net.NetworkCredential($user, $pass)

function Get-FtpDir ($url,$credentials) {
    $request = [Net.WebRequest]::Create($url)
    $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
    if ($credentials) { $request.Credentials = $credentials }
    $response = $request.GetResponse()
    $reader = New-Object IO.StreamReader $response.GetResponseStream() 
    $reader.ReadToEnd()
    $reader.Close()
    $response.Close()
}

#SET FOLDER PATH
$folderPath= $ftp + "/" + $folder + "/"

$Allfiles=Get-FTPDir -url $folderPath -credentials $credentials
$files = ($Allfiles -split "`r`n")

$files 

$webclient = New-Object System.Net.WebClient 
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) 
$counter = 0
foreach ($file in ($files | where {$_ -like "*.*"})){
    $source=$folderPath + $file  
    $destination = $target + $file 
    $webclient.DownloadFile($source, $target+$file)

    #PRINT FILE NAME AND COUNTER
    $counter++
    $counter
    $source
}

感谢您的帮助(:

【问题讨论】:

    标签: powershell download ftp subdirectory


    【解决方案1】:

    .NET 框架或 PowerShell 对递归文件操作(包括下载)没有任何明确的支持。您必须自己实现递归:

    • 列出远程目录
    • 迭代条目、下载文件并递归到子目录(再次列出它们等)

    棘手的部分是从子目录中识别文件。使用 .NET 框架(FtpWebRequestWebClient)无法以可移植的方式做到这一点。不幸的是,.NET 框架不支持MLSD 命令,这是在 FTP 协议中检索具有文件属性的目录列表的唯一可移植方式。另见Checking if object on FTP server is file or directory

    您的选择是:

    • 对文件名执行操作,对于文件肯定会失败,而对于目录会成功(反之亦然)。 IE。您可以尝试下载“名称”。如果成功,它是一个文件,如果失败,它是一个目录。
    • 您可能很幸运,在您的特定情况下,您可以通过文件名来区分目录中的文件(即,您的所有文件都有扩展名,而子目录没有)
    • 您使用长目录列表(LIST 命令 = ListDirectoryDetails 方法)并尝试解析特定于服务器的列表。许多 FTP 服务器使用 *nix 样式的列表,您可以在其中通过条目开头的 d 标识目录。但是许多服务器使用不同的格式。以下示例使用这种方法(假设为 *nix 格式)
    function DownloadFtpDirectory($url, $credentials, $localPath)
    {
        $listRequest = [Net.WebRequest]::Create($url)
        $listRequest.Method =
            [System.Net.WebRequestMethods+Ftp]::ListDirectoryDetails
        $listRequest.Credentials = $credentials
        
        $lines = New-Object System.Collections.ArrayList
    
        $listResponse = $listRequest.GetResponse()
        $listStream = $listResponse.GetResponseStream()
        $listReader = New-Object System.IO.StreamReader($listStream)
        while (!$listReader.EndOfStream)
        {
            $line = $listReader.ReadLine()
            $lines.Add($line) | Out-Null
        }
        $listReader.Dispose()
        $listStream.Dispose()
        $listResponse.Dispose()
    
        foreach ($line in $lines)
        {
            $tokens = $line.Split(" ", 9, [StringSplitOptions]::RemoveEmptyEntries)
            $name = $tokens[8]
            $permissions = $tokens[0]
    
            $localFilePath = Join-Path $localPath $name
            $fileUrl = ($url + $name)
    
            if ($permissions[0] -eq 'd')
            {
                if (!(Test-Path $localFilePath -PathType container))
                {
                    Write-Host "Creating directory $localFilePath"
                    New-Item $localFilePath -Type directory | Out-Null
                }
    
                DownloadFtpDirectory ($fileUrl + "/") $credentials $localFilePath
            }
            else
            {
                Write-Host "Downloading $fileUrl to $localFilePath"
    
                $downloadRequest = [Net.WebRequest]::Create($fileUrl)
                $downloadRequest.Method =
                    [System.Net.WebRequestMethods+Ftp]::DownloadFile
                $downloadRequest.Credentials = $credentials
    
                $downloadResponse = $downloadRequest.GetResponse()
                $sourceStream = $downloadResponse.GetResponseStream()
                $targetStream = [System.IO.File]::Create($localFilePath)
                $buffer = New-Object byte[] 10240
                while (($read = $sourceStream.Read($buffer, 0, $buffer.Length)) -gt 0)
                {
                    $targetStream.Write($buffer, 0, $read);
                }
                $targetStream.Dispose()
                $sourceStream.Dispose()
                $downloadResponse.Dispose()
            }
        }
    }
    

    使用如下函数:

    $credentials = New-Object System.Net.NetworkCredential("user", "mypassword") 
    $url = "ftp://ftp.example.com/directory/to/download/"
    DownloadFtpDirectory $url $credentials "C:\target\directory"
    

    代码翻译自我在C# Download all files and subdirectories through FTP 中的C# 示例。

    虽然Microsoft does not recommend FtpWebRequest for a new development.


    如果您想避免解析服务器特定目录列表格式的麻烦,请使用支持MLSD 命令和/或解析各种LIST 列表格式的第三方库;和递归下载。

    例如使用WinSCP .NET assembly,您只需调用Session.GetFiles即可下载整个目录:

    # 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 = "user"
        Password = "mypassword"
    }
    
    $session = New-Object WinSCP.Session
    
    try
    {
        # Connect
        $session.Open($sessionOptions)
    
        # Download files
        $session.GetFiles("/directory/to/download/*", "C:\target\directory\*").Check()
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }    
    

    如果服务器支持,WinSCP 在内部使用MLSD 命令。如果没有,它使用LIST 命令并支持数十种不同的列表格式。

    Session.GetFiles method 默认是递归的。

    (我是 WinSCP 的作者)

    【讨论】:

    • 非常感谢。与 winscp 完美搭配!
    【解决方案2】:

    为了通过powerShell从FTP中检索文件/文件夹,我写了一些函数,你甚至可以从FTP中获取隐藏的东西。

    获取特定文件夹中所有文件和子文件夹(甚至是隐藏的)的示例:

    Get-FtpChildItem -ftpFolderPath "ftp://myHost.com/root/leaf/" -userName "User" -password "pw" -Directory -File
    

    您可以直接从以下模块复制函数,而无需安装任何第三个库: https://github.com/AstralisSomnium/PowerShell-No-Library-Just-Functions/blob/master/FTPModule.ps1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多