powershell : ftp 目录列表(脚本帮助) [英] powershell : ftp directory listing (help on a script)

查看:46
本文介绍了powershell : ftp 目录列表(脚本帮助)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作 ftp 目录的列表视图.到目前为止,查看部分还可以,但我无法操纵我返回的数据.这是我使用的脚本;

I am trying to do a list view of a ftp directory. The viewing part is ok so far but I am unable to manipulate the data I am getting back. Here is the script that I used;

[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

这是我对这个脚本的回复;

Here is the response I am geting back for this script;

PS C:\Users\Toshiba> C:\Apps\@PowerShell\FTPListDirectory.ps1
forfiles.exe
logtime.exe
timeserv
w32time

从那里开始,我如何处理我返回的数据.文件信息(名称、创建时间、上次更新等)和其他内容.

From there, how I can work on the data I am getting back. the file info (name, creation time, last update, etc.) and the other stuff.

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

My goal here is to view all the ftp data in a directory and then I can download all the files in a directory.

有机会吗?

推荐答案

首先你应该检索所有数据

First you should retrieve all the data

用这个替换方法

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

如果没有详细信息,您只能获得文件名.

Without the details you only get the filenames.

要下载文件,我通常使用 webclient 类

To download a file I usually use the webclient class

$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")

下载文件方法webclient类的

这篇关于powershell : ftp 目录列表(脚本帮助)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆