2011-08-13 4 views
6

ftp 디렉토리 목록보기를 수행하려고합니다. 보기 부분은 지금까지는 괜찮지 만 돌아 오는 데이터를 조작 할 수 없습니다. 다음은 내가 사용한 스크립트입니다.powershell : 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 데이터를보고 디렉토리의 모든 파일을 다운로드 할 수 있습니다.

기회가 있으십니까?

답변

8

은 첫째로 당신은 당신은 단지 파일 이름을 얻을 세부 사항없이이

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

과 방법을 바꾸기 모든 데이터

를 검색합니다.

내가 보통 WebClient를 클래스를 사용하여 파일을 다운로드하려면

$webclient = New-Object System.Net.WebClient 
$webclient.credentials = New-Object System.Net.NetworkCredential("anonymous","[email protected]") 
$webclient.downloadfile("ftp://ftp.host.com/file.txt", "c:\temp\file.txt") 

Downloadfile method of the webclient class