2016-10-13 119 views
-1

嗨,我是新的電源shell腳本,我試圖根據FTP服務器的文件名稱下載根目錄中的所有文件和子文件夾中的文件。因爲我正在尋找下載文件名稱相似的文件,因爲我的路徑將始終指向根目錄,並且根目錄具有文本文件和子文件夾,所以子文件夾包含文本文件。從FTP服務器遞歸下載文件

我發現這個例子中的文本文件下載到FTP服務器上的地方,但我發現的例子只從根目錄下載文本文件它不掃描子文件夾和子文件夾中沒有得到的文件。

這裏是我試過

# $url = "ftp://XXX.com/" 
    $user = 'UserName' 
    $pass = 'Password' 
    $folder = 'FTP_Folder' 
    $target = "C:\Folder\Folder1\" 

    #SET CREDENTIALS 
    $credentials = new-object System.Net.NetworkCredential($user, $pass) 

    function Get-FtpDir ($url,$credentials) { 
     $request = [Net.WebRequest]::Create($url) 
     $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory 
     if ($credentials) { $request.Credentials = $credentials } 
     $response = $request.GetResponse() 
     $reader = New-Object IO.StreamReader $response.GetResponseStream() 
     $reader.ReadToEnd() 
     $reader.Close() 
     $response.Close() 
    } 

    #SET FOLDER PATH 
    $folderPath= $ftp + "/" + $folder + "/" 

    $Allfiles=Get-FTPDir -url $folderPath -credentials $credentials 
    $files = ($Allfiles -split "`r`n") 

    $files 

    $webclient = New-Object System.Net.WebClient 
    $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) 
    $counter = 0 
    foreach ($file in ($files | where {$_ -like "*.txt"})){ 
     $source=$folderPath + $file 
     $destination = $target + $file 
     $webclient.DownloadFile($source, $target+$file) 

     #PRINT FILE NAME AND COUNTER 
     $counter++ 
     $counter 
     $source 
    } 

我試着像遞歸獲取-ftpchilditem功能的模塊PSFTP模塊的功能,但它不是爲我工作的例子。

請幫我在這裏從FTP服務器的根文件夾和子文件夾中獲取文件到本地系統。

回答

1

我在過去使用WinSCP和SFTP完成了它。示例如下。這使用基本上通過SSH的SFTP SCP。我相信你可以將WInSCP協議更改爲ftp並嘗試不同的組合。希望這可以幫助你。將FileMasks更改爲只包含.txt,這將通過子文件夾。同樣對於這個工作,你將需要WinSCPnet.dll和WinSCP.exe,你可以從WinSCP站點輕鬆下載。下載「.Net Assembly/COM Library」,並將腳本和所需文件保存在同一文件夾中,並更新腳本中的路徑。祝你好運。

# Load WinSCP .NET assembly 
Add-Type -Path "C:\Users\administrator.AD\Documents\WinSCP Script\WinSCPnet.dll" 


$hostname = "ftp.xyz.net" 
$user = "me" 
$pass = "mypassword" 
$sfolder = "/home/me" 
$dfolder = "C:\tmp" 
$sshHostKeyFingerprint = "ssh-rsa 2048 eb:2c:f9:ab:19:2e:0c:22:02:d4:8f:64:44:75:ec:04" 
$mask = "*.jpg;*.jpeg;*.png;*.tiff;*.txt" 
####################### END USER CONFIGURABLE SETTINGS ######################### 

# Main script 
# Setup session options 

$sessionOptions = New-Object WinSCP.SessionOptions -Property @{ 

Protocol = [WinSCP.Protocol]::Scp 

HostName = $hostname 

UserName = $user 

Password = $pass 

SshHostKeyFingerprint = $sshHostKeyFingerprint 

} 


$session = New-Object WinSCP.Session 

$transferOptions = New-Object WinSCP.TransferOptions 

$transferOptions.FileMask = "*.jpg; *.jpeg; *.png; *.tiff;" 

try 

{ 
# Will continuously report progress of synchronization 

$session.add_FileTransferred({ FileTransferred($_) }) 

# Connect 

$session.Open($sessionOptions) 

# Synchronize files 

$synchronizationResult = $session.SynchronizeDirectories(

[WinSCP.SynchronizationMode]::Local,$dfolder, $sfolder, $False, $False, 1, $transferOptions) 

# Throw on any error 

$synchronizationResult.Check() 

} 

finally 

{ 

# Disconnect, clean up 

$session.Dispose() 

} 
+0

感謝Junaid的快速重播...這是非常有用的。 – cooluser505

+0

嗨Junid請看看下面的情景,我提到並幫助我...在此先感謝 – cooluser505

相關問題