windows - 电源外壳 : ftp directory listing (help on a script)

标签 windows powershell windows-vista powershell-2.0

我正在尝试创建 ftp 目录的 ListView 。到目前为止,查看部分还可以,但我无法操作我返回的数据。这是我使用的脚本;

[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:\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")

Downloadfile method of the webclient class

关于windows - 电源外壳 : ftp directory listing (help on a script),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7053243/

相关文章:

delphi - (保存对话框) 如何在 Vista/Win7 中更改文件过滤器时自动更改文件扩展名?

powershell - POWERSHELL -lt不起作用

powershell - 为什么我通过 Chef 获得以管理员身份运行 Octopus Tentacle 的 UnauthorizedAccessException?

c++ - 将目录压缩成单个文件的方法有哪些

javascript - 像exe一样在Windows窗口中运行HTML页面

powershell - 如何将多个值添加到区分大小写的Powershell哈希表?

python - 在 Vista 中以管理员身份运行已编译的 python (py2exe)

c# - 如何从 C# 中找到 "Shared Documents"文件夹的路径?

windows - 如何自动关闭 Azure VM?

windows - 当其中一个文件有错误时,如何获取特定 Mercurial 修订版的内容?