Powershell: FTP目录列表(脚本帮助)



我正在尝试做一个ftp目录的列表视图。到目前为止,查看部分是ok的,但我无法操纵我得到的数据。下面是我使用的脚本;

[System.Net.FtpWebRequest]$ftp = [System.Net.WebRequest]::Create("ftp://ftp.microsoft.com/ResKit/y2kfix/alpha/") 
$ftp.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory #Details
$response = $ftp.getresponse() 
$stream = $response.getresponsestream() 
$buffer = new-object System.Byte[] 1024 
$encoding = new-object System.Text.AsciiEncoding 
$outputBuffer = "" 
$foundMore = $false 
## Read all the data available from the stream, writing it to the 
## output buffer when done. 
do 
{ 
    ## Allow data to buffer for a bit 
    start-sleep -m 1000 
    ## Read what data is available 
    $foundmore = $false 
    $stream.ReadTimeout = 1000
    do 
    { 
        try 
        { 
            $read = $stream.Read($buffer, 0, 1024) 
            if($read -gt 0) 
            { 
                $foundmore = $true 
                $outputBuffer += ($encoding.GetString($buffer, 0, $read)) 
            } 
        } catch { $foundMore = $false; $read = 0 } 
    } while($read -gt 0) 
} while($foundmore)
$outputBuffer

这是我得到这个脚本的响应;

PS C:UsersToshiba> C:Apps@PowerShellFTPListDirectory.ps1
forfiles.exe
logtime.exe
timeserv
w32time

从那里,我如何处理我得到的数据。文件信息(名称,创建时间,最后更新等)和其他东西。

我的目标是查看目录中所有的ftp数据,然后我可以下载目录中的所有文件。

任何机会吗?

首先,您应该检索所有数据

将方法替换为

$ftp.Method = [System.Net.WebRequestMethods+FTP]::ListDirectoryDetails

没有详细信息,只能得到文件名。

要下载文件,我通常使用webclient类
$webclient = New-Object System.Net.WebClient
$webclient.credentials = New-Object System.Net.NetworkCredential("anonymous","anonymous@test.com")
$webclient.downloadfile("ftp://ftp.host.com/file.txt", "c:tempfile.txt")

webclient类的下载文件方法

最新更新