![](/img/trans.png)
[英]How to download files that contain a pound/hash sign '#' in the filename from an FTP server using PowerShell's FtpWebRequest class
[英]How to download multiple files from ftp using powershell
我已經有一個循環,該循環列出了FTP目錄中的文件摘要。 $ outputBuffer包含
/archive/20120806_141250.txt
/archive/20120806_142114.txt
/archive/20120807_090149.txt
/archive/20120808_090348.txt
我有一個For-EachObject用完整的ftp地址替換/ archive:
$outputBuffer | Foreach-Object {
$_ -replace 'archive/', 'ftp://ftp.test.com/outbound/archive/'
}
我的結果是:
ftp://test.com/outbound/archive/20120806_141250.txt
ftp://test.com/outbound/archive/20120806_142114.txt
ftp://test.com/outbound/archive/20120807_090149.txt
ftp://test.com/outbound/archive/20120808_090348.txt
替換后,如何在此ForEach-Object語句中添加一行以下載每個文件?
$links = $outputBuffer | Foreach-Object {
$_ -replace 'archive/', 'ftp://ftp.test.com/outbound/archive/'
}
接着
foreach($link in $links)
{
Invoke-WebRequest $link
}
這應該在PWD中下載文件
嘗試這個:
$webclient = New-Object System.Net.WebClient
$webclient.credentials = new-object system.net.networkcredential("user", "password")
$outputBuffer | Foreach-Object {
$filePath = $_ -replace 'archive/', 'ftp://ftp.test.com/outbound/archive/'
#Adjust D:\local\ to be an actual folder.
$fileDest = $_ -replace '/archive/', 'D:\local\'
$uri = New-Object System.Uri($filePath)
$webclient.DownloadFile($uri, $fileDest)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.