powershell :ftp 目录列表(脚本帮助)
我正在尝试创建 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, 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数据,然后我可以下载一个目录中的所有文件。
有机会吗?
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.
My goal here is to view all the ftp data in a directory and then I can download all the files in a directory.
Any chance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您应该检索所有数据,
用此替换方法,
如果没有详细信息,您只能获取文件名。
要下载文件,我通常使用 webclient 类
webclient类的Downloadfile方法
First you should retrieve all the data
Replace the method with this
Without the details you only get the filenames.
To download a file I usually use the webclient class
Downloadfile method of the webclient class