我正在尝试做一个ftp目录的列表视图.到目前为止观看部分还可以,但我无法操纵我要回来的数据.这是我使用的脚本;
[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,1024) if($read -gt 0) { $foundmore = $true $outputBuffer += ($encoding.GetString($buffer,$read)) } } catch { $foundMore = $false; $read = 0 } } while($read -gt 0) } while($foundmore) $outputBuffer
以下是我为此脚本做出的回应;
PS C:\Users\Toshiba> C:\Apps\@PowerShell\FTPListDirectory.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:\temp\file.txt")