【问题标题】:FTP Download Multiple Files using PowerShell使用 PowerShell FTP 下载多个文件
【发布时间】:2012-02-02 23:58:16
【问题描述】:

我是 PowerShell 的新手,我正在尝试转换一个批处理文件,该批处理文件根据名称和扩展名从 ftp 站点上的目录下载多个文件。虽然我找到了几个下载文件的示例,但我很难找到一个显示如何下载多个文件的示例。在批处理中,我可以很简单地使用 ftp.exe 和带有通配符的 mget 命令??

谁能指点我正确的方向。

提前致谢。 约翰

【问题讨论】:

    标签: powershell ftp download


    【解决方案1】:

    可以在变量中构造文件列表,并与常规 FTP 命令一起使用....

    $FileList="file1_$cycledate.csv
    file2_$cycledate.csv
    file3_$cycledate.csv
    file4_$cycledate.csv"
    
    "open $FTPServer
        user $FTPUser $FTPPassword
    ascii
    cd report
    " +
    ($filelist.split(' ') | %{ "mget $_" }) | ftp -i -n
    

    【讨论】:

      【解决方案2】:

      它不是特定于 Powershell 的。但到目前为止,我已经尝试了许多其他解决方案

      http://ncftp.com/ 客户端效果最好。它带有ncftpls.exe 用于列出远程文件,ncftpget.exe 用于获取文件。将它们与Start-Process -Wait 一起使用

      【讨论】:

        【解决方案3】:

        这就是我所做的。因为我需要根据模式下载文件,所以我动态创建了一个命令文件,然后让 ftp 完成剩下的工作 我使用了基本的 powershell 命令。我不需要下载任何额外的组件 我首先检查是否存在所需的文件数。如果他们这样做,我第二次使用 Mget 调用 FTP。 我从连接到 windows XP 远程服务器的 windows 2008 服务器运行它

                    function make_ftp_command_file($p_file_pattern,$mget_flag)
                       {
                       # This function dynamically prepares the FTP file
                       # The file needs to be prepared daily because the pattern changes daily
                       # Powershell default encoding is Unicode
                       # Unicode command files are not compatible with FTP so we need to make sure we create an  ASCII File
        
                       write-output "USER" | out-file -filepath C:\fc.txt -encoding ASCII
                       write-output "ftpusername" | out-file -filepath C:\fc.txt -encoding ASCII -Append
                       write-output "password" | out-file -filepath C:\fc.txt -encoding ASCII -Append
                       write-output "ASCII" | out-file -filepath C:\fc.txt -encoding ASCII -Append
                       If($mget_flag -eq "Y")
                       {
                          write-output "prompt" | out-file -filepath C:\fc.txt -encoding ASCII -Append
                          write-output "mget $p_file_pattern" | out-file -filepath C:\fc.txt -encoding ASCII -Append
                       }
                       else
                       {
                          write-output "ls $p_file_pattern" | out-file -filepath C:\fc.txt -encoding ASCII -Append
                       }          
        
                       write-output quit | out-file -filepath C:\fc.txt -encoding ASCII -Append
        
                    }
        
                    ###########################  Init Section ###############################
                    $yesterday = (get-date).AddDays(-1)
                    $yesterday_fmt = date $yesterday -format "yyyyMMdd"
                    $file_pattern = "BRAE_GE_*" + $yesterday_fmt + "*.csv"
                    $file_log = $yesterday_fmt + ".log"
        
                    echo  $file_pattern
                    echo  $file_log
        
        
        
                    ############################## Main Section ############################
                    # Change location to folder where the files need to be downloaded
                    cd c:\remotefiles
        
                    # Dynamically create the FTP Command to get a list of files from the Remote Servers
                    echo "Call function that creates a FTP Command "
                    make_ftp_command_file $file_pattern N
        
        
                    #echo "Connect to remote site via FTP"
                    # Connect to Remote Server and get file listing
                    ftp -n -v -s:C:\Clover\scripts\fc.txt 10.129.120.31 > C:\logs\$file_log
        
                    $matches=select-string -pattern "BRAE_GE_[A-Z][A-Z]*" C:\logs\$file_log
        
                    # Check if the required number of Files available for download
                    if ($matches.count -eq 36)
                    {
                        # Create the ftp command file
                            # this time the command file has an mget rather than an ls
                        make_ftp_command_file $file_pattern Y
                            # Change directory if not done so
                        cd c:\remotefiles
                            # Invoke Ftp with newly created command file
                        ftp -n -v -s:C:\Clover\scripts\fc.txt 10.129.120.31 > C:\logs\$file_log 
                    }
                    else
                    {
                        echo "Full set of Files not available"
                    }
        

        【讨论】:

          【解决方案4】:

          @雅各布。您需要 ::ListDirectory 方法来制作列表。之后,您必须使用 out-file 命令将其输出到文本文件中。之后,您使用 get-content 命令导入列表。因此,对于文本文件,您可以使用 foreach 循环创建对象集合(不要忘记使用“-cne”条件跳过最后一行)。 您在此循环中包含您的 download-ftp 函数和您的循环参数。 明白了吗?不知道我的解释好不好。

          所以我的脚本中有一个例子:

          $files = Get-FtpList $ftpSource $ftpDirectory $ftpLogin $ftpPassword | Out-File -Encoding UTF8 -FilePath list.txt
          
          $list = Get-Content -Encoding UTF8 -Path list.txt
          
          foreach ($entry in $list -cne "")
          {
          Get-FtpFile $ftpSource $ftpDirectory $entry $target $ftpLogin $ftpPassword
          Start-Sleep -Milliseconds 10
          }
          

          希望它现在对你有用。

          PS:Get-FtpList 和 Get-FtpFile 是自定义函数。

          【讨论】:

            【解决方案5】:

            奇怪的是,没有内置的 cmdlet 来处理 FTP。我不确定 PowerShell 团队为什么做出这个决定,但这意味着您将不得不依赖使用 .NET 代码、第三方脚本/模块/管理单元或 Win32 程序(如 FTP.exe),因为其他人已经使用了回答。

            以下是使用 .NET 代码下载多个文件(二进制和文本)的示例:

            $files = "Firefox Setup 9.0.exe", "Firefox Setup 9.0.exe.asc"
            $ftpFolder = 'ftp://ftp.mozilla.org/pub/firefox/releases/9.0/win32/en-US'
            $outputFolder = (Resolve-Path "~\My Documents").Path
            
            foreach ($file in $files) {
                try {
                    $uri = $ftpFolder + '/' + $file
                    $request = [Net.WebRequest]::Create($uri)
                    $request.Method = [Net.WebRequestMethods+Ftp]::DownloadFile
                    $responseStream = $request.GetResponse().GetResponseStream()
            
                    $outFile = Join-Path $outputFolder -ChildPath $file
                    $fs = New-Object System.IO.FileStream $outFile, "Create"
            
                    [byte[]] $buffer = New-Object byte[] 4096
            
                    do {
                        $count = $responseStream.Read($buffer, 0, $buffer.Length)
                        $fs.Write($buffer, 0, $count)
                    } while ($count -gt 0)
            
                } catch {
                    throw "Failed to download file '{0}/{1}'. The error was {2}." -f $ftpFolder, $file, $_
                } finally {
                    if ($fs) { $fs.Flush(); $fs.Close() }
                    if ($responseStream) { $responseStream.Close() }
                }
            }
            

            【讨论】:

            • 我认为这可行,但我需要在下载之前创建 $files 列表,因为文件名每天都在变化。在 VBscript 中,我会使用“mget”和“IDY03101”之类的东西。 & Year() & Month()& Day() & "*.axf" 这将下载当天的所有文件。
            • @JohnE 您想使用Get-Date cmdlet 来格式化文件名。 $file = 'IDY03101.{0}.axf' -f (Get-Date -Format yyyyddmm).
            • 谢谢安迪,我想我现在唯一需要做的就是找到一个导出文件列表以用于 $files 的好方法。 @ravikanth 的例子会是一个很好的方法吗?
            • @JohnE 获取 FTP 目录文件列表并下载每个文件或匹配命名标准的每个文件都很容易。
            • @Andy 你将如何下载与 FTP 目录列表匹配的每个文件?
            【解决方案6】:

            您可能要检查的另一个资源:PowerShell FTP 客户端模块

            http://gallery.technet.microsoft.com/scriptcenter/PowerShell-FTP-Client-db6fe0cb

            【讨论】:

              【解决方案7】:

              在批处理中,我可以非常简单地使用 ftp.exe 和 mget 命令 带通配符??

              如果你愿意,你可以在 Powershell 中做同样的事情。

              对于更多的 Powershell 方式,您可以使用 FTPWebRequest。见这里:http://msdn.microsoft.com/en-us/library/ms229711.aspx。您可以在示例的基础上循环下载多个文件。

              但底线是,您不必将您拥有的东西批量转换为 Powershell。如果你愿意,你可以,但是你有批处理的,尤其是在调用外部程序时,应该也能正常工作。

              【讨论】:

              • 我同意你的说法,但在这种情况下使用 Win32 FTP.exe 并没有给我足够的灵活性;这有点奇怪,考虑到 PowerShell 没有给我功能!?!
              【解决方案8】:

              有多种方法可以实现这一目标。一种是使用 System.Net.FtpWebRequest,如本例所示: http://www.systemcentercentral.com/BlogDetails/tabid/143/IndexID/81125/Default.aspx

              或者您可以使用 /n Software NetCmdlet:

              http://www.nsoftware.com/powershell/tutorials/FTP.aspx

              【讨论】:

              • 感谢您的快速回复。我看了你的第一个例子,但问题是它假设我知道我将下载什么。至于 /n NetCmdlet,唉,我的公司不会为它们付钱的 :-(
              • aah,所以使用通配符是唯一的选择!我会看看我是否有任何其他参考。
              猜你喜欢
              • 1970-01-01
              • 2017-04-12
              • 2012-03-04
              • 2016-09-02
              • 1970-01-01
              • 2022-11-25
              相关资源
              最近更新 更多